Show or Hide Soft Keyboard in Android Application And More

Android’s API is very confusing when it comes to handling Android soft keyboard, to hide or show. But here I have compiled the ways to hide or show the keyboard programatically.

Let us quickly see how we can do that in this post. And not only hide or show keyboard but we are also going to learn something new.

Creating KeyboardUtil class

Let us first create a utility class to show or hide the soft keyboard from anywhere.

public class KeyboardUtil { /** * * @param activity */ public static void hideKeyboard(Activity activity) { InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); View view = activity.getCurrentFocus(); if (view == null) { view = new View(activity); } imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } /** * * * @param activity the activity instance */ public static void showKeyboard(Activity activity) { View view = activity.getCurrentFocus(); if (view == null) view = new View(activity); Context context = view.getContext(); InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); mgr.showSoftInput(view, InputMethodManager.SHOW_FORCED); } }
Code language: Java (java)

Now this class will help us show or hide the soft keyboard on Android device.

Also Read: Android Data Binding: Replace findViewById() And More

TECHENUM

Hiding the soft keyboard in Android

You will see the android keyboard when you change the activity or fragment. When in reality the keyboard should be hidden once you change the fragment or whenever something hapens.

We have already created a utility class and now we are going to use it.

// just add this line whenever you want to hide the keyboard // this = activity instance KeyboardUtil.hideKeyboard(this); // if you are in a fragment you can do KeyboardUtil.hideKeyboard(getActivity());
Code language: Java (java)

Now that we have successfully hidden the keyboard. Let us show it programatically.

Showing the soft keyboard in Android

To show the keyboard programatically all we have to do is add the code below.

The keyboard will be shown taking in the focused object in the passed Activity.

// just add this line whenever you want to show the keyboard // this = activity instance KeyboardUtil.showKeyboard(this); // if you are in a fragment you can do KeyboardUtil.showKeyboard(getActivity());
Code language: JavaScript (javascript)

Also Read: Android Notification Manager: Create Notification in Android

TECHENUM

Disabling keyboard on launching activity

Sometimes when we launch an activity with the EditText on it. We will see the keyboard popup immediately.

But we can easily disable that from the AndroidManifest. All you have to do is add the following line in the desired Activity.

<activity ... android:windowSoftInputMode="stateHidden|adjustResize"> </activity>
Code language: HTML, XML (xml)

You have to add the highlighted line. Or the next option available to you is invoke the KeyboardUtil.showKeyboard(this); method on last line of onCreate() method.

Also Read: Learn Android Room Persistence Library for SQLite Database

TECHENUM

That is all we have to do to show or hide the soft input keyboard in android.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *