How to Programmatically Close or Hide the Android Soft Keyboard

How to Programmatically Close or Hide the Android Soft Keyboard

22 May 2024 Stephan Petzl Leave a comment Tech-Help

Managing the Android soft keyboard can be a bit challenging, especially when you want to programmatically control its visibility. This guide will help you understand how to close or hide the Android soft keyboard using various methods. Whether you are working with activities, fragments, or dialogs, we’ve got you covered.

Using InputMethodManager

The most common way to hide the keyboard is by using the InputMethodManager. Below is a simple example of how to do this.


    // Check if no view has focus:
    View view = this.getCurrentFocus();
    if (view != null) {  
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    

This code snippet checks if any view currently has focus and, if so, hides the soft keyboard associated with that view.

Kotlin Version


    // Only runs if there is a view that is currently focused
    this.currentFocus?.let { view ->
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
        imm?.hideSoftInputFromWindow(view.windowToken, 0)
    }
    

Utility Method for Activities

If you want a more reusable method, you can create a utility method within your Activity class:


    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);
    }
    

Note that this method works well when called from an Activity. For fragments, you may need a different approach.

Handling Fragments

When dealing with fragments, you should pass the context and the view explicitly:


    public static void hideKeyboardFrom(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    

This method ensures that the keyboard is hidden even if the focus is on a view within a fragment.

Using Window Soft Input Mode

Another method to control keyboard visibility is by setting the window’s soft input mode:


    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    

This can be useful to suppress the keyboard until the user actually touches the EditText view.

Newer AndroidX Approach

For those using AndroidX, you can leverage the WindowInsetsController for a more modern approach:


    import androidx.core.view.ViewCompat;
    import androidx.core.view.WindowInsetsCompat;

    // Hide keyboard
    fun View.hideKeyboard() = ViewCompat.getWindowInsetsController(this)?.hide(WindowInsetsCompat.Type.ime())
    

This approach is backward compatible and integrates well with the latest AndroidX libraries.

Practical Example: Hiding Keyboard in a Dialog

Here’s a practical example of how to hide the keyboard when a dialog is dismissed:


    fun Dialog.hideKeyboard() {
        window?.decorView?.hideKeyboard()
    }
    

This ensures that the keyboard is hidden whenever the dialog is closed.

Enhancing Your Development Workflow with Repeato

Managing the soft keyboard’s visibility is just one aspect of mobile app development. For a comprehensive solution to automated testing, consider using Repeato, a no-code test automation tool for iOS and Android. Repeato allows you to create, run, and maintain automated tests effortlessly, leveraging computer vision and AI for accurate results.

With Repeato, mobile developers can focus more on building great apps rather than spending time on creating and maintaining tests. Repeato also allows non-technical colleagues or QA teams to handle test automation, making the development process more efficient.

Learn more about how Repeato can streamline your testing process by visiting our Getting Started page.

Like this article? there’s more where that came from!