Displaying soft keyboard whenever AlertDialog.Builder object is opened
Solution 1
With the encouragement of Mur Votema (see his answer above) I have answered my question by building a custom dialog based on the Dialog class. Unlike an alert based on AlertDialog.Builder such a custom dialog does accept the getWindow().setSoftInputMode(...) command and therefore allows the soft keyboard to be displayed automatically.
For guidance on building a custom dialog I found this web page and this especially helpful.
Solution 2
As long as you always need to show the keyboard immediately once the dialog opens rather than once a specific form widget inside gets focus (for instance, if your dialog just shows an EditText
and a button), you can do the following:
AlertDialog alertToShow = alert.create();
alertToShow.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
alertToShow.show();
Rather than calling .show()
on your builder immediately, you can instead call .create()
which allows you to do some extra processing on it before you display it onto the screen.
Solution 3
This is in response to miannelle.
The following method is called when a menu option is selected:
private void addNote() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.textentryalertdialog);
dialog.setTitle("Add note");
TextView msgText = (TextView) dialog.findViewById(R.id.messagetext);
msgText.setText("Whatever prompt you want");
final EditText inputLine = (EditText) dialog.findViewById(R.id.my_edittext);
Button okButton = (Button) dialog.findViewById(R.id.OKButton);
okButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
dialog.dismiss();
// app specific code
}
});
Button cancelButton = (Button) dialog.findViewById(R.id.CancelButton);
cancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
dialog.dismiss();
}
});
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();
}
The textentryalertdialog.xml file defines a linear layout containing
TextView android:id="@+id/messagetext" ...
EditText android:id="@+id/my_edittext" ...
Button android:id="@+id/OKButton" ...
Button android:id="@+id/CancelButton" ...
I hope this helps.
Solution 4
If you want to pop up dialog box along with soft key pad, so that user could free from tap on edit text inside dialog to show keypad, for example if you are going to take some value from dialog box, then use this simple code, it will solve your problem.
public void onClick(final View v)
{
AlertDialog.Builder alert = new AlertDialog.Builder(v.getContext());
alert.setIcon(R.drawable.smsicon);
alert.setTitle(" Secrete Code");
alert.setMessage("Enter a Key for secrete text !");
final EditText shft_val = new EditText(v.getContext());
shft_val.setInputType(InputType.TYPE_CLASS_NUMBER);//changing the keyBoard to No only,restrict the string etc
alert.setView(shft_val);
//pOp Up the key pad on Edit Text focus event
shft_val.setOnFocusChangeListener(new OnFocusChangeListener()
{
public void onFocusChange(View arg0, boolean arg1)
{ InputMethodManager inputMgr = (InputMethodManager)v.getContext().
getSystemService(Context.INPUT_METHOD_SERVICE);
inputMgr.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
}
});
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
//Your specific code...
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
return;
}
});
alert.show();
}
Solution 5
Have you tried to set focus on your EditText -> inputBox.requestFocus() or something like that?
prepbgg
Updated on April 14, 2020Comments
-
prepbgg about 4 years
My code for opening an input dialog reads as follows:
final AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("Dialog Title"); alert.setMessage("Request information"); LayoutInflater factory = LayoutInflater.from(this); final View textEntryView = factory.inflate(R.layout.edittextautotextlayout, null); final EditText inputBox = (EditText) textEntryView.findViewById(R.id.my_et_layout); alert.setView(inputBox);
This works fine except that I have to tap the text entry line before the soft keyboard appears.
Following the advice given here I have tried inserting:
inputBox.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { alert.getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); } } });
but Eclipse objects that "the method getWindow() is not defined for the type AlertDialog.Builder".
It seems that the setOnFocusChangeListener code works for an AlertDialog object but not an AlertDialog.Builder. How should I modify my code to make the soft keyboard appear automatcially.
-
prepbgg over 13 yearsThanks for the suggestion. However, when I try that Eclipse says "the method getWindow() is undefined for type EditText"
-
prepbgg over 13 yearsThanks for the suggestion. I tried that, but unfortunately I got the same error message ("the method getWindow() is undefined for the type View")
-
prepbgg over 13 yearsThanks for the suggestion. I tried inserting inputBox.requestFocus(); after alert.setView(inputBox);, but no joy!
-
Tima over 13 years... and if you try to call requestFocus after alert.create() ? Because first dialog should be created and then edit can get focus, no?!
-
prepbgg over 13 yearsMy code doesn't explicitly call create(). Presumably it is called indirectly by the statement "AlertDialog.Builder alert = new AlertDialog.Builder(this);" which definitely comes before any other reference to alert.
-
prepbgg over 13 yearsYes, the code shown in my original question continues "alert.setPositiveButton(...); alert.setNegativeButton(...); alert.show();". Following this with "inputBox.requestFocus()" does nothing. Trying "inputBox.setPressed(true);" just highlights the text box in green. I can't see any other relevant methods to try.
-
Tima over 13 yearsi'm not sure, but probably it's not possible to get access to a view, which was added to builder (android.git.kernel.org/?p=platform/frameworks/…) ....... why don't you try to use custom dialogs then?! another idea to create your own alertdialog.builder extending original alertdialog.builder and having member view in it.
-
prepbgg over 13 yearsI'm afraid that would be above my pay grade! (I think Android needs something like Visual Basic to make things easier for amateurs like me ... I've even found HTML+DHTML+Javascript much easier to use (and amazingly powerful.))
-
Tima over 13 yearsI had had the same thoughts on this weekend. But ... maybe this link will help you to create a custom dialog developer.android.com/guide/topics/ui/dialogs.html#CustomDialog it's not so difficult. Wish you much luck
-
prepbgg over 13 yearsThanks for your encouragement. I'll have a look at your suggestion and report back.
-
prepbgg over 13 yearsI've read the section on Custom Dialogs in the page you referred me to. After describing how to build a custom dialog based on the Dialog class, this goes on to discuss customising a dialog based on AlertDialog.Builder (much as I have already done) and says "using an AlertDialog for your custom layout lets you take advantage of built-in AlertDialog features like managed buttons". This suggests that unless I use AlertDialog.Builder I won't get "Ok" and "Cancel" buttons. So I am flummoxed as to which way to proceed!
-
prepbgg over 13 yearsI see that the "Creating A Custom Dialog" notes say that "an AlertDialog is created easiest with the AlertDialog.Builder class". This suggests that perhaps you don't have to use AlertDialog.Builder. I wonder if there is a tutorial anywhere on how to build an AlertDialog object without using AlertDialog.Builder. As you can probably tell, I'm floundering here.
-
prepbgg over 13 yearsWith the help of a couple of tutorials (helloandroid.com/tutorials/… and blog.androgames.net/10/custom-android-dialog) I've created a custom Dialog object which emulates the default AlertDialog.Builder object well (so far as I can tell) and which (unlike an AlertDialog.Builder object) accepts the getWindow().setSoftInputMode(...) command. Bingo! It wasn't so difficult, I suppose, but it's an extraordinarily fiddly thing to have to do just to allow the automatic display of the soft keyboard.
-
prepbgg over 13 yearsThanks again to you and everyone else for your help. :)
-
Xiao about 12 yearsCode does not work on device which have a real keyboard (like Moto Droid). Anyone knows how to deal with that?
-
Defragged over 9 years@Johnny This is the code to display the alert, so place it wherever you determine you need to show the alert. It's probably replacing an existing call to
alert.show()
. -
Georg about 9 yearsis shows the keyboard, but the focus is not in my
EditText
. Any ideas? -
sberezin over 8 yearsTHE REAL PROBLEM IS that when you call to setSoftInputMode() before alert show() it layouts differently (it's strange...) - my custom views get ugly overlapped with Ok/Cancel buttons. So CALL setSoftInputMode() AFTER show()!!!
-
Ajji over 7 yearsBrilliant!! Thank you!
-
ingyesid over 6 yearsThanks! it works!, for my case my dialog have a recyclerview with items(with edittext) and i was trying with alertdialog with no success. y changed the alertdialog by a dialog and now the keyboard popups.
-
Jose Ricardo Citerio Alcala about 5 yearsYeah, it's working great in Kotlin. Sheet man, you are awesome.