I got a relative simple question. I have an activity with a lot of EditText's in them. When I open the activity it automatically focusses to the first EditText and displays the virtual keyboard.
How can I prevent this?
I got a relative simple question. I have an activity with a lot of EditText's in them. When I open the activity it automatically focusses to the first EditText and displays the virtual keyboard.
How can I prevent this?
I have found this simple solution that worked for me.Set these attributes in your parent layout:
And now, when the activity starts this main layout will get focus by default.
Also, we can remove focus from child views at runtime by giving the focus to the main layout again, like this:
Hope it will work for you .
Use this attributes in your layout tag in XML file:
As reported by other members in comments it doesn't works on
ScrollView
therefore you need to add these attributes to the main child ofScrollView
.If you have another view on your activity like a
ListView
, you can also do:in your onResume() to grab focus from the
editText
.I know this question has been answered but just providing an alternative solution that worked for me :)
I had a simular problem, even when switching tabs the keyboard popped up automatically and stayed up, with Android 3.2.1 on a Tablet. Use the following method:
In the onCreate() and in the onPause() of the activity for each EditText:
setEditTextFocus(myEditText, false);
For each EditText an OnTouchListener:
For each
EditText
in theOnEditorActionListener
:And for each
EditText
in thelayout xml
:There is probably more code optimizing possible.
This has some good answers at the following post : Stop EditText from gaining focus at Activity startup. The one I regularly use is the following code by Morgan :
NOTE : The dummy item has to be PLACED RIGHT BEFORE the focusable element.
And I think it should work perfectly even with ScrollView and haven't had any problems with accessibility either for this.