I have a dialog includes edittext, when user tabs the dialog and not on the keyboard or on the edittext, dialog will dismiss and so does softkeyboard, what does system deal with this action? It can dismiss the keyboard sometimes and others does not, I am confused about it. Thank you!
use
public void hideKeyboard(){
Activity activity = (Activity) mContext;
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
add
softInputmode:statehidden inside of manifest activity tag
Related
fun Activity.hideKeyboard() {
if (currentFocus != null) {
val inputMethodManager =
getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(currentFocus!!.windowToken, 0)
}
}
I use this function to hide keyboard when user press enter in keyboard. But the screen often goes dimmed. I also use this code in other screen but the screen not goes dimmed. How to prevent it?
https://youtu.be/wRZtWohEac4
This is the video. When i press enter, keyboard gone and background color got dimmed. Then i click the edit text again, and screen back to bright
I can't reproduce your problem without your activity/fragment code so these are just some guesses
Try this code for hiding your keyboard:
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
if (imm != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
After hiding the keyboard, try requesting focus for your EditText:
edittext.requestFocus()
We are trying to emulate user click to gain back focus to EditText to prevent screen dimming.
Also try calling this function after a little delay (using handler). sometimes that does the job.
If this doesn't resolve the issue then something is probably wrong in your activity/fragment and you must share your activity code.
I have a edittext in my android app where I can put a person birthdate. When I click on it a dialog fragment appears, but I have to click two times. After first click there is an keyboard and I don't want it.
I know that I can use button instead, but i prefer editext, because it looks better :)
How can I change it? Can I open diagram in first edittext click? Thanks in advance!
You can make your EditText not focusable by adding android:focusable="false" in XML declaration and then use simple onClickListener on the EditText.
Else, you can also use the following method to hide the keyboard:
public void hideSoftKeyBoard(Activity activity) {
// Check if no view has focus:
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
This can be invoked as follows:
hideSoftKeyBoard(this) from an activity or hideSoftKeyBoard(getActivity()) from a fragment.
Hope this helps!
I have a fragment which i use to show map. From this fragment I am opening another dialog fragment which have an editText. On clicking editText the keyboard opens but when I dismiss the dialogFragment without first closing the keyboard, the dialogFragment closes as it should but the keyboard remains open. and after again touching anywhere the keyboard closes. How do I close the keyboard on dismissing the dialogFragment.
I have already tried :
android:windowSoftInputMode="stateAlwaysHidden" in activity.
also tried :
InputMethodManager imm =
(InputMethodManager) messageEditTxt.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive())
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
in onDismiss function.
Try this
public static void hideSoftKeyboard(Context context, View view) {
try {
InputMethodManager inputMethodManager =
(InputMethodManager) context.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
view.getWindowToken(), 0);
} catch (Exception e) {
e.printStackTrace();
}
}
Usage
hideSoftKeyboard(getActivity(), getView())
I had the same problem and found that it is related to the windowSoftInput defined in the manifest. After removing android:windowSoftInputMode="stateHidden" from the activity, it worked.
Just place this in your global class and you can access anywhere
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
for Activity-:
GlobalClass.hideSoftKeyborad(MainActivity.this);
for fragments-
GlobalClass.hideSoftKeyborad(getActivity);
I have multiple edit text and button on screen, on every button click there are some validation if validation is successful then i have to hide keyboard. I have tried so many code but nothing work.
Currently i am using,
InputMethodManager imm = (InputMethodManager) cntx.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
This is toggle, it opens if keyboard is hidden, but i wants to hide keyboard whether it is open or not.
Make one function like this, and Call from anywhere by passing context
like this :
public static void hideKeyBoard_WithView(Context context) {
// Check if no view has focus:
View view = ((Activity) context).getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
Hope it will help you ! :) just try !
edittext.setFocusable(true);
edittext.setFocusableInTouchMode(true);
//try using this.. make the parameter as false when u dont required the keyboard & write the onclick method on edit text & make parameters true to get the focus again
Get current focussed view and using that view window token hide your keyboard.
View view = this.getCurrentFocus(); // get current focussed edittext
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
I have an activity which opens up a dialog that contains edittexts on a button click. As soon as the dialog opens the softkeyboard shows. I want to prevent this. Only when I click on the edittext in the dialog should the softkeyboard appear. I am using Android 4.
Thanks in advance
To achieve this, set the soft input mode just before you call dialog.Show():
// Build and create your dialog.
AlertDialog dialog = new AlertDialogBuilder(...).create();
// Hide soft keypad by default (will be displayed when user touches any edit field).
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
// Show the dialog.
dialog.show();
This should work is any case:
public void hideKeyboard() {
mActivity.runOnUiThread(new Runnable() {
public void run() {
InputMethodManager inputManager = (InputMethodManager) mActivity
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(mActivity
.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
});
}
adding this to your Manifest is all you hav to do:
android:windowSoftInputMode="stateHidden"
Simple, but in case someone was looking here
:)