Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add two edit text fields or views in an AlertDialog box?

I want to add two edit text fields in an alert dialog box. As simple as the solution sounds I have not been able to gather a working one as of yet. I am not able to set the two (edit text) views simultaneously.

Please comment in case you want to see any further code.

                alertDialog.setTitle("Values");
                final EditText quantity = new EditText(SecondScan.this);
                final EditText lot = new EditText(SecondScan.this);

                quantity.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                lot.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

                Project=arr[0].toString();
                Item=arr[1].toString();


                alertDialog.setMessage( "Employee No. : " + (Login.user).trim()+
                        "\nWarehouse      : " + (FirstScan.Warehouse).trim()+ 
                        "\nLocation           : " + (FirstScan.Location).trim()+ 
                        "\nProject              : " + Project.trim() + 
                        "\nItem                   : " + Item.trim() + 
                        "\nLot                      : " + Lot.trim()+  
                        "\n\nQuantity   :" );
                alertDialog.setView(quantity);
                    alertDialog.setView(lot);
 // the bit of code that doesn't seem to be working.


                alertDialog.setCancelable(false);
                alertDialog.setPositiveButton("Update",  new DialogInterface.OnClickListener() { 

                    public void onClick(DialogInterface dialog, int id) {
                        //ACTION
                    }
                });

                AlertDialog alert = alertDialog.create();
                alert.show();

I want the first edit text to occur after the lot and the second one after the quantity whereas only one of them seems to be working when I try pushing in both the views.

UPDATE : As it turns out there is in fact no method of adding more than one view alone to an alert dialog box without having to create a layout for it.

like image 525
Garima Tiwari Avatar asked Apr 23 '13 12:04

Garima Tiwari


People also ask

How many button options can you use in creating AlertDialog?

An alert dialog can have maximum three action buttons. If you want the user to accept the action, use Positive action button. It is normally displayed as OK/YES. If the user wants to cancel the action , then you can use Negative action button (NO).

What is the difference between dialog and AlertDialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

What are the three buttons that can be added to a dialog?

Android Alert Dialog is built with the use of three fields: Title, Message area, and Action Button. Alert Dialog code has three methods: setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message.

What is the correct syntax for creating an object of AlertDialog builder to make an alert dialog?

AlertDialog alertDialog = alertDialogBuilder. create(); alertDialog. show(); This will create the alert dialog and will show it on the screen.


3 Answers

See Creating a Custom Layout in android.

enter image description here

EDIT

alertDialog.setTitle("Values");
final EditText quantity = new EditText(SecondScan.this);
final EditText lot = new EditText(SecondScan.this);

quantity.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
lot.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

Project=arr[0].toString();
Item=arr[1].toString();

LinearLayout ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(quantity);
ll.addView(lot);
alertDialog.setView(ll);

alertDialog.setCancelable(false);
alertDialog.setPositiveButton("Update",  new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        //ACTION
    }
});

AlertDialog alert = alertDialog.create();
alert.show();
like image 176
Arun C Avatar answered Oct 07 '22 03:10

Arun C


I used LinearLayout for a login pop-up:

public final String POPUP_LOGIN_TITLE="Sign In";
public final String POPUP_LOGIN_TEXT="Please fill in your credentials";
public final String EMAIL_HINT="--Email--";
public final String PASSWORD_HINT="--Password--";

AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle(POPUP_LOGIN_TITLE);
        alert.setMessage(POPUP_LOGIN_TEXT);

        // Set an EditText view to get user input 
        final EditText email = new EditText(this);
        email.setHint(EMAIL_HINT);
        final EditText password = new EditText(this);
        password.setHint(PASSWORD_HINT);
        LinearLayout layout = new LinearLayout(getApplicationContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(email);
        layout.addView(password);
        alert.setView(layout);

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

          // Do something with value!
          }
        });

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

        alert.show();
like image 10
Gil Allen Avatar answered Oct 07 '22 02:10

Gil Allen


You should create a vertical LinearLayout on which you can add your EditTexts. Then use alertDialog.setView() with the LinearLayout.

Look here for mor infos: How to implement a custom AlertDialog View or here How to add two edit text fields in an alert dialog

like image 2
AlexVogel Avatar answered Oct 07 '22 04:10

AlexVogel