I am using Search Widget to implement search interface in my app in action bar. As soon as the activity starts the keyboard pops up even if I have not interacted with the search widget. So for that I added the attribute in the activity tag in manifest file windowSoftInputMode giving value either stateHidden or stateAlwaysHidden but this did not worked. So I tried to do it programmatically using the hideSoftInputFromWindow method but this also did not worked.
What does this causes to pop up the keyboard even after the attributes are set not to show and how to solve this issue? Please help!
Try to Use this functions to show OR hide the keyboard:
/**
* Hides the soft keyboard
*/
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
0);
}
}
/**
* Shows the soft keyboard
*/
public void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}
Just set focus as false at first time when that screen loaded. By default when screen loaded, the focus goes to first edittext and Keyboard appears.set like this
view.setFocusable(false);
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
and on clickListener of that view make that view as focused. by using
view.requestFocus();
You can try to set the focus on any other view of the activity like this:
view.requestFocus();
I spotted the problem and solved the issue. I was using android.widget.SearchView and changed it to android.support.v7.widget.SearchView, after which I didn't even need to use windowSoftInputMode in the manifest or the hideSoftInputFromWindow() method. Please note!
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);
}
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
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.
I have an Edittext with android:windowSoftInputMode="stateVisible" in Manifest. Now the keyboard will be shown when I start the activity. How to hide it? I cannot use android:windowSoftInputMode="stateHidden because when keyboard is visible then minimize the app and resume it the keyboard should be visible.
I tried with
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
but it did not work.
In the AndroidManifest.xml:
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
or try
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Please check this also
Use the following functions to show/hide the keyboard:
/**
* Hides the soft keyboard
*/
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
/**
* Shows the soft keyboard
*/
public void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}
Just add two attributes to the parent view of editText.
android:focusable="true"
android:focusableInTouchMode="true"
Put this in the manifest inside the Activity tag
android:windowSoftInputMode="stateHidden"
Try this:
<activity
...
android:windowSoftInputMode="stateHidden|adjustResize"
...
>
Look at this one for more details.
To hide the softkeyboard at the time of New Activity start or onCreate(),onStart() etc. you can use the code below:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Using AndroidManifest.xml
<activity android:name=".YourActivityName"
android:windowSoftInputMode="stateHidden"
/>
Using Java
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
using the above solution keyboard hide but edittext from taking focus when activiy is created, but grab it when you touch them using:
add in your EditText
<EditText
android:focusable="false" />
also add listener of your EditText
youredittext.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
return false;
}});
Add the following text to your xml file.
<!--Dummy layout that gain focus -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >
</LinearLayout>
If you don't want to use xml, make a Kotlin Extension to hide keyboard
// In onResume, call this
myView.hideKeyboard()
fun View.hideKeyboard() {
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}
Alternatives based on use case:
fun Fragment.hideKeyboard() {
view?.let { activity?.hideKeyboard(it) }
}
fun Activity.hideKeyboard() {
// Calls Context.hideKeyboard
hideKeyboard(currentFocus ?: View(this))
}
fun Context.hideKeyboard(view: View) {
view.hideKeyboard()
}
How to show soft keyboard
fun Context.showKeyboard() { // Or View.showKeyboard()
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInput(SHOW_FORCED, HIDE_IMPLICIT_ONLY)
}
Simpler method when simultaneously requesting focus on an edittext
myEdittext.focus()
fun View.focus() {
requestFocus()
showKeyboard()
}
Bonus simplification:
Remove requirement for ever using getSystemService: Splitties Library
// Simplifies above solution to just
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
I hope this will work, I tried a lot of methods but this one worked for me in fragments. just put this line in onCreateview/init.
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
add in your activity in manifasts
this property
android:windowSoftInputMode="stateHidden"
Put this code your java file and pass the argument for object on edittext,
private void setHideSoftKeyboard(EditText editText){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
To hide the softkeyboard at the time of New Activity start or onCreate(),onStart() method etc. use the code below:
getActivity().getWindow().setSoftInputMode(WindowManager.
LayoutParams.SOFT_INPUT_STATE_HIDDEN);
To hide softkeyboard at the time of Button is click in activity:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Use SOFT_INPUT_STATE_ALWAYS_HIDDEN instead of SOFT_INPUT_STATE_HIDDEN
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Above answers are also correct. I just want to give a brief that there's two ways to hide the keyboard when starting the activity, from manifest.xml.
eg:
<activity
..........
android:windowSoftInputMode="stateHidden"
..........
/>
The above way always hide it when entering the activity.
or
<activity
..........
android:windowSoftInputMode="stateUnchanged"
..........
/>
This one says don't change it (e.g. don't show it if it isn't already shown, but if it was open when entering the activity, leave it open).
You can set config on AndroidManifest.xml
Example:
<activity
android:name="Activity"
android:configChanges="orientation|keyboardHidden"
android:theme="#*android:style/Theme.NoTitleBar"
android:launchMode="singleTop"
android:windowSoftInputMode="stateHidden"/>
Use the following code to Hide the softkeyboard first time when you start the Activity
getActivity().getWindow().setSoftInputMode(WindowManager.
LayoutParams.SOFT_INPUT_STATE_HIDDEN);
This is what I did:
yourEditText.setCursorVisible(false); //This code is used when you do not want the cursor to be visible at startup
yourEditText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event); // handle the event first
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0); // hide the soft keyboard
yourEditText.setCursorVisible(true); //This is to display cursor when upon onTouch of Edittext
}
return true;
}
});
Try this one also
Ed_Cat_Search = (EditText) findViewById(R.id.editText_Searc_Categories);
Ed_Cat_Search.setInputType(InputType.TYPE_NULL);
Ed_Cat_Search.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Ed_Cat_Search.setInputType(InputType.TYPE_CLASS_TEXT);
Ed_Cat_Search.onTouchEvent(event); // call native handler
return true; // consume touch even
}
});
If your application is targeting Android API Level 21 or more than there is a default method available.
editTextObj.setShowSoftInputOnFocus(false);
Make sure you have set below code in EditText XML tag.
<EditText
....
android:enabled="true"
android:focusable="true" />
Try this.
First in your searchable xml the fields (name and hint etc) put #string and not literal strings.
Then method onCreateOptionsMenu, it must have a ComponentName object with your package name and your completed class name (with package name) - In case activity which has the SearchView component is the same as the show search results use getComponentName(), as the google android developer says.
I tried a lot of solutions and after much,much work this solution works for me.
Ed_Cat_Search = (EditText) findViewById(R.id.editText_Searc_Categories);
Ed_Cat_Search.setInputType(InputType.TYPE_NULL);
Ed_Cat_Search.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Ed_Cat_Search.setInputType(InputType.TYPE_CLASS_TEXT);
Ed_Cat_Search.onTouchEvent(event); // call native handler
return true; // consume touch even
}
});
this one worked for me
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
it will works