Automatic keyboard display for EditText in AlertDialog.Builder
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("Finalise List")
.setMessage("Specify file name for saved list.")
.setView(textFileName)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(MainActivity.this,
textFileName.getText().toString(), Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
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("Finalise List")
.setMessage("Specify file name for saved list.")
.setView(textFileName)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(MainActivity.this,
textFileName.getText().toString(), Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
Same story as before.
Any help please? Would be much appreciated.
No comments:
Post a Comment