68
votes

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?

10
(Shooting in the dark, but) have you tried requestFocus on the text box?Roy Truelove

10 Answers

102
votes

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

android:windowSoftInputMode="stateVisible"

Hope that helps for you as well.

21
votes

Easiest solution: Put

android:windowSoftInputMode = "stateVisible" 

in Activity section of AndroidManifest.xml

14
votes

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.

11
votes

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

android:windowSoftInputMode="stateVisible|adjustPan"
8
votes

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"
6
votes

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);
}
3
votes

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)
1
votes

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

0
votes

File : AndroidManifest.xml

<activity android:name=".MainActivity">

Add following property :

android:windowSoftInputMode="stateVisible"

Which worked for me.

0
votes

paste this after setContentView

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