Forcing the Soft Keyboard open

Android

Android Problem Overview


I am trying to force the Soft Keyboard open in an Activity and grab everything that is entered as I want to handle the input myself, I don't have an EditText. Currently I have tried this but it does not work. I would like the Soft Keyboardto open below mAnswerTextView (Note: it is a TextView not EditText).

	InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
	// only will trigger it if no physical keyboard is open
	mgr.showSoftInput(mAnswerTextView, InputMethodManager.SHOW_IMPLICIT);
  1. how do I force the Soft Keyboard open
  2. How do I gab everything that is entered so that I can handle each character. I would like to flush each character from the Soft Keyboard after I have handled it. ie, the user should not be able to enter whole words in the Soft Keyboard.

Android Solutions


Solution 1 - Android

try this to force open soft keyboard:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

then you can to use this code to close the keyboard:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(_pay_box_helper.getWindowToken(), 0);

Solution 2 - Android

You'll probably need to have an editable text area of some kind to take focus. You can probably have one invisible or on a transparent background with no cursor, though. You may need to toy around with the focusability settings for the view.

Use a TextWatcher to check for edits to that EditText with addTextChangedListener, or if you need an even-lower-level hook, set the textview's key listener with its setOnKeyListener() method. See the KeyListener documentation.

Use this call to force the soft keyboard open:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
    .showSoftInput(myEditText, InputMethodManager.SHOW_FORCED);

and this one to close it:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
    .hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

note that this is really not recommended - forcing the keyboard open is kind of messy. What's your use case that really necessitates your taking user input without a normal edit box and requires eating user input on a key-by-key basis without echoing it back?

Solution 3 - Android

To force the keyboard to open I used

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

it worked for me.

Solution 4 - Android

Sometimes the other answers won't work.
Here is another way..

It will force the keyboard to show when the activity starts by listening to the window focus. onWindowFocusChanged() it will clear and request focus of the EditText, then set the soft input mode to visible and set the selection to the text in the box. This should always work if you are calling it from the activity.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        mEditText.clearFocus();
        mEditText.requestFocus();
        getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        mEditText.setSelection(mEditText.getText().toString().length());
    }
}

You may also need

mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);
            }
        }
    });

Edit: I have also seen the keyboard not open inside nested fragments, beware of those kinds of situations.

Solution 5 - Android

Sadly, as much as I'd have liked to up-vote one of the replies, none worked for me. It seems the solution is to wait for the layout phase to complete. In the code below, notice how I check if the showKeyboard method returns TRUE, and that's when I remove the global layout listener. Without doing this, it was hit and miss. Now it seems to work perfectly.

You need to do the below ideally in onResume()

@Override
public void onResume()
{
    super.onResume();

    ViewTreeObserver vto = txtTaskTitle.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
            {
                @Override
                public void onGlobalLayout()
                {
                    if (txtTaskTitle.requestFocus())
                    {
                        if (showKeyboard(getContext(), txtTaskTitle))
                        {
                            txtTaskTitle.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        }
                    }
                }
            });
}

public static boolean showKeyboard(Context context, EditText target)
{
        if (context == null || target == null)
        {
            return false;
        }

        InputMethodManager imm = getInputMethodManager(context);

        ((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(target, InputMethodManager.SHOW_FORCED);

        boolean didShowKeyboard = imm.showSoftInput(target, InputMethodManager.SHOW_FORCED);
        if (!didShowKeyboard)
        {
            didShowKeyboard = ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(target, InputMethodManager.SHOW_FORCED);
        }
        return didShowKeyboard;
}

Solution 6 - Android

if you want to control soft keyboard inside activity then use this code:

//create soft keyboard object
InputMethodManager imm = (InputMethodManager)this.getSystemService(INPUT_METHOD_SERVICE);

//1.USE
your_view.setFocusableInTouchMode(true); //Enable touch soft keyboard to this view
//or
your_view.setFocusable(true); //Enable keyboard to this view
imm.showInputMethod(your_view, InputMethodManager.SHOW_IMPLICIT);

//2.USE show keyboard if is hidden or hide if it is shown
imm.toggleSoftInputFromWindow(your_view.getWindowToken(),InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
//or
imm.toggleSoftInputFromWindow(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);

//3.USE (you cannot control imm)
this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);

//4.USE (with Dialog)
Dialog d = new Dialog(this, android.R.style.Theme_Panel);
d.getWindow().setTitle(null);
d.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
d.setOnKeyListener(keyListener);
d.setCanceledOnTouchOutside(true);
d.setCancelable(true);
d.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
d.show();

//to hide keyboard call:
d.dismiss();
//if you want get soft keyboard visibility call:
d.isShowing();

Solution 7 - Android

if(search.getText().toString().trim().isEmpty()) {
	InputMethodManager imm = (InputMethodManager)getSystemService(
		      Context.INPUT_METHOD_SERVICE);
	imm.hideSoftInputFromWindow(search.getWindowToken(), 0);
}

Solution 8 - Android

I have tested and this is working:

... //to show soft keyboard

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

//to hide it, call the method again

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

Solution 9 - Android

Working Great.........

edt_searchfilter_searchtext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){
                edt_searchfilter_searchtext.post(new Runnable() {
                    @Override
                    public void run() {
                        InputMethodManager imm = (InputMethodManager) getFragmentActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(edt_searchfilter_searchtext, InputMethodManager.SHOW_IMPLICIT);
                    }
                });
            }
        }
    });

Below line call when you want to open keyboard

 edt_searchfilter_searchtext.requestFocus();

Solution 10 - Android

Simply, using adding 2 lines will work like a charm:

If using XML

android:focusable="true"
android:focusableInTouchMode="true"

Else in Java:

view.setFocusableInTouchMode(true);
view.requestFocus();

Solution 11 - Android

You can use this KeyboardHelper.java class

    import android.content.Context;
    import android.view.View;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.EditText;
    
    /**
     * Created by khanhamza on 06-Mar-17.
     */

    public class KeyboardHelper {
    
        public static void hideSoftKeyboard(Context context, View view) {
            if (context == null || view == null) {
                return;
            }
    
            InputMethodManager imm = (InputMethodManager) context
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    
        }
    
    
        public static void hideSoftKeyboardForced(Context context, View view) {
            if (context == null) {
              

  return;
        }

        InputMethodManager imm = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromInputMethod(view.getWindowToken(), 0);

    }

    public static void hideSoftKeyboard(Context context, EditText editText) {
        if (context == null) {
            return;
        }
        InputMethodManager imm = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    public static void showSoftKeyboard(Context context, EditText editText) {

        if (context == null) {
            return;
        }

        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
        editText.requestFocus();
    }

    public static void showSoftKeyboardForcefully(Context context, EditText editText) {

        if (context == null) {
            return;
        }

        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
        editText.requestFocus();
    }




}

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
QuestionjaxView Question on Stackoverflow
Solution 1 - AndroidDmitryView Answer on Stackoverflow
Solution 2 - AndroidYoni SamlanView Answer on Stackoverflow
Solution 3 - AndroidAditya MehtaView Answer on Stackoverflow
Solution 4 - AndroidtricknologyView Answer on Stackoverflow
Solution 5 - AndroidstrangetimesView Answer on Stackoverflow
Solution 6 - AndroidNoUserNoNameView Answer on Stackoverflow
Solution 7 - AndroidS SView Answer on Stackoverflow
Solution 8 - AndroidbeginnerView Answer on Stackoverflow
Solution 9 - AndroidPiyushView Answer on Stackoverflow
Solution 10 - Androidsud007View Answer on Stackoverflow
Solution 11 - AndroidHamza KhanView Answer on Stackoverflow