6

I want to show a Dialog with an EditText. I envision it having two buttons: a positive button, which will persist the text contained in the EditText, and a negative button, which will cancel and return to the Activity that launched it as if nothing had happened. I have tried using an AlertDialog with no success:

AlertDialog.Builder builder = new Builder(context);
final EditText text = new EditText(context);

builder.setTitle("New Profile").setMessage("Name this new profile").setView(text);
builder.setPositiveButton("Create", new OnClickListener() {

    public void onClick(DialogInterface di, int i) {
        final String name = text.getText().toString();
        //do something with it
    }
});
builder.setNegativeButton("Cancel", new OnClickListener() {

    public void onClick(DialogInterface di, int i) {
    }
});
builder.create().show();

Am I doing something wrong with the setView() method? Would using a custom Dialog be more practical?

EDIT:

I've gotten several answers saying the code works for them... not sure why it isn't working for me. Here's the logcat:

E/AndroidRuntime(  326): FATAL EXCEPTION: main
E/AndroidRuntime(  326): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
E/AndroidRuntime(  326):    at android.view.ViewRoot.setView(ViewRoot.java:531)
E/AndroidRuntime(  326):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
E/AndroidRuntime(  326):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
E/AndroidRuntime(  326):    at android.app.Dialog.show(Dialog.java:241)
E/AndroidRuntime(  326):    at com.gobernador.TopPage.onListItemClick(TopPage.java:77)
E/AndroidRuntime(  326):    at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
E/AndroidRuntime(  326):    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
E/AndroidRuntime(  326):    at android.widget.ListView.performItemClick(ListView.java:3513)
E/AndroidRuntime(  326):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
E/AndroidRuntime(  326):    at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(  326):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(  326):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  326):    at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(  326):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  326):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(  326):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(  326):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(  326):    at dalvik.system.NativeStart.main(Native Method)
gobernador
  • 5,659
  • 3
  • 32
  • 51

4 Answers4

2

gobernador Agarwal is right , i also tried you code and it was working , try it by replacing context with this

AlertDialog.Builder builder = new Builder(this);
        final EditText text = new EditText(this);

        builder.setTitle("New Profile").setMessage("Name this new profile").setView(text);
        builder.setPositiveButton("Create", new OnClickListener() {

            public void onClick(DialogInterface di, int i) {
                final String name = text.getText().toString();
                //do something with it
            }
        });
        builder.setNegativeButton("Cancel", new OnClickListener() {

            public void onClick(DialogInterface di, int i) {
            }
        });
        builder.create().show();
Avi Kumar
  • 4,403
  • 8
  • 36
  • 67
  • 1
    The difference was a disparity between the Application context and the Activity context. Future readers of this post, see [this answer](http://stackoverflow.com/a/4343666/1210260) for more information on why this was the accepted answer. – gobernador Apr 05 '12 at 02:28
1

Try this, it worked for me

         AlertDialog.Builder alert = new AlertDialog.Builder(this);
         alert.setTitle("Title");
         alert.setMessage("Message");
        // Set an EditText view to get user input 
       final EditText input = new EditText(this);
       alert.setView(input);
       alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int whichButton) {
                  String value = input.getText();
                 // Do something with value!
       }
       });

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

     alert.show();
deepa
  • 2,496
  • 1
  • 26
  • 43
1

You can use custom dialog instead of AlertDialog like this

Dialog dilog=new Dialog(this);
dilog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dilog.setContentView(R.layout.yourlayout_in_xml);

and refer to its field

EditText youredittext=(EditText)dilog.findViewById(R.id.youredittext);
String textGet = youredittext.getText().toString(); // u will get ur inserted stting here

dilog.show() to show it
Lucifer
  • 29,392
  • 25
  • 90
  • 143
Nitin
  • 1,966
  • 4
  • 22
  • 53
0

You need to place your custom view within the dialog. You can either create the view dynamically or inflate it from XML. The method you will want to use instead of setTitle, setNegativeButton, etc is setView.

Phil
  • 35,852
  • 23
  • 123
  • 164