Forcing the Soft Keyboard open
Asked Answered
C

11

53

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.
Croy answered 19/3, 2010 at 18:0 Comment(1)
Hi, I have the same question as you. I am able to show the keyboard, but how do I grab everything that is entered without having an EditText? Thanx!Scrip
O
139

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);
Orvalorvan answered 8/8, 2011 at 3:50 Comment(3)
Thanks. What's _pay_box_helper in the 'close keyboard' example? EDIT: Ah, I see, it's a text field variable.Anthropocentric
Works great for me, on the collapsible menu edit text on Action Bar Sherlock. Thanks.Gareth
The only solution that worked for my fragment's EditText in Lollipop.Orola
C
15

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?

Compossible answered 19/3, 2010 at 18:17 Comment(5)
I am getting a huge quantity of errors from the Droid. In particular the java.lang.IndexOutOfBoundsException: getChars (6 ... 0) has end before start bug. This is due to the EditTextCroy
due to what EditText? And where is this error coming from, the TextWatcher, KeyListener, or the showSoftInput/hideSoftInput calls? You'll also have to post your code and the stack trace for anyone to be able to help you out here.Compossible
showSoftInput() doesn't always work on the first try. I am finding that I have to repeat my action on the view 2 times in order for it to show.Milesmilesian
Important note: InputMethodManager.SHOW_FORCED is not a valid flag for the showSoftInput() method! Only 0 and InputMethodManager.SHOW_IMPLICIT are valid flags!Alas
Pay attention on moment that you execute this command. Sometimes, the view was not rendered yet.Falzetta
R
9

To force the keyboard to open I used

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

it worked for me.

Rafaellle answered 7/12, 2012 at 22:14 Comment(2)
Works for me, but you have to call this before 'super.onCreate()' in 'onCreate'Slipcase
Does not work. And, calling this before super in .onCreate creates a null. Wrong way too.Assign
L
8

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.

Louisville answered 14/8, 2014 at 19:43 Comment(1)
Nested fragments is what I am striving with. Probably a true statementAssign
C
2

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();
Coelenteron answered 23/2, 2015 at 18:40 Comment(0)
A
2

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;
}
Anesthesiologist answered 30/5, 2016 at 15:35 Comment(0)
P
1

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();
Pointillism answered 8/11, 2016 at 11:25 Comment(0)
W
0
if(search.getText().toString().trim().isEmpty()) {
    InputMethodManager imm = (InputMethodManager)getSystemService(
              Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(search.getWindowToken(), 0);
}
Wernsman answered 25/2, 2015 at 4:58 Comment(1)
Please edit in an explanation of why/how this code answers the question? Code-only answers are discouraged, because they are not as easy to learn from as code with an explanation. Without an explanation it takes considerably more time and effort to understand what was being done, the changes made to the code, if the code answers the question, etc. The explanation is important both for people attempting to learn from the answer and those evaluating the answer to see if it is valid, or worth up voting.Puddling
V
0

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);
Varicella answered 31/3, 2016 at 9:35 Comment(1)
Bad copy and pasteThar
P
0

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();
Prescript answered 5/7, 2017 at 10:26 Comment(0)
S
-1

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




}
Samos answered 19/8, 2019 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.