I have an Activity
in Android, with two elements:
EditText
ListView
When my Activity
starts, the EditText
immediately has input focus (flashing cursor). I don't want any control to have input focus at startup. I tried:
EditText.setSelected(false);
No luck. How can I convince the EditText
to not select itself when the Activity
starts?
I had tried several answers individually but the focus is still at the EditText. I only managed to solve it by using two of the below solution together.
( Reference from Silver https://stackoverflow.com/a/8639921/15695 )
and remove
at EditText
( Reference from floydaddict https://stackoverflow.com/a/9681809 )
Is the actual problem that you just don't want it to have focus at all? Or you don't want it to show the virtual keyboard as a result of focusing the
EditText
? I don't really see an issue with theEditText
having focus on start, but it's definitely a problem to have the softInput window open when the user did not explicitly request to focus on theEditText
(and open the keyboard as a result).If it's the problem of the virtual keyboard, see the
AndroidManifest.xml
<activity> element documentation.android:windowSoftInputMode="stateHidden"
- always hide it when entering the activity.or
android:windowSoftInputMode="stateUnchanged"
- don't change it (e.g. don't show it if it isn't already shown, but if it was open when entering the activity, leave it open).Try this before your first editable field:
Late, but maybe helpful. Create a dummy EditText at the top of your layout then call
myDummyEditText.requestFocus()
inonCreate()
That seems to behave as I expect. No need to handle configuration changes, etc. I needed this for an Activity with a lengthy TextView (instructions).
Write this code inside
Manifest
file in theActivity
where you do not want to open the keyboard.Manifest file:
None of this solutions worked for me. The way I fix the autofocus was: