Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8482101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:44:17+00:00 2026-06-10T19:44:17+00:00

I would like an AlertDialog with an EditText field in it to accept input.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:44:18+00:00Added an answer on June 10, 2026 at 7:44 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to retrieve user input via an Alert Dialog... is there any
I would like to create an AlertDialog similar to this figure. I can't resize
I am validating an AlertDialog, and I would like to raise a Toast on
Here is my problem. I would like to have an AlertDialog that have a
I am creating a menu in Android and I would like this menu to
I have this code and would like it to appear on the page of
I'm creating an alertDialog to ask for a login data. I would like to
I would like to prompt the user to give me input in my android
case DIALOG_ID: AlertDialog.Builder builder = new AlertDialog.Builder(this); EditText edittext=(EditText)findViewById(R.id.name); Appendable name=edittext.getText(); builder.setMessage(name + Certificate
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.