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.
Related
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.
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 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.
;)
I want to disable keyboard pop up in my activity. There will not be any keboard in the actvity. but hardware keyboard must work.how to do this? hardware buttons must work
Just try below code.
InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(editView.getWindowToken(), 0);
Use this in your manifest:
<activity android:name=".SampleActivity" android:windowSoftInputMode="stateAlwaysHidden"/>
Managing soft keyboard in android is not that easy, although you can hide keyboard programatically.
private void hideKeyboard() {
//This will get any view in focus.
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
note that Hardware Keyboard will work.