Android show softkeyboard with showSoftInput is not working?

mobibob picture mobibob · Apr 2, 2011 · Viewed 50.2k times · Source

I have created a trivial application to test the following functionality. When my activity launches, it needs to be launched with the softkeyboard open.

My code does not work?!

I have tried various "state" settings in the manifest and different flags in the code to the InputMethodManager (imm).

I have included the setting in the AndroidManifest.xml and explicitly invoked in the onCreate of the only activity.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mycompany.android.studyIme"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".StudyImeActivity"
                  android:label="@string/app_name" 
                  android:windowSoftInputMode="stateAlwaysVisible">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

... the main layout (main.xml) ...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
    <EditText
        android:id="@+id/edit_sample_text"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:hint="@string/hello"
        android:inputType="textShortMessage"
    />
</LinearLayout>

... and the code ...

public class StudyImeActivity extends Activity {
    private EditText mEditTextStudy;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mEditTextStudy = (EditText) findViewById(R.id.edit_study);
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(mEditTextStudy, InputMethodManager.SHOW_FORCED);
    }
}

Answer

AndroidGeek picture AndroidGeek · Dec 18, 2014

When the activity launches It seems that the keyboard is initially displayed but hidden by something else, because the following works (but is actually a dirty work-around):

First Method

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.postDelayed(new Runnable()
{
    @Override
    public void run()
    {
        editText.requestFocus();
        imm.showSoftInput(editText, 0);
    }
}, 100);

Second method

in onCreate to launch it on activity create

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() 
    {
    //  InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    //    inputMethodManager.toggleSoftInputFromWindow(EnterYourViewHere.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);

        if (inputMethodManager != null)
        {
            inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
        } 
    }
}, 200);

Third Method ADD given code to activity tag in Manifest. It will show keyboard on launch, and set the first focus to your desire view.

android:windowSoftInputMode="stateVisible"