Reputation: 559
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.
Upvotes: 16
Views: 21366
Reputation: 1559
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)
}
Upvotes: 2
Reputation: 4585
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
Upvotes: 6
Reputation: 199
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);
}
}
Upvotes: 1
Reputation: 9700
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);
Upvotes: 0
Reputation: 559
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);
}
}
Upvotes: 19
Reputation: 1283
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.
Upvotes: 18
Reputation: 6792
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.
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()
).
Upvotes: 0
Reputation: 2365
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);
Upvotes: 6
Reputation: 5145
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;
}
Upvotes: 1