Show soft keyboard when Activity starts

AndroidKeyboardSoft Keyboard

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

android:windowSoftInputMode="stateVisible"

Hope that helps for you as well.

Solution 2 - Android

Easiest solution: Put

android:windowSoftInputMode = "stateVisible" 

in Activity section of AndroidManifest.xml

Solution 3 - Android

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.

Solution 4 - Android

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

android:windowSoftInputMode="stateVisible|adjustPan"

Solution 5 - Android

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"

Solution 6 - Android

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

Solution 7 - Android

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)

Solution 8 - Android

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

Solution 9 - Android

File : AndroidManifest.xml

<activity android:name=".MainActivity">

Add following property :

android:windowSoftInputMode="stateVisible"

Which worked for me.

Solution 10 - Android

paste this after setContentView

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAl.View Question on Stackoverflow
Solution 1 - AndroidLeoView Answer on Stackoverflow
Solution 2 - AndroidUncaught ExceptionView Answer on Stackoverflow
Solution 3 - AndroidsynicView Answer on Stackoverflow
Solution 4 - AndroidPaulView Answer on Stackoverflow
Solution 5 - AndroidabcView Answer on Stackoverflow
Solution 6 - AndroidwhlkView Answer on Stackoverflow
Solution 7 - AndroidKishan SolankiView Answer on Stackoverflow
Solution 8 - AndroidChad HedgcockView Answer on Stackoverflow
Solution 9 - AndroidKeyur SureliyaView Answer on Stackoverflow
Solution 10 - AndroidAshwiniView Answer on Stackoverflow