I'm trying to get a piece of code work which should focus an EditText in an AlertDialog as soon as it shows and then automatically open the soft keyboard. Instead, it just makes the screen go darker.
Builder builder = new Builder(this);
final EditText input = new EditText(this);
AlertDialog dialog = builder.create();
builder
.setTitle(R.string.dialog_title_addsubject)
.setMessage(R.string.dialog_addsubject)
.setView(input)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String value = input.getText().toString();
if (input.getText().toString().trim().length() == 0) {
Toast.makeText(Main.this, R.string.input_empty, Toast.LENGTH_SHORT).show();
} else {
db.insertSubject(value);
getData();
}
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
input.requestFocus();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();
I tried many ways of doing this but none worked. I hope you guys can help me here. Thanks in advance!
open keyborad in onShow :-
Ok I managed to get it working:
This method doesn't need a dialog so I can use builder.show() to show the dialog and the code provided by Sabre opens the soft keyboard. Another code snippet in each of the buttons closes the soft keyboard automatically.
Try showing it after a second -
You can use this instead of
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
:Call this after
dialog.show();