2

Apologies for asking a question which seems to have been asked and answered before but none of the solutions I've found seem to work for me. I'm creating an AlertDialog with an EditText to obtain a string from the user. When this dialog is shown, there is no soft keyboard visible and only once the user taps on the EditText does the keyboard pop up. How can I get the EditText to automatically have focus and the keyboard to automatically show the moment the dialog is shown?

Here is my code for creating and showing the dialog. All of this is inside an OnClickListener for a button on the main activity.

@Override
public void onClick(View v) {
    final EditText textFileName = new EditText(MainActivity.this);
    textFileName.setRawInputType(Configuration.KEYBOARD_QWERTY);
    textFileName.setInputType(InputType.TYPE_CLASS_TEXT);

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
    .setTitle("Save Data")
    .setMessage("Specify file name for saved data.")
    .setView(textFileName)
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Do the file saving bit here
        }
    })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Do whatever you feel is important here
        }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
}

Now I did search around for the solution to this problem and found two answers, this one here and another one here. Both of these seemed to have satisfied the respective original posters but neither of them work for me and I just can't figure out what it is that I'm doing wrong.

Here's my first attempt, based on the code above and the top voted answer posted in the first link.

@Override
public void onClick(View v) {
    final EditText textFileName = new EditText(MainActivity.this);
    textFileName.setRawInputType(Configuration.KEYBOARD_QWERTY);
    textFileName.setInputType(InputType.TYPE_CLASS_TEXT);

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
    .setTitle("Save Data")
    .setMessage("Specify file name for saved data.")
    .setView(textFileName)
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Do the file saving bit here
        }
    })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Do whatever you feel is important here
        }
    });
    AlertDialog dialog = builder.create();
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    dialog.show();
}

It's essentially one line of code added just before the dialog.show() call but it changes nothing. The dialog still pops up with the most beautiful EditText but no keyboard, until I tap on the EditText.

And here is attempt two, based on the top voted answer in the second link.

@Override
public void onClick(View v) {
    final EditText textFileName = new EditText(MainActivity.this);
    textFileName.setRawInputType(Configuration.KEYBOARD_QWERTY);
    textFileName.setInputType(InputType.TYPE_CLASS_TEXT);
    textFileName.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            textFileName.post(new Runnable() {
                @Override
                public void run() {
                    InputMethodManager inputMethodManager = (InputMethodManager)MainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.showSoftInput(textFileName, InputMethodManager.SHOW_IMPLICIT);
                }
            });
        }
    });
    textFileName.requestFocus();

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
    .setTitle("Save Data")
    .setMessage("Specify file name for saved data.")
    .setView(textFileName)
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Do the file saving bit here
        }
    })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Do whatever you feel is important here
        }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
}

Same story as before.

Any help please? Would be much appreciated.

Community
  • 1
  • 1
Dewald Swanepoel
  • 1,651
  • 4
  • 15
  • 38

1 Answers1

1

try this, it helps me:

editText.postDelayed(new Runnable() {
    @Override
    public void run() {
        InputMethodManager keyboard = (InputMethodManager) activity
            .getSystemService(Context.INPUT_METHOD_SERVICE);
        keyboard.showSoftInput(editText, 0);
    }
}, 50);
WOLVERINE
  • 769
  • 3
  • 12
  • 28
  • I get a "Cannot make a static reference to the non-static method getSystemService(String) from the type" error on that **.getSystemService** call. No idea why because none of my classes or functions are static but so the above does not work. – Dewald Swanepoel Aug 21 '13 at 05:13
  • Unfortunately I can't fit all of it into the 600 chars allowed for a comment but, if you look at my original code in the question, I've added this section immediately below the line that reads **textFileName.setInputType(InputType.TYPE_CLASS_TEXT);** textFileName.postDelayed(new Runnable() { @Override public void run() { InputMethodManager keyboard = (InputMethodManager)MainActivity.getSystemService(Context.INPUT_METHOD_SERVICE); keyboard.showSoftInput(editText, 0); } }, 50); And I get the error on the **MainActivity.getSystemService(Context.INPUT_METHOD_SERVICE)** bit. – Dewald Swanepoel Aug 21 '13 at 06:23
  • Sorry for the ugly code formatting but I don't know how to tidy it up in comments. Just a further question on your suggestion: What exactly is meant by the **activity** keyword? I assumed it refers to the current activity which is called **MainActivity** in my case so you will notice that I've replaced all occurrences of **editText** in your code with **textFileName** in mine and **activity** with **MainActivity**. This may well be where I'm getting it wrong? – Dewald Swanepoel Aug 21 '13 at 06:31
  • yes, i send application activity to my adapter, and then use only that code, and it work perfect: final EditText editbet = (EditText) dialog.findViewById(R.id.dialogEditText); editbet.requestFocus(); editbet.post(new Runnable() { @Override public void run() { InputMethodManager keyboard = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); keyboard.showSoftInput(editbet, 0); } }); – WOLVERINE Aug 21 '13 at 07:23
  • Good heavens, I can't believe how convoluted Android development is! I can't be the only one who finds this super frustrating. You would think that displaying a text dialog is a fairly straight forward issue but it is quite the opposite. I'm having to implement listeners inside classes inside listeners inside classes. No surprise I'm having all these scope and static/non-static issues. I'm not sure I understand what you mean by "sending application activity to your adapter". Would you mind explaining that, and perhaps how that applies to my block of code at the very top? – Dewald Swanepoel Aug 21 '13 at 08:09