I had to figure it out today so I thought I’ll shared it with the world. So say for whatever reason you need to show or to hide a virtual (soft) keyboard in Android app. It’s not very intuitive but can be easily done:
To show
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)) .showSoftInput(editText, 0);
And to hide:
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)) .hideSoftInputFromWindow(editText.getWindowToken(), 0);
Could not find a suitable section so I written here, how to become a moderator for your forum, that need for this?
This is a private one-man blog, sorry but I can't grant you or anyone else moderator privileges. You can certainly write me if you find any errors or you can edit your own comments
It didnt work for me!!! :(
code:
EditText etName = (EditText) dialog.findViewById(R.id.txtName);
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(etName, 0);
Wats wrong here?
Does not work for me neither. I wrote the code at the end of the onCreate of my activity and the keyboard always show at the start of this activity.
Didn't work for me either. I had to set editor input to NULL, then re-set it correctly upon touch. Like this (where testcaseText is my EditText):
testcaseText.setInputType(InputType.TYPE_NULL); // disable soft input
testcaseText.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
testcaseText.setInputType(InputType.TYPE_CLASS_NUMBER);
testcaseText.onTouchEvent(event); // call native handler
return true; // consume touch even
}
If you are experimenting with this in the emulator, there is a good chance it won't work by default.
When creating a new emulator Virtual Device, you have to make sure to disable the physical keyboard. The soft keyboard won't show when the physical one is available.
Hope this helps!
That's what I'm searching for. Thank you YBSoUpset.
Works perfectly for me!
Wow! I have looked at at least 40 different approaches to this and yours is the first one that worked and is succinct as well!!!
Thanks,
I know this has nothing to do with this, but I have a question. I am trying to use setKeyListener to format the data a user enters. The problem I have is that once a user presses the delete key, the next keytap does not appear.
I am using setText() to reset the display and that is what causes the problem, but without the setText(), there is no need to use the KeyListener.
Mike – this would make a perfect question for http://stackoverflow.com I really recommend it
Thank YOU!!!!!!!!!!!!!!!!!!
I've been looking for this for hours!!!
CHEARS MATE!
If you want the keyboard to show up as soon as the Activity launches, then put android:windowSoftInputMode="stateVisible" in the manifest as an attribute for that Activity. Then set the focus to the EditText that you want the keyboard to show up for:
View exampleView = (View)findViewById(R.id.exampleBox);
exampleView.requestFocus();
Thanks
Thanks a lot!
Had almost given up hope of finding out how to do this and finally found this post!
If you are not getting the solution using this code means, you can force it to show, the code is on this site http://android-codes-examples.blogspot.com/2011/1...
Or Very simple use this :
<activity name="EditContactActivity"
android:windowSoftInputMode="stateVisible|adjustResize">
…
</activity>
Doesn't work on a tablet either.