1

I have created a custom AlertDialog with multiple EditText fields using a custom layout. Now the problem is don't know how to get data from these EditText when user clicks. Here's the AlertDialog.

    case R.id.menu_newitem:
        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.newvslayout, null);
        new AlertDialog.Builder(this).setTitle("New VS")
                .setView(textEntryView).setPositiveButton("Ok", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked OK so do some stuff */
                    }
                }).setNeutralButton("Cancel", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked cancel so do some stuff */
                    }
                }).show();

And this is my layout newvslayout.xml

....
....
<EditText android:id="@+id/vsname" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="Name"
    android:inputType="text" />

<EditText android:id="@+id/csize" android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
            android:text="100" 
    android:inputType="number" />

<EditText android:id="@+id/dsize" android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="600" android:inputType="number" />

<EditText android:id="@+id/ssize" android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="200" android:inputType="number" />
....
....
Binoy Babu
  • 16,699
  • 17
  • 91
  • 134

2 Answers2

3
edittext t1 = (edittext) textEntryView.findviewbyid(R.id.vsname);
String s = t1.gettext().toString();
Royi
  • 745
  • 8
  • 22
1

Let me expand on user1027958's answer:

First, we need to get hold of the EditText components:

EditText et_vsName = (EditText) textEntryView.findviewbyid(R.id.vsname);
EditText et_cSize = (EditText) textEntryView.findviewbyid(R.id.csize);
EditText et_dSize = (EditText) textEntryView.findviewbyid(R.id.dsize);
EditText et_sSize = (EditText) textEntryView.findviewbyid(R.id.ssize);

Then we can get the text inside each of the EditText boxes into a String variable, for example:

String vsName = et_vsName.getText().toString();

The finished code would probably look like this:

case R.id.menu_newitem:
    LayoutInflater factory = LayoutInflater.from(this);
    final View textEntryView = factory.inflate(R.layout.newvslayout, null);
    final EditText et_vsName = (EditText) textEntryView.findviewbyid(R.id.vsname);
    final EditText et_cSize = (EditText) textEntryView.findviewbyid(R.id.csize);
    final EditText et_dSize = (EditText) textEntryView.findviewbyid(R.id.dsize);
    final EditText et_sSize = (EditText) textEntryView.findviewbyid(R.id.ssize);
    new AlertDialog.Builder(this).setTitle("New VS")
            .setView(textEntryView).setPositiveButton("Ok", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int whichButton) {
                    //Get the value of the EditText(s)
                    String vsName = et_vsName.getText().toString();
                }
            }).setNeutralButton("Cancel", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int whichButton) {
                    //Close the dialog
                    this.dismiss();
                }
            }).show();
Todd Davies
  • 5,484
  • 8
  • 47
  • 71