Friday, 8 February 2013

How to add view dynamically on AlertDialogBox

 This tutorial relates to the adding view(such as text view,edit-text,etc) to the Alert dialog
 dynamically in a java code.This will be used for creating the log in screen with Alert dialog.

Step1)First,create the object of the Alert Dialog builder as follows:

              AlertDialog.Builder alert = new AlertDialog.Builder(this);
              alert.setTitle("Login Screen");

Step2) After you create object of the views you want and add properties to them with following:
 
             LinearLayout lila1 = new LinearLayout(this);
             lila1.setOrientation(1);
             TextView t1 = new TextView(this);
             TextView t2 = new TextView(this);
             t1.setText("Enter your Login ID:");
             t1.setTextColor(Color.parseColor("#FFFFFF"));
             input1 = new EditText(this);
             t2.setText("Enter your Password:");
             t2.setTextColor(Color.parseColor("#FFFFFF"));
             input2 = new EditText(this);
             input2.setInputType(0x00000081);

Step3)Then,you add the view and set the view with the following:

             lila1.addView(t1);
             lila1.addView(input1);
             lila1.addView(t2);
             lila1.addView(input2);
             alert.setView(lila1);

Step4) Last,To add the buttons to the alert dialog you do the following:

              alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {

               }
              });

              alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int whichButton) {
                    
                }
                });

 Step5)    
                alert.show();

Thanks..

                  
            

3 comments:

  1. Please tell me how to open dialog box within dialog box means open spinner's list within dialog box.
    Please reply me. Thanks....

    ReplyDelete
  2. This is basically the type of Custom-Dialog box in this you will add the views to the dialog.so refer this link.
    http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

    ReplyDelete
  3. Thank you so much. Your explanation for code is too good. You are doing a great job. Thanks a lot !

    ReplyDelete

 

Copyright © Unique Android Tutorial.. Design by Free CSS Templates | Blogger Theme by BTDesigner | Powered by Blogger