How to make a edittext box in a dialog How to make a edittext box in a dialog android android

How to make a edittext box in a dialog


I know its too late to answer this question but for others who are searching for some thing similar to this here is a simple code of an alertbox with an edittext

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

or

new AlertDialog.Builder(mContext, R.style.MyCustomDialogTheme);

if you want to change the theme of the dialog.

final EditText edittext = new EditText(ActivityContext);alert.setMessage("Enter Your Message");alert.setTitle("Enter Your Title");alert.setView(edittext);alert.setPositiveButton("Yes Option", new DialogInterface.OnClickListener() {    public void onClick(DialogInterface dialog, int whichButton) {        //What ever you want to do with the value        Editable YouEditTextValue = edittext.getText();        //OR        String YouEditTextValue = edittext.getText().toString();    }});alert.setNegativeButton("No Option", new DialogInterface.OnClickListener() {    public void onClick(DialogInterface dialog, int whichButton) {        // what ever you want to do with No option.    }});alert.show();


Use Activtiy Context

Replace this

  final EditText input = new EditText(this);

By

  final EditText input = new EditText(MainActivity.this);    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(                        LinearLayout.LayoutParams.MATCH_PARENT,                        LinearLayout.LayoutParams.MATCH_PARENT);  input.setLayoutParams(lp);  alertDialog.setView(input); // uncomment this line


Simplest of all would be.

  • Create xml layout file for dialog . Add whatever view you want likeEditText , ListView , Spinner etc.

    Inflate this view and set this to AlertDialog

Lets start with Layout file first.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center_horizontal"    android:orientation="vertical">    <EditText        android:id="@+id/etComments"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="top"        android:hint="Enter comments(Optional)"        android:inputType="textMultiLine"        android:lines="8"        android:maxLines="3"        android:minLines="6"        android:scrollbars="vertical" /></LinearLayout>

final View view = layoutInflater.inflate(R.layout.xml_file_created_above, null);AlertDialog alertDialog = new AlertDialog.Builder(ct).create();alertDialog.setTitle("Your Title Here");alertDialog.setIcon("Icon id here");alertDialog.setCancelable(false);Constant.alertDialog.setMessage("Your Message Here");final EditText etComments = (EditText) view.findViewById(R.id.etComments);alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new OnClickListener() {    @Override    public void onClick(DialogInterface dialog, int which) {    }});alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new OnClickListener() {    @Override    public void onClick(DialogInterface dialog, int which) {        alertDialog.dismiss()    }});alertDialog.setView(view);alertDialog.show();