Open soft keyboard programmatically

AndroidAndroid Softkeyboard

Android Problem Overview


I have an activity with no child widgets for it and the corresponding xml file is,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"
>
</LinearLayout>

and I want to open soft keyboard programmatically while the activity gets start.and what I've tried upto now is,

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    if (inputMethodManager != null) {
        inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    }

Give me some guidance.

Android Solutions


Solution 1 - Android

I have used the following lines to display the soft keyboard manually inside the onclick event, and the keyboard is visible.

InputMethodManager inputMethodManager =
    (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
    linearLayout.getApplicationWindowToken(),
    InputMethodManager.SHOW_FORCED, 0);

But I'm still not able to open this while the activity gets opened, so are there any solution for this?

Solution 2 - Android

In your manifest file, try adding the following to the <activity> that you want to show the keyboard when the activity starts:

android:windowSoftInputMode="stateVisible"

This should cause the keyboard to become visible when the activity starts.

For more options, checkout the documentation.

Solution 3 - Android

Please follow the below code. I am sure your problem will be solved.

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

Solution 4 - Android

This is works

<activity
    ...
    android:windowSoftInputMode="stateVisible" >
</activity>

or

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

Solution 5 - Android

All I needed was to expose the keyboard, in a very precise moment. This worked for me! Thanks Benites.

	private Handler mHandler= new Handler();

And in the very precise moment:

	mHandler.post(
	new Runnable() {
	    public void run() {
	        InputMethodManager inputMethodManager =  (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
	        inputMethodManager.toggleSoftInputFromWindow(yourEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
	        yourEditText.requestFocus();
	    }
	});	

Solution 6 - Android

I have used the following lines to display the soft keyboard manually inside the onclick event.

public void showKeyboard(final EmojiconEditText ettext){
 		  ettext.requestFocus();
 		  ettext.postDelayed(new Runnable(){
 		    @Override public void run(){
 		      InputMethodManager keyboard=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
 		      keyboard.showSoftInput(ettext,0);
 		    }
 		  }
 		,200);
 		}

Solution 7 - Android

##Kotlin

fun hideKeyboard(activity: Activity) {
    val view = activity.currentFocus
    val methodManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    assert(view != null)
    methodManager.hideSoftInputFromWindow(view!!.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}

private fun showKeyboard(activity: Activity) {
    val view = activity.currentFocus
    val methodManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    assert(view != null)
    methodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}

##Java

public static void hideKeyboard(Activity activity) {
    View view = activity.getCurrentFocus();
    InputMethodManager methodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    assert methodManager != null && view != null;
    methodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

private static void showKeyboard(Activity activity) {
    View view = activity.getCurrentFocus();
    InputMethodManager methodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    assert methodManager != null && view != null;
    methodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}

Solution 8 - Android

Put that in onResume method:

findViewById(R.id.root_view_of_your_activity_layout).post(
new Runnable() {
    public void run() {
        InputMethodManager inputMethodManager =  (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.toggleSoftInputFromWindow(yourEditText.getApplicationWindowToken(),     InputMethodManager.SHOW_FORCED, 0);
        yourEditText.requestFocus();
    }
});

the runnable is needed because when the OS fires the onResume method you can't be sure that all the views where draw, so the post method called from your root layout makes it wait till every view is ready.

Solution 9 - Android

in onCreate method of activity or onActivityCreated of a fragment

....
view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            view.removeOnPreDrawListener(this);
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
            
            // !Pay attention to return `true`
            // Chet Haase told to 
            return true;
        }
    });

Solution 10 - Android

seems like this is working

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patientid);
        
        editText = (EditText)findViewById(R.id.selectPatient);
        //editText.requestFocus(); //works without that
    
    }

@Override
	protected void onResume() {
		
		findViewById(R.id.selectPatient).postDelayed(
		new Runnable() {
		    public void run() {
		    	 editText.requestFocus();
		        InputMethodManager inputMethodManager =  (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		        inputMethodManager.showSoftInput(editText,InputMethodManager.SHOW_IMPLICIT);
		    }
		},100);
		super.onResume();
	}

seems this works better: in manifest:

<application>
    <activity
        android:name="com.doodkin.myapp.ReportActivity"
        android:label="@string/title_activity_report"
        android:screenOrientation="sensor" 
        android:windowSoftInputMode="stateHidden" > // add this or stateVisible
    </activity>
</application>

seems the manifest working in android 4.2.2 but not working in android 4.0.3

Solution 11 - Android

I used it as singleton like:

public static void showSoftKeyboard(final Context context, final EditText editText) {
        try {
            editText.requestFocus();
            editText.postDelayed(
                    new Runnable() {
                        @Override
                        public void run() {
                            InputMethodManager keyboard = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                            keyboard.showSoftInput(editText, 0);
                        }
                    }
                    , 200);
        } catch (NullPointerException npe) {
            npe.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Use it in your activity like:

showSoftKeyboard(this, yourEditTextToFocus);

Solution 12 - Android

I have used like this to show the soft keyboard programatically and this is worked for me to prevent the auto resize of the screen while launching the keyboard.

In manifest:

<activity android:name="XXXActivity" android:windowSoftInputMode="adjustPan">
</activity>

In XXXActvity:

EditText et =  (EditText))findViewById(R.id.edit_text);  
  Timer timer = new Timer();
    		TimerTask task = new TimerTask() {
    			
    			@Override
    			public void run() {
    				InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    				inputMethodManager.toggleSoftInputFromWindow(et.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
    				
    			}
    		};
    		timer.schedule(task, 200);

I assume this will save others time to search for this problem.

Solution 13 - Android

InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

Solution 14 - Android

This works:

private static void showKeyboard(Activity activity) {
    View view = activity.getCurrentFocus();
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}

And you call this method like this:

showKeyboard(NameOfActivity.this);

Solution 15 - Android

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

Use above code in onResume() to open soft Keyboard

Solution 16 - Android

InputMethodManager.SHOW_FORCED isn't good choice. If you use this setting you should manage hiding keyboard state. My suggestion is like this;

    public void showSoftKeyboard(View view) {
    InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
    view.requestFocus();
    inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
}

Also, you can focus on view (usually EditText) taking parameters it. This makes it a more useful function

for more info about InputMethodManager.SHOW_IMPLICIT and SHOW_FORCED; InputMethodManager

Solution 17 - Android

public final class AAUtilKeyboard {

public static void openKeyboard(final Activity activity, final EditText editText) {
    final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm != null) {
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
    }
}

public static void hideKeyboard(final Activity activity) {
    final View view = activity.getCurrentFocus();
    if (view != null) {
        final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
}

Solution 18 - Android

perfectly working code to show and hide softkeyboard for edittextbox.....

// code to hide soft keyboard
public void hideSoftKeyBoard(EditText editBox) {
     InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
     imm.hideSoftInputFromWindow(editBox.getWindowToken(), 0);  
}




// code to show soft keyboard
private void showSoftKeyBoard(EditText editBox){
     InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
     editBox.requestFocus();
     inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}

Solution 19 - Android

Similar to the answer of @ShimonDoodkin this is what I did in a fragment.

https://stackoverflow.com/a/29229865/2413303

    passwordInput.postDelayed(new ShowKeyboard(), 300); //250 sometimes doesn't run if returning from LockScreen

Where ShowKeyboard is

private class ShowKeyboard implements Runnable {
    @Override
    public void run() {
        passwordInput.setFocusableInTouchMode(true);
        passwordInput.requestFocus();            
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(passwordInput, 0);
    }
}

After a successful input, I also make sure I hide the keyboard

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(getView().getWindowToken(), 0);

Solution 20 - Android

This is the required source code :

public static void openKeypad(final Context context, final View v) 
 {
new Handler().postDelayed(new Runnable() 
{
    @Override
    public void run() 
    {
        InputMethodManager inputManager =   (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); 
        inputManager.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);    
        Log.e("openKeypad", "Inside Handler");
    }
},300);}

For details , Please go through this link. This helped me. https://github.com/Nikhillosalka/Keyboard/blob/master/README.md

Solution 21 - Android

There are already too many answers but nothing worked for me apart from this

inputMethodManager.showSoftInput(emailET,InputMethodManager.SHOW_FORCED);

I used showSoftInput with SHOW_FORCED

And my activity has

 android:windowSoftInputMode="stateVisible|adjustResize"

hope this helps someone

Solution 22 - Android

Post this method in your base activity and use it other activities like a charm

public void openKeyboard() {
    InputMethodManager imm =
            (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm != null) {
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    }
}

Solution 23 - Android

I like to do it as an extension to Context so you can call it anywhere

fun Context.showKeyboard(editText: EditText) {
    val inputMethodManager: InputMethodManager =
        getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.toggleSoftInputFromWindow(
        editText.applicationWindowToken,
        InputMethodManager.SHOW_IMPLICIT, 0
    )
    editText.requestFocus()
    editText.setSelection(editText.text.length)
}

fun Context.hideKeyboard(editText: EditText) {
    val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(editText.windowToken, 0)
}

Solution 24 - Android

Based on above answers like this it works in KOTLIN as long as you have the context.

fun Context.showKeyboard(editText: EditText) {

    editText.requestFocus()
    editText.setSelection(editText.text.length)

    GlobalScope.launch {
        delay(200L)

        val inputMethodManager: InputMethodManager =
            getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.toggleSoftInputFromWindow(
            editText.applicationWindowToken,
            InputMethodManager.SHOW_IMPLICIT, 0
        )
    }
}

Then you can call it in your fragment for example as follows

> requireContext().showKeyboard(binding.myEditText)

Solution 25 - Android

import androidx.core.view.WindowInsetsCompat.Type
import androidx.core.view.WindowInsetsControllerCompat

fun Activity.openKeyboard() {
    WindowInsetsControllerCompat(window, window.decorView).show(Type.ime())
}

fun Activity.hideKeyboard() {
    WindowInsetsControllerCompat(window, window.decorView).hide(Type.ime())
}

Solution 26 - Android

If you want the keyboard to be up along with the activity/fragment launch, you can use the below code.

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

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
QuestionVigneshView Question on Stackoverflow
Solution 1 - AndroidVigneshView Answer on Stackoverflow
Solution 2 - AndroidjemerickView Answer on Stackoverflow
Solution 3 - AndroidAndroidDangerView Answer on Stackoverflow
Solution 4 - AndroidahtartamView Answer on Stackoverflow
Solution 5 - AndroidDiego SotoView Answer on Stackoverflow
Solution 6 - AndroidRamView Answer on Stackoverflow
Solution 7 - AndroidKhemraj SharmaView Answer on Stackoverflow
Solution 8 - AndroidMarcelo BenitesView Answer on Stackoverflow
Solution 9 - AndroidAlexView Answer on Stackoverflow
Solution 10 - AndroidShimon DoodkinView Answer on Stackoverflow
Solution 11 - AndroidShailendra MaddaView Answer on Stackoverflow
Solution 12 - AndroidNoundla SandeepView Answer on Stackoverflow
Solution 13 - AndroidKai WangView Answer on Stackoverflow
Solution 14 - AndroidThiago SilvaView Answer on Stackoverflow
Solution 15 - AndroidSanket ParchandeView Answer on Stackoverflow
Solution 16 - AndroidCafer Mert CeyhanView Answer on Stackoverflow
Solution 17 - AndroidArsenView Answer on Stackoverflow
Solution 18 - AndroidYESVGO JOBSView Answer on Stackoverflow
Solution 19 - AndroidEpicPandaForceView Answer on Stackoverflow
Solution 20 - AndroidNikhil LosalkaView Answer on Stackoverflow
Solution 21 - AndroidspaceMonkeyView Answer on Stackoverflow
Solution 22 - AndroidAjay ChauhanView Answer on Stackoverflow
Solution 23 - AndroidSean CalkinsView Answer on Stackoverflow
Solution 24 - AndroidF.MysirView Answer on Stackoverflow
Solution 25 - AndroidTatsuya FujisakiView Answer on Stackoverflow
Solution 26 - AndroidShubham GoelView Answer on Stackoverflow