Automatic popping up keyboard on start Activity

101,917

Solution 1

Use this attributes in your layout tag in XML file:

android:focusable="true"
android:focusableInTouchMode="true"

As reported by other members in comments it doesn't works on ScrollView therefore you need to add these attributes to the main child of ScrollView.

Solution 2

You can add this to your Android Manifest activity:

android:windowSoftInputMode="stateHidden|adjustResize"

Solution 3

I have several implementations described here, but now i have added into the AndroidManifest.xml for my Activity the property:

android:windowSoftInputMode="stateAlwaysHidden"

I think this is the easy way even if you are using fragments.

"stateAlwaysHidden" The soft keyboard is always hidden when the activity's main window has input focus.

Solution 4

If you have another view on your activity like a ListView, you can also do:

ListView.requestFocus(); 

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 :)

Solution 5

Use this in your Activity's code:

@Override
public void onCreate(Bundle savedInstanceState) {

getWindow().setSoftInputMode(
   WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

}
Share:
101,917

Related videos on Youtube

Galip
Author by

Galip

Updated on January 01, 2021

Comments

  • Galip
    Galip over 3 years

    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?

  • Luke
    Luke about 13 years
    This fixed my issue as well. I added this to the RelativeLayout tag for my activity. Thank you!
  • Jerry Brady
    Jerry Brady about 13 years
    Interestingly enough, it didn't work when I added the above to a ScrollView, but it did work for a LinearLayout
  • David Doria
    David Doria over 10 years
    This did not work for me - the keyboard still popped up when I start my activity.
  • David Doria
    David Doria over 10 years
    I did this but they keyboard still pops up and the focus is with the editText.
  • Utsav Gupta
    Utsav Gupta almost 10 years
    I think you have to put it on topmost layout
  • crowmagnumb
    crowmagnumb almost 10 years
    So I guess this works because you are telling the layout itself that it can have focus? Otherwise it assumes that it needs to give focus to the first control on the form? Oddly enough, I was getting the keyboard even if all the controls on my form were disabled. Fixed now. Thank you!
  • iversoncru
    iversoncru about 9 years
    this worked for me with both a ScrollView and a RelativeLayout.
  • Pelpotronic
    Pelpotronic about 9 years
    It's not that great for fragments, as you are associating the "hidden state" with the activity that will contain all your fragments, meaning that any fragment you want to display in the activity will be affected by the property (not on a "per fragment" basis).
  • Arthez
    Arthez over 8 years
    It doesnt work for my top level scrollview, I suggest to use Mobile Bloke's answer, so far works for me everytime (in android manifest activity android:windowSoftInputMode="stateHidden|adjustResize")
  • dazza5000
    dazza5000 about 8 years
    Thank you - like mentioned earlier, this didnt' work on a scrollview, but worked on a linearlayout
  • Android Help
    Android Help over 7 years
    Check my answer on this question stackoverflow.com/questions/39593324/…
  • Renato
    Renato over 7 years
    It's important to note that this solution creates accessibility problems (a view that should not be focusable being marked as having focus) and could have unintended consequences.
  • Sreelal S
    Sreelal S about 7 years
    using fragment ..this code work to hide keyboard ...when a fragment open avoid the focus on edittext field keyboard ...
  • Faisal
    Faisal about 7 years
    It works for me, thank you.. I put this code on <LinearLayout />
  • ArtiomLK
    ArtiomLK almost 7 years
    This works, however if you have an image background and are worried about your soft Keyboard shrinking such image background, use android:windowSoftInputMode="stateHidden|adjustPan"
  • jk7
    jk7 over 6 years
    The question was how to prevent the keyboard from opening.
  • Sujeet
    Sujeet about 4 years
    You can check this link, It'll work: stackoverflow.com/questions/18977187/…
  • Sujeet
    Sujeet about 4 years
    If you are facing the issue in activities, You can check this link, It'll work: stackoverflow.com/questions/18977187/…
  • Usama Abdulrehman
    Usama Abdulrehman almost 4 years
    You need to provide any limitations, assumptions or simplifications in your answer. See more details on how to answer at this link: stackoverflow.com/help/how-to-answer