Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Android Soft Keyboard for a particular activity?

I have an activity with one EditText where I need to input numbers only.

Now, I have defined the Input Type for my EditText to be number only and have drawn up a pretty keypad for my user to use, however I also need to make sure the soft keyboard doesn't pop up for my user when they click on the EditText.

I have tried hiding the keyboard through the manifest by adding

android:windowSoftInputMode="stateAlwaysHidden"

in my Manifest for the particular activity, but this doesn't work for me because as soon as the user clicks on the EditText the keyboard appears again.

I've tried doing the same programmatically like so

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

but that doesn't work either. Keyboard appears when the user clicks on the EditText.

The only thing that worked was setting InputType to null for the EditText like so:

EditText.setInputType(InputType.TYPE_NULL);

but I cannot use this because it will allow users who have a keyboard attached to their device to input letters and other symbols in the EditText field, while I want everyone to specifically use only the keypad to enter data in the field.

I should also mention that I am currently testing my app under android 2.1, but I would like my solution to work across all versions. Any help would be appreciated. Thanks in advance.

like image 574
Gligor Avatar asked May 01 '11 20:05

Gligor


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?

Ok everyone knows that to hide a keyboard you need to implement: InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);

How do I hide soft keyboard when EditText is focused?

editTextNum. setShowSoftInputOnFocus(false); to disable the software keyboard showing when the EditText is touched. the hideKeyboard(this); call in OnCreate to forcible hide the software keyboard.


1 Answers

You can try this. It is working for me:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
                     WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
like image 108
Willy Lin Avatar answered Nov 16 '22 03:11

Willy Lin