12

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.

Gligor
  • 2,862
  • 2
  • 33
  • 40

6 Answers6

6

In Whichever Edittext you need to disable the soft keyboard, add attribute in xml of that edit text.

<EditText android:id=".."
          ..
          android:focusable="false" />

It will stop the execution of soft keyboard.

Krups
  • 407
  • 6
  • 13
  • 2
    You should not do that. If thats the case then from a previous EditText field, clicking on Next would not make this view to requestFocus. – lokoko Feb 05 '13 at 07:45
6

Just thought it might be possible to extend EditText and handle the access to the softkeyboard through there and came across this question which has a very elegant solution to my problem.

Just followed the steps and this handled the softkeyboard perfectly for me. Hope it helps others that come across this issue.

Community
  • 1
  • 1
Gligor
  • 2,862
  • 2
  • 33
  • 40
4

Use the android:windowSoftInputMode="stateHidden" in your activity in manifest file. This will work. Don't use the android:focusable="false" for EditText unless if you are not willing to input the text. If you want to give input then remove that property for the edittext in the layout file.

java dev
  • 1,044
  • 1
  • 11
  • 17
  • 3
    This does not seem to work: when selecting an EditText, the soft keyboard still opens. I believe the windowSoftInputMode attribute is designed to control the behavior of the activity when the window receives focus, not all the time. – personne3000 May 09 '13 at 08:26
  • This works for what I want: On activity start don't show soft keyboard – adrian4aes Feb 04 '14 at 17:13
4

You can try this. It is working for me:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
                     WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
YesThatIsMyName
  • 1,585
  • 3
  • 23
  • 30
Willy Lin
  • 41
  • 1
0

If you are using for example ViewGroup it allows you to block the soft keyboard using this method:

view.setDescendantFocusability(view.FOCUS_BLOCK_DESCENDANTS);

Thanks to this the view will get focus before any of its descendants.

Tafari
  • 2,639
  • 4
  • 20
  • 28
-2

you can use This Code:

<EditText
    .
    .
    android:enabled="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:inputType="numberPassword" />