Monday 12 December 2011

Android - Show Soft Keyboard When Activity Starts

This morning I needed to have a soft keyboard (AKA, the on-screen keyboard) showing when an activity launched. Why? Well it was login screen for my app, and the input text field would have focus by default so there was no need for the user to tap on the field prior to the keyboard appearing.

I came across code-based solutions which didn't work for me unfortunately.

The solution was thankfully available using a manifest-based approach.


Manifest-Based Solution (This works for me)
Open the manifest file, in the XML view. Find the activity which will show the keyboard automatically upon being shown. Add the line android:windowSoftInputMode="stateVisible" to your XML.


For example:

<activity
     android:name=".activity.PinEntryActivity" 
     android:windowSoftInputMode="stateVisible"
     android:screenOrientation="portrait"
/>



Code-Based Solution (Note, this didn't work for me)
For anyone wondering about the code-based solution, in case it works for you but not me, I was trying these lines of code to launch the on-screen keyboard:
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(pinInputEditText, InputMethodManager.SHOW_IMPLICIT);



// where pinInputEditText should be replaced with your own EditText.

No comments:

Post a Comment