How to disable Android Soft Keyboard for a particular activity? - android

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.

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.

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.

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.

You can try this. It is working for me:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

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.

you can use This Code:
<EditText
.
.
android:enabled="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="numberPassword" />

Related

EditText lose focus when the soft keyboard is displayed in Android Nougat

In Android Nougat, the EditText loses focus when the keyboard is displayed. This is working fine with all the lower versions.
I am using requestFocus() manually to get the focus to the EditText and programmatically showing the keyboard. The EditText is placed inside a ListView.
Could someone help me to resolve the issue?
If you can replace ListView with RecyclierView easily, that would be my suggestion. It worked for me.
Try:
Add android:descendantFocusability="afterDescendants" to your ListView.
Add android:focusable="true" and android:focusableInTouchMode="true" to your EditText.
Add android:windowSoftInputMode="adjustPan" to your Activity in AndroidManifest file.

How to set keyboard in password-type mode on start of the Activity?

I'm using the android:windowSoftInputMode="stateVisible" command for drawing keyboard at the beginning of the Activity and it works just as I want it to work with 2 slight problems - swype-typing is enabled and so is the word prediction and I want to prevent it from happening. I thought maybe setting keyboard in passwordText input mode may solve the problem but I cannot find anywhere how to do it. Is there any method that will allow me to show keyboard in passwordText mode on start of the Activity?
Also I should add that I'm looking for the way of doing that in such a way, that the keybord won't show when app is ran on device with physical keyboard (that's why I used XML declaration in the first place).
Add the following to your Edittext in xml:
android:inputType="textPassword|textNoSuggestions"
or in code:
edittext.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS|InputType.TYPE_TEXT_VARIATION_PASSWORD);
Well, for the first question, AFAIK you set the textfield to password mode, not the keyboard, like this:
<EditText
android:id="#+id/password"
android:hint="#string/password_hint"
android:inputType="textPassword"
... />
More info here: https://developer.android.com/training/keyboard-input/style.html
Now, about preventing the keyboard from appearing on a device with physical keyboard, I recomend you check this answer:
How do I prevent the software keyboard from popping up?

Android 4.0 EditText cursor is always invisible for editable=false

In my app I want an EditText that doesn't accept any input, i.e. android:editable="false" in XML layout or setKeyListener(null) in code.
I only want to add characters in a very controlled manner, and so I always add it programmatically with setText() and I don't want any virtual keyboard to show up. However, I still need a visible cursor in the EditText so that the user will know where the programmatic input will be inserted.
This was very easy to implement (android:editable="false") until Android 4.0. In 4.0, the cursor was apparently removed. I've tried android:cursorVisible="true" but it doesn't work.
Does anyone know how to both have a visible cursor and still suppress input in Android 4.0? Really grateful for any help here.
Please Try
android:clickable="false"
Set android:focusable=false in your EditText.
I had similar problem. Try using:
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setTextIsSelectable(true);
it worked for me. For more details see http://code.google.com/p/android/issues/detail?id=27609

How to prevent the soft keyboard from displaying, or at least evict it once it does?

My app has an EditText that, when I click in it to enter text in the Emulator, brings up a soft keyboard. I don't want this confounded thing to begin with, but then, like the visiting loud-mouthed uncle in the plaid pants, doesn't want to go away, and it is blocking the button beneath it. How do I either (a) prorgrammatically prevent the soft keyboard from appearing or at least (b) evict it, albeit manually, when it pops up?
Provided that the user is not supposed to input text, but is able to click the EditText and then add text in some other way, you could change the EditText to a TextView and then apply the following three tags to it in the layout file:
style="#android:style/Widget.EditText"
android:editable="false"
android:focusableInTouchMode="false"
This will make it look like an EditText, but behave like a TextView.
Since you want the user to be able to write stuff in the EditText there are in my opinion two solutions:
Leave it be. To remove the keyboard, all you need is to hit the back button once and every Android user knows this. It's standard behaviour.
Wrap everything but the Button you say dissapears in a ScrollView. The ScrollView will then wrap its content to allow the Button to be shown in between the keyboard and the ScrollView.
Just set android:editable="false" for your EditText
The answer is to set the focus on an other View like a Button, TextView or similar:
// REQUEST FOCUS
viewName.setFocusableInTouchMode(true);
viewName.requestFocus();
I think what you really need is take a look at android:windowSoftInputMode attribute in Manifest.xml Look into this link.
You can specify the screen to pan/ resize to show the buttons that the input method might be blocking. Not allowing the keyboard to show will make the user unable to enter text at all!

edittext not showing the typed text in android

In my android application i am using a simple login page.
The issue is that when testing in android htc wildfire,the user could not see the text typed till he press back or done
I would like the user to view his text while typing itself.
'
Could anyone please let me know how to resolve this?
Please share your valuable suggestions.
Thanks in advance :)
Looks like your problem is this:
When user clicks in the EditText to type, the keyboard pops up and hides the EditText, thus the user can't see what he's typing
For this, you can put that EditText into a ScrollView so that when keyboard pops-up, Android scrolls the EditText up. This way,the user can see what he's typing.
Well I don't know whether it will help you or not. but am sharing because some other may be benifited. I got the same issue in my Kindle tablet.
And I have solved this just by adding the simple attribute to the edittext.
<EditText
.........
android:textCursorDrawable="#drawable/Black"
/>
And its done. Now it will show each character you types.
If it helps you, I will suggest you to write the above attribute everytime you declare an edittext, even though this issue doesn't occur.

Categories

Resources