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.
- 1stackoverflow.com/questions/1109022/…Sardor Dushamov– Sardor Dushamov2014-03-12 06:08:24 +00:00Commented Mar 12, 2014 at 6:08
- go to this :http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard?rq=1M D– M D2014-03-12 06:09:05 +00:00Commented Mar 12, 2014 at 6:09
- show what you have tried and whats not working.Sahil Mahajan Mj– Sahil Mahajan Mj2014-03-12 06:42:39 +00:00Commented Mar 12, 2014 at 6:42
- Best answer: stackoverflow.com/a/17393446/1164529Harpreet– Harpreet2021-02-04 09:32:12 +00:00Commented Feb 4, 2021 at 9: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); } } Comments
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.
Comments
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
1 Comment
getDialog().getCurrentFocus().getWindowToken() is what we need to hide keyboard from DialogFragmentHere 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) } Comments
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; } 3 Comments
- 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()).
Comments
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);