Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide Android soft keyboard on EditText

I have an Activity with some EditText fields and some buttons as a convenience for what normally would be used to populate those fields. However when we the user touches one of the EditText fields the Android soft keyboard automatically appears. I want it to remain hidden by default, unless the user long presses the menu button. I have search for a solution to this and found several answers, but so far I can't get them to work.

I have tried the following:

1 - In the onCreate method,

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

2 - Also in the onCreate method,

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY); 

3 - and fIn the Manifest file,

<activity android:name=".activityName" android:windowSoftInputMode="stateAlwaysHidden"/> 

None of these methods work. Whenever the user clicks on the EditText field, the soft keyboard appears. I only want the soft keyboard to appear if the user explicitly shows it by long pressing the menu key.

Why isn't this working?

like image 273
ScubaSteve Avatar asked Jan 25 '12 02:01

ScubaSteve


People also ask

How do I get rid of soft keyboard on Android?

Here pass HIDE_IMPLICIT_ONLY at the position of showFlag and 0 at the position of hiddenFlag . It will forcefully close soft Keyboard.

How do I hide the soft keyboard on Android after clicking outside EditText?

and put the following code in the onTouch method. InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);

How do I close hide the soft keyboard programmatically in Android?

Hiding the Soft Keyboard Programmatically You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.

How to disable software keyboard on edittext on Android?

Show software keyboard for numeric input EditText and hide on other input EditText. Android has no property API to disable the software keyboard from coming up, when a user touches an EditText.

How to enter alphabetic symbols in edittext using smart pad?

Close EditText Smart pad keyboard on button click through coding. After clicking on EditText it will automatically open a soft keyboard also known as smart pad to enter alphabetic, numeric, symbols values inside edittext so user can use them any where.

Is there a way to display soft keyboard in WebView?

However, there’s no same support for WebView yet! Check out the result below, the focus retains in EditText, and selection together with context menu all work, but however hard you tried clicking, the soft keyboard just won’t show up. This is great! Now you can programmatically to make the view of custom keyboard visible.

How to show soft keyboard in long key press event?

To show soft keyboard, you have to write following code in long key press event of menu button editText.setInputType (InputType.TYPE_CLASS_TEXT); editText.requestFocus (); InputMethodManager mgr = (InputMethodManager) getSystemService (Context.INPUT_METHOD_SERVICE); mgr.showSoftInput (editText, InputMethodManager.SHOW_FORCED);


2 Answers

This will help you

editText.setInputType(InputType.TYPE_NULL); 

Edit:

To show soft keyboard, you have to write following code in long key press event of menu button

editText.setInputType(InputType.TYPE_CLASS_TEXT);             editText.requestFocus();             InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);             mgr.showSoftInput(editText, InputMethodManager.SHOW_FORCED); 
like image 190
Sandy Avatar answered Oct 07 '22 03:10

Sandy


You need to add the following attribute for the Activity in your AndroidManifest.xml.

<activity     ...     android:windowSoftInputMode="stateHidden|adjustResize"     ... /> 
like image 23
Emran Hamza Avatar answered Oct 07 '22 01:10

Emran Hamza