70

I have 2 activities, A and B. When A starts, it checks for a condition and if true, it calls startActivityForResult() to start B. B only takes text input so it makes sense for the soft keyboard to automatically pop up when B start. When the activity starts, the EditText already has focus and it ready for input.

The problem is that the keyboard never shows up, even with windowSoftInputMode="stateAlwaysVisible" set in the manifest under the <activity> tag for B. I also tried with the value set to stateVisible. Since it doesn't show up automatically, I have to tap the EditText to make it show.

Anyone know what the solution might be?

Al.
  • 2,285
  • 2
  • 22
  • 30

10 Answers10

103

What worked best for me is in Android Manifest for activity B adding

android:windowSoftInputMode="stateVisible"

starball
  • 20,030
  • 7
  • 43
  • 238
Leo
  • 4,652
  • 6
  • 32
  • 42
  • 2
    I should note that so far, this works well for the Nexus One running 2.2 but some of the Motorola devices such as Milestone with 2.1 seem to ignore this. – Leo Feb 09 '11 at 21:14
  • Thanks, to do that programmatically check https://stackoverflow.com/questions/5593053/open-soft-keyboard-programmatically – David Magalhães Feb 15 '17 at 16:02
  • Using `stateVisible` results in the keyboard reappearing when the device orientation changes. If this behaviour is not desired, then `stateVisible` is not the solution. – Alex Peters Jan 04 '18 at 12:48
  • 3
    For newbies like me: you must add this line right after – LePatay Jul 09 '18 at 10:21
  • The problem with this method is that it can't be propagated to many apps using a central library. You need to repeat this behavior in all manifest files. A programmatic solution solves that problem too. – Saeed Neamati Jul 19 '18 at 08:14
22

Easiest solution: Put

android:windowSoftInputMode = "stateVisible" 

in Activity section of AndroidManifest.xml

Uncaught Exception
  • 2,149
  • 18
  • 25
14

If requestFocus on an EditText isn't showing it, maybe this'll do it:

InputMethodManager imm = (InputMethodManager)getSystemService(
    Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, 0);

Look here for more information.

synic
  • 26,359
  • 20
  • 111
  • 149
  • 1
    this should work so long as you're not specifying a different soft input state anywhere else, like in your manifest or in code. i.e this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); – topwik Nov 13 '12 at 14:55
11

For me worked only this solutions: add in manifest for that activity:

android:windowSoftInputMode="stateVisible|adjustPan"
Paul
  • 3,812
  • 10
  • 50
  • 73
8

I have got two way.

Method 1. Use the following code inside the OnCreate method

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

It will prevent popping up keyboard unless you click.

or

Method 2 You can move away the focus on other view like TextView by using "requestfocus" in the xml.

<TextView
            android:id="@+id/year_birth_day"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="1991">            
           <requestFocus />
           </TextView>

Method 3 ( I think it should be avoidable) Using the following code in the manifest-

android:windowSoftInputMode="stateVisible"
abc
  • 275
  • 3
  • 11
6

Try showing the keyboard with some delay. Something similar to this:

public void onResume() {
    super.onResume();

    TimerTask tt = new TimerTask() {

        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(yourTextBox, InputMethodManager.SHOW_IMPLICIT);
        }
    };

    final Timer timer = new Timer();
    timer.schedule(tt, 200);
}
whlk
  • 15,487
  • 13
  • 66
  • 96
3

Major Attention Required!

android:windowSoftInputMode="stateVisible|adjustPan" This alone won't work to show keyboard on activity start.

You also need to explicitly add this into your class

editTextXYZ.requestFocus()
        val imm: InputMethodManager =
            getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(editTextXYZ, InputMethodManager.SHOW_IMPLICIT)
Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82
1

File : AndroidManifest.xml

<activity android:name=".MainActivity">

Add following property :

android:windowSoftInputMode="stateVisible"

Which worked for me.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Keyur Sureliya
  • 164
  • 3
  • 11
1

If you're using an emulator, you have to turn the hard keyboard off in order for the soft keyboard to show.

Chad Hedgcock
  • 11,125
  • 3
  • 36
  • 44
0

paste this after setContentView

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Ashwini
  • 653
  • 6
  • 7