How to disable the soft keyboard - android

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.

Related

How to Hide Soft Keyboard on android after clicking TextView (which is set to focusable)?

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.

Soft keyboard keeps popping up

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.

Android webview. Keyboard hide

When clicking on shows the keyboard. When you click on another area of the screen keyboard is hidden. Is it possible to prevent the hide keyboard when clicking on another area of the screen?
Try this for hide keyboard
public static void hideSoftKeyboard(Activity context) {
InputMethodManager inputManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager != null)
inputManager.hideSoftInputFromWindow(context.getWindow()
.getDecorView().getApplicationWindowToken(), 0);
context.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
Here is the code to hide the keyboard
public void hideKeyboard(View view)// pass your main view {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
}

How to hide keyboard in android

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.
;)

Close virtual keyboard on button press

I have an Activity with an EditText, a button and a ListView. The purpose is to type a search screen in the EditText, press the button and have the search results populate this list.
This is all working perfectly, but the virtual keyboard is behaving strange.
If I click the EditText, I get the virtual keyboard. If I click the "Done" button on the virtual keyboard, it goes away. However, if I click my search button before clicking "Done" on the virtual keyboard, the virtual keyboard stays and I can't get rid of it. Clicking the "Done" button does not close the keyboard. It changes the "Done" button from "Done" to an arrow and remains visible.
Thanks for your help
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
I put this right after the onClick(View v) event.
You need to import android.view.inputmethod.InputMethodManager;
The keyboard hides when you click the button.
mMyTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(m_txtSearchText.getWindowToken(),
InputMethodManager.RESULT_UNCHANGED_SHOWN);
return true;
}
return false;
}
});
Use Below Code
your_button_id.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
}
}
});
You should implement OnEditorActionListener for your EditView
public void performClickOnDone(EditView editView, final View button){
textView.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(EditView v, int actionId, KeyEvent event) {
hideKeyboard();
button.requestFocus();
button.performClick();
return true;
}
});
And you hide keyboard by:
public void hideKeybord(View view) {
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),
InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
You should also fire keyboard hiding in your button using onClickListener
Now clicking 'Done' on virtual keyboard and button will do the same - hide keyboard and perform click action.
For Activity,
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
For Fragments, use getActivity()
getActivity().getSystemService();
getActivity().getCurrentFocus();
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
In your case, as you only have one EditText, the bellow solution is the best one, I believe. If you had more than one EditText component, then either you would need to focus on the EditText that you wanted to close, or call the bellow function for every EditText. For you though, this works brilliantly:
editText.onEditorAction(EditorInfo.IME_ACTION_DONE);
Basically, the EditText already has a function that is intended to deal with what to do after the keyboard has been used. We just pass that we want to close the keyboard.
Add the following code inside your button click event:
InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
This solution works perfect for me:
private void showKeyboard(EditText editText) {
editText.requestFocus();
editText.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.RESULT_UNCHANGED_SHOWN);
editText.setSelection(editText.getText().length());
}
private void closeKeyboard() {
InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
Try this...
For Showing keyboard
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
For Hide keyboard
InputMethodManager inputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm =(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);enter code here}
Kotlin example:
val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
from Fragment:
inputMethodManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
from Activity:
inputMethodManager.hideSoftInputFromWindow(currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
You use this code in your button click event
// 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);
}
Crash Null Point Exception Fix:
I had a case where the keyboard might not open when the user clicks the button. You have to write an if statement to check that getCurrentFocus() isn't a null:
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(getCurrentFocus() != null) {
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
If you set android:singleLine="true", automatically the button hides the keyboard¡

Categories

Resources