Explain Codes LogoExplain Codes Logo

Android - Programmatically Hide/Show Soft Keyboard

android
prompt-engineering
input-method-manager
keyboard-visibility
Nikita BarsukovbyNikita Barsukov·Nov 2, 2024
TLDR

Hide the keyboard by leveraging the hideSoftInputFromWindow method on the InputMethodManager along with the view's windowToken:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { imm.hideSoftInputFromWindow(view.getWindowToken(), 0); }

Show it easily by invoking showSoftInput with the view you are intending to type in:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); }

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:

@Override protected void onResume() { super.onResume(); myEditText.post(()-> { myEditText.requestFocus(); // Roll out the red carpet InputMethodManager mgr = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); mgr.showSoftInput(myEditText, InputMethodManager.SHOW_IMPLICIT); // Make some noise! }); }

Focusing on EditText

Remember to:

  • Set android:focusableInTouchMode="true" on your EditText in 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:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { // It's showtime! imm.showSoftInput(view, 0); // or, undercover time: imm.hideSoftInputFromWindow(view.getWindowToken(), 0); }

Mastering your AndroidManifest.xml

Make your AndroidManifest.xml work for you when soft keyboard orientation changes rock your boat:

<activity android:name=".MyActivity" android:windowSoftInputMode="adjustResize|stateHidden" />

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:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { // Like flipping a coin! imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); // or flip it back: imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); }

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:

editText.post(() -> { editText.requestFocus(); InputMethodManager mgr = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); });

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:

<!-- In your XML layout file --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:focusableInTouchMode="true"> <!-- Other views (co-stars in the grand play) --> </LinearLayout>

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.