How to make the keyboard show / hide?

I tried to show the keyboard after inflating LinearLayout and calling setContentView, for example:

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

This did not work. I also tried this:

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

But that didn't work either. How to make the keyboard show / hide? What have I done wrong?

+7
source share
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 
+30
source

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

 yourEditText.requestFocus(); 
+1
source

All Articles