I try to hide the keyboard from within a fragment. The fragment is inside a viewpager.
I use this code, which works fine when running on Android emulator.
View view = getActivity().getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
On a huawei mate 20 pro device view is always null.
How can I get this working on every device?
I solved it by calling requestFocus() on the edittext before hiding the keyboard.
getView().findViewById(R.id.notes_edittext).requestFocus();
View view = getActivity().getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Maybe its helpfull to someone else.
Related
In continuation of the answer given in
Android setError("error") not working in Textview
After applying the solution i.e. changing textview to focusable, A keyboard gets pop-up when that textview is clicked. How to hide that.
P.S. I tried onFocusChangeListener and onTouchListener
I want to know where to call this hide keyboard method as I tried this but it's not solving the issue;
mEndTimeView.setOnClickListener(v -> {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if(ViewUtils.isKeyboardShown(mEndTimeView.getRootView())){
imm.hideSoftInputFromWindow(mEndTimeView.getWindowToken(), 0);
}
showEndTimePicker();
});
public static void hideKeyboard(Activity activity) {
View view = activity.findViewById(android.R.id.content);
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Call this method in button click.
You need this method to hide Soft keyboard.
public void closeKeyboard() {
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
Call this method to hide/close soft keyboard.
I am working on a project with an AutoCompleteTextView. I've put some buttons to work as a keyboard the problem is when i click(focus) on the autocompletetextview
the soft keyboard appears i dont want it to appear at all i have tried use this .
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
but this seems only to work if i put it in an onclick (button) and it is used to hide the keyboard while i need to disable it completely...any ideas?
public static void hideKeyboard(Activity activity) {
View view = activity.findViewById(android.R.id.content);
if (view != null) {
InputMethodManager imm =
(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
You can call this method on button click Maybe this helps you.
I've implemented an app with a NavigationDrawer and some Fragments.
But everytime I change Fragment with the NavigationDrawer, the soft keyboard keeps popping up, even if there's no EditText on the screen.
How can I solve this?
Have you tried adding this to your manifest for the activity:
android:windowSoftInputMode="stateHidden"
Add this line of code in your activity
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
And try with adding to fragment also.
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Try This in your BaseActivity or Main Activity
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
This helped me out. I hope it works.
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 wants to hide keyboard of device. I have try this code but its not working for me, Please Suggest me some other codes.
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
I have also try for manifest file but its also not working
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
When this code in root view's onTouch, it should work. If you want to hide keyboard when activity opened, you can add android:windowSoftInputMode="stateHidden" to AndroidManifest.xml
EditText myEditText = (EditText) findViewById(R.id.myEditText);
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
use this code using inputmanager and hideSoftInputFromWindow
You can simply add LinearLayout(with visibility gone/invisible) or any layout,that has no child.
And redirect the focus to it when activity starts.
;)