7

I have a quite simple Layout with an EditText and a button. The problem is the keyboard appears immediately after the activity is started and the EditText gets the focus. I removed the </requestFocus> from XML and I also don't do that in code. How can I prevent that behavior of EditText so the keyboard only appears after the used taps the editText?

Droidman
  • 11,485
  • 17
  • 93
  • 141

3 Answers3

5

In your manifest.xml file, under the activity tag, place this:

android:windowSoftInputMode="stateHidden"
yedpodtrzitko
  • 9,035
  • 2
  • 40
  • 42
Gridtestmail
  • 1,459
  • 9
  • 10
  • 1
    thanks, that solved the issue. Can you tell me if that code can have any side effects which I should keep in mind? – Droidman Jan 09 '13 at 16:59
  • The answer below using `adjustNothing` is better. If you use `stateHidden`, then the keyboard will be closed when open on rotate. That is not usually desirable behavior. – Sky Kelsey May 05 '14 at 00:18
  • 3
    I cannot believe how many people state this very wrong answer. – Anderson Jul 08 '14 at 09:47
4

try this

In your AndroidManifest.xml file write these lines

<activity
        android:configChanges="keyboardHidden|orientation"
        android:name=".YourActivityName"
        android:windowSoftInputMode="stateHidden" />

i just added details..

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

From my POV a more elegant solution is:

  1. In XML add these lines to the main layout:

    <LinearLayout
        android:id="@+id/mainLayout"
        android:focusable="true"
        android:focusableInTouchMode="true"
        ...
        .../>
    
  2. And In Java in onCreate():

      LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
      mainLayout.requestFocus();
    
Artyom Okun
  • 939
  • 1
  • 8
  • 14