Eman Ch
Eman Ch

Reputation: 192

android edittext with alertdialog

I have EditText that, when I click on it, it shows a dialog list of some items. Now when I click on an item I want the item which I select to automatically be entered in the EditText. Help me to solve this. Thanks in advance.

code of alert dialog

public void alert(View view) {

/*
 * WebView is created programatically here.
 *
 * @Here are the list of items to be shown in the list
 */
    final CharSequence[] items = { "John", "Michael", "Vincent", "Dalisay" };

    AlertDialog.Builder builder = new AlertDialog.Builder(AddNewDish.this);
    builder.setTitle("Make your selection");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
           // String items = quantity.getText().toString();
            // will toast your selection
           // showToast("Name: " + items[item]);
            dialog.dismiss();

        }
    }).show();
}

code of edittext

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:onClick="alert"
    android:id="@+id/dish_quantity"
    android:layout_below="@+id/dish_name"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:hint="Quantity" />

like

enter image description here

Upvotes: 1

Views: 236

Answers (2)

ryguy
ryguy

Reputation: 481

If you have reference to the EditText that you want to write to, simply add the code to write to it in you onClick() method.

The way to write text to an EditText is as follows yourEditText.setText("Your text");. So in your case, just set the text to your selected item.. i.e. yourEditText.setText(items[item]);.

Let me know if this helps, or if you have any other issues/questions!

Upvotes: 1

once2go
once2go

Reputation: 1500

This one:

final EditText yourEditTextView =(EditText)findViewById(R.id. dish_quantity);
final CharSequence[] items = { "John", "Michael", "Vincent", "Dalisay" };
 builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            yourEditTextView.setText(items[item].toString());
            dialog.dismiss();

        }
    }).show()

Upvotes: 0

Related Questions