In my application custom dialog is in BaseExpandableListAdapter class. In dialog I have two edit text. First is name and its mandatory. And second is address its optional. And two buttons OK and cancel. When Dialog shows I want to show keyboard with request focus for edit text name. After clicking of OK button Soft Keyboard should get hide.
You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases, you will want to pass in InputMethodManager.
Ok everyone knows that to hide a keyboard you need to implement: InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);
on click of ok button write the below code:-
final Dialog dialog=new Dialog(this);
dialog.setContentView(R.layout.dialog);
final EditText text = (EditText) dialog.findViewById(R.id.nameField);
Button mOkBtn=(Button)dialog.findViewById(R.id.okBtn);
// if button is clicked, close the custom dialog
mOkBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager)getSystemService(context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(text.getWindowToken(), 0);
}
});
dialog.show();
Define context as Context context=this.
final Dialog dialog = new Dialog(_context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.prompts);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
final EditText name = (EditText) dialog.findViewById(R.id.name);
final EditText add = (EditText) dialog.findViewById(R.id.add);
Button btnok = (Button) dialog.findViewById(R.id.btn_ok);
Button btncancel = (Button) dialog.findViewById(R.id.btn_cancel);
btnAddExpList.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With