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.
-
1http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard?rq=1 – Sardor Dushamov Mar 12 '14 at 06:08
-
go to this :[http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard?rq=1](http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard?rq=1) – M D Mar 12 '14 at 06:09
-
show what you have tried and whats not working. – Sahil Mahajan Mj Mar 12 '14 at 06:42
-
Best answer: https://stackoverflow.com/a/17393446/1164529 – Harpreet Feb 04 '21 at 09:32
9 Answers
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);
}
}

- 9,664
- 4
- 30
- 41

- 549
- 1
- 6
- 16
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.

- 1,263
- 2
- 17
- 27
Use following code to hide keyboard
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Use following code to show keyboard
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

- 2,345
- 1
- 14
- 31
Use this function:
public void hideKeyboard() {
if (getDialog().getCurrentFocus() != null) {
InputMethodManager inputManager = (InputMethodManager) Objects.requireNonNull(getActivity()).getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getDialog().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
I wish it be useful

- 3,184
- 3
- 41
- 71
Here is a solution:
private fun showCustomDialog() {
// Normal dialog stuff
// -----
val builder = AlertDialog.Builder(activity as Context)
val customLayout = View.inflate(context, R.layout.dialog_layout, null)
val editText: EditText = customLayout.findViewById(R.id.edit_text)
// -----
// Get a hold of the inpoutMethodManager here
val imm = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
builder.setTitle(getText(R.string.dialog_title))
builder.setView(customLayout)
builder.setPositiveButton(getText(R.string.action_confirm)) { _, _ ->
// Hide the soft keyboard here after the positive button onclick
imm.hideSoftInputFromWindow(editText.windowToken, 0)
/*
* Do your logic here for the positive click
* ....
*/
}
builder.setNegativeButton(getText(R.string.action_cancel)) { dialog, _ ->
// Also hide the soft keyboard if the user clicked negative button
imm.hideSoftInputFromWindow(editText.windowToken, 0)
/*
* Do your logic here for the negative click
* ....
*/
dialog.cancel()
}
// added not cancelable for the dialog since it might mess with the keyboard hiding
builder.setCancelable(false)
builder.show()
// make sure you call this to request focus, or else the hiding might not work...
editText.requestFocus()
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
}

- 1,265
- 13
- 12
Use this in your activity
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
View v = getCurrentFocus();
boolean ret = super.dispatchTouchEvent(event);
if (v instanceof EditText) {
View w = getCurrentFocus();
int scrcoords[] = new int[2];
w.getLocationOnScreen(scrcoords);
float x = event.getRawX() + w.getLeft() - scrcoords[0];
float y = event.getRawY() + w.getTop() - scrcoords[1];
if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()) ) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
}
}
return ret;
}

- 5,135
- 2
- 24
- 49
-
-
basically is it ontouch method override and in this i put code for dismiss soft keyboard – Bhanu Sharma Mar 12 '14 at 06:28
-
just paste this as it is in your activity class its a @Override methos – Bhanu Sharma Mar 12 '14 at 06:29
Use it.
protected void hideKeyboardDialog(Dialog dialog){
View view = dialog.getView();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}

- 8,057
- 8
- 35
- 54

- 201
- 3
- 6
Show the KeyBoard
when you show the Dialog
and close the KeyBoard
when you press OK
button as below...
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext());
alertDialogBuilder.setTitle(getString(R.string.app_error) + ":" + errorCode);
alertDialogBuilder.setMessage("Message");
alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0); }
});
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

- 9,664
- 4
- 30
- 41
- When Dialog shows I want to show keyboard with request focus for edit text name.
This is easy as you yourself answered it. Add <requestfocus/>
to your EditText via xml or editText.requestFocus();
via code before you show the dialog.
- After clicking of OK button Soft Keyboard should get hide.
This can be achieved in two ways, depending upon what you are doing on click of your OK button.
a. If you are starting a new Activity - Add android:windowSoftInputMode="stateHidden"
to this activity in the Manifest, so everytime the activity starts keyboard will be hidden unless you call it.
b. If you are on the same page - call the below method.
private void hideSoftKeyBoard() {
try {
// hides the soft keyboard when the drawer opens
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
} catch (Exception e) {
e.printStackTrace();
}
}
In case getCurrentFocus().getWindowToken() gives error then pass any View to it (you can track this via the try catch block) where View could be anything, a Button, EditText etc of your Activity (myButton..getWindowToken()
).

- 6,692
- 4
- 39
- 74