How to hide Soft Keyboard when activity starts

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to hide Soft Keyboard when activity starts



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


android:windowSoftInputMode="stateVisible"


android:windowSoftInputMode="stateHidden



InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);


InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);



but it did not work.




18 Answers
18



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





Context.INPUT_METHOD_SERVICE for those who are in fragments or are not in the main activity etc.
– Oliver Dixon
Oct 16 '15 at 8:02







Doesn't work on Nexus 6P Android 6.0.1.
– GeneralKimi
Jan 19 '16 at 17:51





@GeneralKimi: I also like your problem
– Luan Dang
Jan 21 '16 at 10:22





You can try this. It works if you call it from the activity. getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)‌​;
– GeneralKimi
Jan 21 '16 at 14:25





not working in Cynogen 6.0.1
– Faisal Shaikh
Sep 18 '16 at 6:25



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





did you tried with getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
– Neenu
Sep 24 '13 at 10:47







Thanks for android:windowSoftInputMode="stateHidden"
– Shylendra Madda
Jan 9 '16 at 18:32


android:windowSoftInputMode="stateHidden"





Actually there is also great answer on preventing focusing on the edit text stackoverflow.com/questions/4668210/…
– Boris Treukhov
Jan 11 '16 at 10:31





This worked for me, while the accepted answer did not.
– dazed
Nov 24 '16 at 20:10



Just add two attributes to the parent view of editText.


android:focusable="true"
android:focusableInTouchMode="true"





Awesome trick +1.
– Ironman
Oct 22 '16 at 13:21



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:


onCreate()


onStart()


getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);



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>



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.


fragments


getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);



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"/>



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



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



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.



First in your searchable xml the fields (name and hint etc) put @string and not literal strings.


@string



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.


onCreateOptionsMenu


ComponentName


SearchView


getComponentName()



I tried a lot of solutions and after much,much work this solution works for me.



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


<EditText
....
android:enabled="true"
android:focusable="true" />


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





While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
– rollstuhlfahrer
Apr 26 at 6:50






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived