5

I would like an AlertDialog with an EditText field in it to accept input. This in itself is pretty straight-forward. However there are a few "obvious" features that I would like as fallout from this request. I'll take them one-by-one. I am looking for anyone that has a simpler solution to these things. I am guessing the solution is, "Write your own custom dialog you lazy bum."

AlertDialog with an EditText

final EditText input = new EditText(context);
final AlertDialog dlg = new AlertDialog.Builder(this).
    setTitle("Title").
    setView(input).
    setCancelable(false).
    setPositiveButton(android.R.string.ok, new OnClickListener()
    {
        @Override
        public void onClick(final DialogInterface dialog, final int which)
        {
            /* Handle ok clicked */
            dialog.dismiss();
        }
    }).
    setNegativeButton(android.R.string.cancel, new OnClickListener()
    {
        @Override
        public void onClick(final DialogInterface dialog, final int which)
        {
            /* Handle cancel clicked */
            dialog.dismiss();
        }
    }).create();
dlg.show();

Yay, works great. It'd sure be nice if that input field got focused right away (and show the keyboard), right?

AlertDialog with focused EditText The following code would be after create() and before dlg.show()

/** This requires API Level 8 or greater. */
dlg.setOnShowListener(new OnShowListener()
{
    @Override
    public void onShow(final DialogInterface dialog)
    {
        input.requestFocus();
        ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(input, 0);
    }
});

Nice job... I guess. Now that I have this focused input it'd be nice if it respected the inputs IME option...

AlertDialog with focused EditText with a custom IME Option

input.setImeOptions(EditorInfo.IME_ACTION_DONE);
input.setOnEditorActionListener(new OnEditorActionListener()
{
    @Override
    public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event)
    {
        /** Same code here that goes in the dialog.setPositiveButton OnClickListener */
        dlg.dismiss();
        return true;
    }
});

Now that's really not a great solution (repeated code) but it works...

Do people have a better way of solving this problem, or is it really that rare to ask a user for a small piece of information in a dialog, or am I just a winer and should go write my own dialog?

xbakesx
  • 13,202
  • 6
  • 48
  • 76
  • Why not just move all the repeated code into a method like `setUpImeDialog(AlertDialog dlg, EditText input) { ... }`? – Cat Sep 05 '12 at 20:33
  • @Eric Clearly you would. Still less than ideal that you have to listen for a done (or whatever) IME event AND a click ok request. – xbakesx Sep 05 '12 at 20:35
  • 1
    Yes, but you can have the IME event trigger the click. Like so: `dlg.getButton(DialogInterface.BUTTON_POSITIVE).performClick();`. It's still a secondary event, but it avoids repeated code. (If this is what you're looking for, I can add it as an answer.) – Cat Sep 05 '12 at 20:39
  • @Eric that certainly makes it better. Add it as an answer to the repeated code issue. Hopefully it helps people out! – xbakesx Sep 05 '12 at 20:42

2 Answers2

7

As per comments on the OP:

You do not have to have such repeated code in the OnEditorActionListener. Instead of repeating the code, you can tell the OS to click the "Ok" button when it is activated.

Something like this:

public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
    dlg.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); // Click OK button
    return true;
}

Overall I would say you are taking the right approach (documentation about collecting information through dialogs). As I mentioned in a comment, the OS uses an AlertDialog w/ EditText for adding dictionary words (to the user dictionary), so this is an expected functionality in the OS.

Cat
  • 66,919
  • 24
  • 133
  • 141
2

You can always switch to an Activity with Theme.Dialog theme or a DialogFragment which gives you a lot more freedom in tuning your widgets. AlertDialogs are probably better for displaying information. Hope this helps.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • Actually I would disagree. [The documentation](http://developer.android.com/guide/topics/ui/dialogs.html) shows ways to have it collecting information rather than displaying it. Also, an example baked into the OS: adding dictionary words. – Cat Sep 05 '12 at 20:42
  • @Egor I think before anyone goes and creates a new Activity, they should consider creating a Dialog with their own layout and do what they want that way. – xbakesx Sep 05 '12 at 20:45