Suppress Soft Input (hide keyboard) for Edittext view - android

I'm trying to make a simple calculator and to do so, I want to, and exit view that the user can move the courser within but can only input based off of the buttons I've included.
When I press on the Edittext view, however, the keyboard pops up and I can't figure out how to suppress it - I've tried both android:windowSoftInputMode="stateAlwaysHidden" and android:configChanges="keyboardHidden" in the manifest and also
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
//Hide keyboard
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
In Java but none of them work

thanks for the help but I've just found a solution
XML:
<EditText
android:id="#+id/InputLine"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_above="#id/Sixth_Up"
android:onClick="hideKeyboard">
</EditText>
Java:
public void hideKeyboard(View v) {
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editInput.getWindowToken(),0);
}

You can check that view is in focus and then hide the keyboard.
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager manager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

Related

How to disable the 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.

Software Keyboard doesn't appear the first time the fragment is loaded

I've been trying to automatically show the keyboard with focus on a edit text when a fragment(that includes the edittext) is loaded.
I've tried using :
editTextPrice.requestFocus();
InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.showSoftInput(getView(), InputMethodManager.SHOW_IMPLICIT);
But this doesn't work the first time I load the fragment. The focus is set and I can see the cursor but no keyboard appears. If I close the fragment and add it again it works.
If tried everything from post() to a postpone it with a handler, to onResume,ect.
Does anybody have an idea what might cause this to happen?
Thanks in advance.
Best regards
let me explain why keyboard is not opening when we load a fragment the view cannot get focused, so we need set focus to the view in a separate thread, Then after we need to open keyBoard
void showKeyboard(Context mContext, View view)
{
if (mContext != null && view != null && view.requestFocus())
{
InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
This above function is to open keyboard
editTextPrice.postDelayed(() -> {
editTextPrice.requestFocus();
showKeyboard(getContext(), editTextPrice);
}, 300);
android:focusable="true"
android:focusableInTouchMode="true"
And
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
you try using 2 function below:
fun hideKeyBoard(context: Context, view: View) {
val inputMethodManager = context.getSystemService(
Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
fun showKeyBoard(context: Context, view: View) {
val inputMethodManager = context.getSystemService(
Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
Do not request focus from java code. Do it from the xml itself like
android:focusable="true"
also do the following in the manifest file
<activity android:name=".YourActivity"
android:windowSoftInputMode="stateAlwaysVisible" />

How to close keyboard on any click event?

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

Hiding soft keyboard without having focus

I am using a FrameLayout to show an EditText and a ListView (with checkboxes) alternately. When showing an EditText, I would like the soft keyboard to be shown. And when showing the ListView, I would like the soft keyboard to be hidden. Now usually a focus is needed in order to hide the soft keyboard. When my ListView gets shown, then getCurrentFocus() returns null. Is there a way to hide the soft keyboard, without having a focus?
I am showing the soft keyboard like that:
public static void requestFocusAndMoveCursorToTheEndAndShowKeyboard(final EditText editTextParam, final Activity activityParam) {
if (editTextParam == null) {
return;
}
if (editTextParam.requestFocus()) {
editTextParam.setSelection(editTextParam.getText().length()); // move Cursor to the end of the EditText
InputMethodManager imm = (InputMethodManager) activityParam.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
And I am trying to hide the soft keyboard like that:
public static void hideSoftInputKeyboardFromWindow(Activity activityParam) {
if (activityParam == null) {
return;
}
View view = activityParam.getCurrentFocus();
if (view != null) {
InputMethodManager inputMethodManager = (InputMethodManager) activityParam.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
Make use of .clearFocus(); on edittext when you don't want a focus on it.
In your AndroidMenifest.xml add this:
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
Try this:
Write following method to youractivity or to your utility class
/**
* Hide soft keypad
*
*/
public static void hideKeyboard(Activity activity, View v) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
In your hideSoftInputKeyboardFromWindow method, try:
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
instead of
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
Edit: ok same answer as Dorami
How do you show them alternately? Using different fragments? Or do you simply inflate different layouts? Provide more details with your complete code
Thank you for your answers. Finally I got it solved using a View.OnFocusChangeListener for the EditText, like described here:
Hide soft keyboard on losing focus

how to disable softkeyboard in an activity?

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.

Categories

Resources