Default Focus And Keyboard To Edittext In Android Alertdialog
Solution 1:
Use the following code. It worked for me.
editText.setOnFocusChangeListener(newOnFocusChangeListener() {
@OverridepublicvoidonFocusChange(View v, boolean hasFocus) {
editText.post(newRunnable() {
@Overridepublicvoidrun() {
InputMethodManager inputMethodManager= (InputMethodManager) YourActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
editText.requestFocus();
Solution 2:
Hidden keyboard when programmatically setting focus on an EditText in an Android Dialog.
I had this problem as well and it was a pretty simple fix - here is my suggested solution. Although it worked on DialogFragments for me, I see no reason why it wouldn't work in your case.
Basically the soft keyboard isn't triggered because the view is being created programmatically. The actual fix was simply putting this line in the onCreateDialog method:
dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
From the Android documentation on DialogFragments:
If the user focuses on an EditText, the soft keyboard will automatically appear. In order to force this to happen with our programmatic focus, we call getDialog().getWindow().setSoftInputMode(). Note that many Window operations you might have done previously in a Dialog can still be done in a DialogFragment, but you have to call getDialog().getWindow() instead of just getWindow().
@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
//setup your dialog builder and inflate the view you want here
...
//Make sure your EditText has the focus when the dialog is displayed
edit.requestFocus();
//Create the dialog and save to a variable, so we can set the keyboard stateDialogdialog= builder.create();
//now, set to show the keyboard automatically
dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return dialog;
}
Solution 3:
in your XML layout
call
<requestFocus/>
inside your default EditText
<EditTextandroid:blabla.... ><requestFocus/></EditText>
Solution 4:
use a custom view if you need it more often
publicclassRequestFocusEditTextextendsAppCompatEditText {
private RequestFocusEditText self;
publicRequestFocusEditText(final Context context, AttributeSet attrs) {
super(context, attrs);
this.self = this;
setOnFocusChangeListener(newOnFocusChangeListener() {
@OverridepublicvoidonFocusChange(View v, boolean hasFocus) {
post(newRunnable() {
@Overridepublicvoidrun() {
InputMethodManagerinputMethodManager= (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(self, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
requestFocus();
}
}
Solution 5:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); For hiding keyboard use:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(),0);
or try below code but you must set the requestFocus() or to your edittext
editText.setOnFocusChangeListener(newView.OnFocusChangeListener() {
@OverridepublicvoidonFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
Post a Comment for "Default Focus And Keyboard To Edittext In Android Alertdialog"