Android - Programmatically Hide/Show Soft Keyboard
Hide the keyboard by leveraging the hideSoftInputFromWindow method on the InputMethodManager along with the view's windowToken:
Show it easily by invoking showSoftInput with the view you are intending to type in:
Remember to ensure view is focused for both actions.
Levelling Up Keyboard Control
Let's take your handling of the soft keyboard's visibility to new vistas. A collection of advanced methods for you:
The Runnable Way
A Runnable in onResume ensures keyboard reveal happens spot on:
Focusing on EditText
Remember to:
- Set
android:focusableInTouchMode="true"on yourEditTextin your layout. XML, it does wonders! - Use
myEditText.requestFocus();programatically. Remember, what you focus on, expands!
InputMethodManager - The Keyboard Conductor
Be wary of the null state of our InputMethodManager. Always have a plan B:
Mastering your AndroidManifest.xml
Make your AndroidManifest.xml work for you when soft keyboard orientation changes rock your boat:
Dynamic Adjustments with Custom Views
For custom views, use the onFinishInflate() method to control dynamic keyboard visibility like a boss!
Toggling Keyboard - A Brave New World
Sometimes, all you need is a good ol' toggle:
Waving the Starting Gun and Showing the Keyboard
Don't rush to display the keyboard before your view is race-ready. Always wait for the starter's pistol:
Keyboard Visibility - Clear the Stage!
Sometimes multiple views need to be in the limelight. A layout wherein tapping a non-editable view needs to vanish the keyboard demands new moves:
Now, tapping outside an EditText briefly shines the spotlight on LinearLayout, pushing the keyboard offstage.
Staying Updated
Stay updated about new methods, APIs, and resources - knowledge is obsolete as soon it is acquired, always keep an eye out for the next frontier.
Was this article helpful?