10

I have an issue when the activity starts, android automatically places focus on the first edittext. How do I prevent android from doing that?

Hades
  • 3,916
  • 3
  • 34
  • 74

3 Answers3

15

Have a look here

Or you could just hide the keyboard, focus remains on the EditText upon starting the activity:

android:windowSoftInputMode="stateHidden"
Community
  • 1
  • 1
SteD
  • 13,909
  • 12
  • 65
  • 76
1

Three ways to solve this..

  1. Set these attributes to the parent..

And now, when activity starts this layout getting default focus.

Also we can remove focus from children views in runtime (e.g. after finishing child editing):

findViewById(R.id.mainLayout).requestFocus(); or

Look in the AndroidManifest.xml element.

android:windowSoftInputMode="stateHidden"

It always hide key board when entering the activity.

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
1

use setFocusable (boolean focusable) to prevent. http://developer.android.com/reference/android/view/View.html#setFocusable(boolean)

Ajay Singh
  • 1,611
  • 3
  • 22
  • 29
  • 4
    The question is how to prevent automatically focus, not how to set it to un-focused. – chengbo Dec 19 '12 at 02:30
  • @cheng "To change whether a view can take focus, call setFocusable(boolean)" - http://developer.android.com/reference/android/view/View.html#setFocusable(boolean) – samus Feb 28 '13 at 21:30