Android show softkeyboard with showSoftInput is not working?

57,424

Solution 1

This is so subtle, that it is criminal. This works on the phones that do NOT have a hard, slide-out keyboard. The phones with a hard keyboard will not open automatically with this call. My LG and old Nexus One do not have a keyboard -- therefore, the soft-keyboard opens when the activity launches (that is what I want), but the MyTouch and HTC G2 phones that have slide-out keyboards do not open the soft keyboard until I touch the edit field with the hard keyboard closed.

Solution 2

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"

Solution 3

Hey I hope you are still looking for the answer as I found it when testing out my code. here is the code:

InputMethodManager imm = (InputMethodManager)_context.getSystemService(Context.INPUT_METHOD_SERVICE);

imm.toggleSoftInput(0, 0);

Here is my question that was answered: android - show soft keyboard on demand

Solution 4

This worked with me on a phone with hard keyboard:

editText1.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
          imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

Solution 5

This answer maybe late but it works perfectly for me. Maybe it helps someone :)

public void showSoftKeyboard(View view) {
    if (view.requestFocus()) {
        InputMethodManager imm = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isShowing = imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
        if (!isShowing)
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }
}

Depends on you need, you can use other flags

InputMethodManager.SHOW_FORCED
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Share:
57,424
mobibob
Author by

mobibob

mobile application developer providing end-to-end content and technology integration

Updated on July 08, 2021

Comments

  • mobibob
    mobibob almost 3 years

    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);
        }
    }
    
  • mobibob
    mobibob about 13 years
    NOTE: I have done a lot of experimenting with the EditText and the InputMethodManager in an attempt to force the soft-keyboard to open when the device has a hard keyboard with no success.
  • Matthew Pape
    Matthew Pape over 9 years
    I have the same problem. Calling showSoftInput() immediately has no visible result, but posting it on a delay causes the keyboard to display correctly. I initially thought, like you, that it was being shown and then promptly hidden by something else. After a little digging though, I found that I could pass in a ResultReceiver and log the results. When I post showSoftInput() on a delay, the result code returned to my receiver is RESULT_SHOWN. When I don't use the delay, my receiver is not called at all. Now I suspect rather than being hidden, it is not showing it at all for some reason.
  • Ankit Bansal
    Ankit Bansal over 8 years
    great I was missing requestFocus() which was stopping the keyboard to be shown for first time.
  • Ugo
    Ugo over 8 years
    Thanks man. Used the first method while fixing issue with the keyboard not showing the first time a dialog fragment (with an EditText) launches.
  • rmirabelle
    rmirabelle almost 8 years
    Another up-vote for calling postDelayed vs. post - there's likely some other default, hidden functionality which is causing the keyboard to be hidden first. Showing and hiding the keyboard is the most massively broken API in existence.
  • Soren Stoutner
    Soren Stoutner over 7 years
    Your First Method works well when programatically switching layouts and opening keyboards. It does seem that some other process is blocking showSoftInput() from functioning correctly.
  • Will Custode
    Will Custode over 7 years
    For anyone doing Xamarin development in Visual Studio, in the AVD Manager you can edit you AVD and there is a setting labelled "Hardware keyboard present". Unchecking this will allow the soft input to display.
  • Shujito
    Shujito about 6 years
    Requesting focus before showing the keyboard works well 👌🏼 (I had it the other way around)
  • mobibob
    mobibob almost 6 years
    @arbitur -- you can relax. This is like a hundred years ago in Google-years. (did you even know that anyone even manufactured slide-out, hard keyboards --haha). Way back then, i was the only one interested in the problem and solution, so I was trying to be complete.
  • Phlip
    Phlip over 3 years
    Brute force to the rescue! (We have an in-house application and support only one tablet, so our Standard Operating Procedure is to use that soft keyboard, so we have no reason to make the user wait for it)
  • Dimon
    Dimon over 2 years
    I was struggling with this problem for a while now, this solved it!