Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force keyboard to show/hide? [duplicate]

I tried to show keyboard after I inflate LinearLayout and call setContentView like:

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(etContent, InputMethodManager.SHOW_FORCED);
getContent.requestFocus();

It didn't work. I also tried this:

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

But it also didn't work. How can I force the keyboard to show/hide? What did I do wrong?

like image 275
Damir Avatar asked Nov 14 '11 15:11

Damir


People also ask

How do I hide my keyboard after typing?

This can be anything you wish. Tap the back button on your Android. It's the left-pointing arrow button at the bottom of the screen, either at the bottom-left or bottom-right corner. The keyboard is now hidden.

How do I force close an Android keyboard?

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases you will want to pass in InputMethodManager.


2 Answers

This should work

public class KeyBoard {

    public static void toggle(Activity activity){
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        if (imm.isActive()){
            imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); // hide
        } else {
            imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY); // show
        }
    }//end method
}//end class
like image 137
slinden77 Avatar answered Sep 22 '22 15:09

slinden77


this link is clear about hiding the soft keyboard. to show it you can use a hack - create an EditText anywhere in your layout, layout_width and layout_height=0dip, and in onCreate do

yourEditText.requestFocus();
like image 41
josephus Avatar answered Sep 19 '22 15:09

josephus