2

My function, run from the onResume() does not cause the keyboard to appear.

Am I doing it wrong?

private void showKeyboard() {
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(txtSearch, InputMethodManager.SHOW_FORCED);
txtSearch.requestFocus();}
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
  • are you sure onResume() is getting called and not something else like onStart()? Mabye add some toast or print lines and look at logcat? I have found that this is often my problem with stuff like this. – Mike Aug 19 '10 at 00:24
  • @Mike Rather than sprinkling print statements, just read through the activity lifecycle documentation. http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle As you can see, onResume will be called each time the activity comes into focus. – Cheryl Simon Aug 19 '10 at 00:46

2 Answers2

0

The soft keyboard can be tricky sometimes. I belive the last line, txtSearch.requestFocus() is unnecessary, and could actually be screwing things up. By telling the keyboard to SHOW_FORCED on txtSearch you are already telling it to have focus.

Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
  • Hmm. I tried with that line and with out. It has been a tricky test, I have not been able to successfully show the keyboard via code. – Ian Vink Aug 19 '10 at 14:11
0

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); may help you. Note that there are Android devices with hardware-keyboards out there. e.G. GoogleTV devices often have no touchscreen but a hardware-keyboard. Some are even emulating the touchscreen.

This is relevant if you are developing in an emulator:

http://plainoldstan.blogspot.com/2010/09/android-set-focus-and-show-soft.html

"When experimenting I was not actually getting what I wanted until I realized I should have an emulator device with no hardware keyboard:"

Marcus Wolschon
  • 2,550
  • 2
  • 22
  • 28
  • I just confirmed that show_forced works but only on real devices with no hardware-keyboard and on emulators where you EXPLICITYLY set that the emulator has no hardware keyboard. – Marcus Wolschon Jul 11 '11 at 10:28