13

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.

Garima Tiwari
  • 1,490
  • 6
  • 28
  • 46
  • create a layout with two views and set that as the content for the AlertDialog – Triode Apr 23 '13 at 12:53
  • You can do this with creating a dialog (without alertDialog.Builder) and set a content view defined in a xml layout. So, You get Your own custom layout for a dialog. – Opiatefuchs Apr 23 '13 at 12:54
  • Can you please provide some example code in the answers so I could try them and accept them in case they work out? Thank you. – Garima Tiwari Apr 23 '13 at 12:57
  • http://stackoverflow.com/questions/13341560/how-to-create-a-custom-dialog-box-in-android/13342157#13342157 – Chintan Khetiya Apr 23 '13 at 12:57
  • @chintankhetiya, thanks! but I am looking towards the goal of adding views to a dialog instead of having to create a seperate layout for the same. Any help there? – Garima Tiwari Apr 23 '13 at 13:02
  • as per you question title i assume that your need is just want to add two edittext inside dialog and get that value.is it ? or just show how should it looks ? i didn't get clearly. – Chintan Khetiya Apr 23 '13 at 13:05
  • I want to add two edit text fields along with other text fields, the only error is that its not able to identify both the views simultaneously. Only one of them is visible at a time. – Garima Tiwari Apr 23 '13 at 13:20
  • http://chat.stackoverflow.com/rooms/19132/java-and-android-era – Chintan Khetiya Apr 23 '13 at 13:23
  • Okay this is embarrassing. My office has blocked all chat rooms so I cant come on chat. Sigh. – Garima Tiwari Apr 23 '13 at 13:26

4 Answers4

29

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();
tharkay
  • 5,913
  • 2
  • 26
  • 33
Arun C
  • 9,035
  • 2
  • 28
  • 42
  • Hey, thanks for the above! Any solution regarding adding the view to the dialog instead of having to create a layout for it? – Garima Tiwari Apr 23 '13 at 13:22
  • Great! Just the solution I was looking for. Another one if you could help me with, is there a way I can position my editText view against the left of a textView? Example: **Lot Number** : **EDIT TEXT FIELD** – Garima Tiwari Apr 24 '13 at 03:51
  • @Garima add Horizontal LinearLayout in that and then put TextView and EditText in that.. – Sagar Maiyad May 23 '13 at 04:58
10

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();
Gil Allen
  • 1,169
  • 14
  • 24
2

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

Community
  • 1
  • 1
AlexVogel
  • 10,601
  • 10
  • 61
  • 71
1

Why don't you make a entirely custom layout for it?

Here is a custom pop up i use for showing a list of categorys and letting the user pick one.

public class CategoryPickerFragment extends DialogFragment implements OnItemClickListener{
private CategoryReceiver receiver;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    View view = inflater.inflate(R.layout.category_picker_fragment, null);

    builder.setView(view);
    AlertDialog ad = builder.create();

    CategoryList categoryList = (CategoryList) view.findViewById(R.id.clCategories);
    categoryList.setOnItemClickListener(this);

    return ad;
}
public void setCategoryReceiver(CategoryReceiver receiver){
    this.receiver = receiver;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Category category = ((CategoryListChild)view).getCategory();
    receiver.setCategory(category);
    this.dismiss();
}

Note that i extend a DialogFragment, override the OnCreateDialog a alertDialog with a custom layout and then show it to the user.

SverkerSbrg
  • 503
  • 1
  • 9
  • 23
  • Thanks for the quick reply but I was looking towards something along the lines of adding the view to the alert rather than having to code the layout. Any help with that will be great! – Garima Tiwari Apr 23 '13 at 13:18