17

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?

Simon Lee
  • 22,224
  • 4
  • 38
  • 44
Damir
  • 48,513
  • 89
  • 234
  • 352
  • 1
    Exact duplicate of http://stackoverflow.com/questions/1109022/how-to-close-hide-the-android-soft-keyboard and http://stackoverflow.com/questions/2479504/forcing-the-soft-keyboard-open – Polynomial Nov 14 '11 at 15:28

2 Answers2

34

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
slinden77
  • 3,326
  • 2
  • 35
  • 34
1

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();
Community
  • 1
  • 1
josephus
  • 8,148
  • 1
  • 35
  • 53