How to remove auto focus/keyboard popup of a field when the screen shows up?

89

14

I have a screen where the first field is an EditText, and it gains the focus at startup, also popups the numeric input type, which is very annoying

How can I make sure that when the activity is started the focus is not gained, and/or the input panel is not raised?

Pentium10

Posted 2010-05-23T16:58:58.480

Reputation: 127 619

Answers

134

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editTextField.getWindowToken(), 0);

or

set activity property in manifest file as below in the application tag

android:windowSoftInputMode="stateHidden"

Mitul Nakum

Posted 2010-05-23T16:58:58.480

Reputation: 4 481

2simple answer ...thanks to Mitual Nakum – None – 2015-12-22T05:59:43.547

Its working now, thanks. – Akshay kumar – 2017-07-04T13:31:08.300

Setting the android:windowSoftInputMode in AndroidManifest.xml file can be ORed with other values. Mode typically implies single values but in this case, the value is a collection of flags. Description of values: Formats: flag Values: adjustNothing, adjustPan, adjustResize, adjustUnspecified, stateAlwaysHidden, stateAlwaysVisible, stateHidden, stateUnchanged, stateUnspecified, stateVisible Specify the default soft-input mode for the main window of this activity. A value besides "unspecified" here overrides any value in the theme. – Jeff Muir – 2017-08-29T05:20:33.290

139

go to your application manifest file, and write this line for that activity you want to disable auto keyboard pop-up.

android:windowSoftInputMode="stateHidden"

sm.euro

Posted 2010-05-23T16:58:58.480

Reputation: 1 399

Didn't work for me. I put it in activity_main.xml. This worked perfect when I put it in onCreate(). getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); – zappullae – 2018-02-25T15:58:41.643

17

To programatically not have the keyboard displayed, but the default widget still recieve focus call:

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

in onResume()

danmux

Posted 2010-05-23T16:58:58.480

Reputation: 1 280

9

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

call the above method inside onCreate().It prevent softKeyboard to show unless user select EditText by tapping or clicking.

or simply add android:windowSoftInputMode="stateHidden" in Activity tag in Manifest.xml

Xar-e-ahmer Khan

Posted 2010-05-23T16:58:58.480

Reputation: 991

4

Have another view grab focus. By default, the first focusable View will get focus when a layout is inflated. You can request focus on a different View via XML:

<TextView
    android:layout_width="wrap_parent"
    android:layout_height="wrap_content"
    android:text="Some other view">

    <requestFocus />
</TextView>

This works for any View.

If you want to do it programmatically, you can use view.requestFocus().

Jason Robinson

Posted 2010-05-23T16:58:58.480

Reputation: 22 546

3

This is usually a mess. The first thing I try is try to steal the focus with another view via . You also have to have the focusable and focusableInTouchMode.

<TextView
  ...
  android:focusable="true"
  android:focusableInTouchMode="true">

    <requestFocus/>
</TextView>

urSus

Posted 2010-05-23T16:58:58.480

Reputation: 7 395

0

if(getWindow().getAttributes().softInputMode==WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED)
{
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}

paul Liang

Posted 2010-05-23T16:58:58.480

Reputation: 1

3Welcome to StackOverflow! This is not like other forums, if you want to change your answer, just click the edit link and make your changes. And please try to format your code by selecting your code and using the Code Sample button {} or pressing Ctrl+K. – Peter Lang – 2012-05-24T10:09:04.183

-1

have not tried this nor am i near my programming computer, but I would suspect programmatically sending focus to the parent view or something of that nature could do the trick - thats more likely a workaround than a solution, but again not able to test it just a thought

Ben

Posted 2010-05-23T16:58:58.480

Reputation: 114

I've tried that but doesn't work. I called .requestFocus(); on a button, and still the keyboard popups. – Pentium10 – 2010-05-28T08:47:12.867

thoughts can be added as comments – Alberto M – 2018-06-05T08:01:26.623