How to hide soft keyboard in Android (Kotlin) programmatically

Introduction :

In this post, I will show you how to hide software keyboard in Android programmatically. We will create one activity with one edit text and one button. The soft keyboard will pop up if you click on the edit text. We will write our code to hide the keyboard in the onClick action of the button i.e. the keyboard will hide if you click on the button.

Example program :

We will create one basic Activity with one EditText and one Button as explained above.

  1. Create one Android project with activity MainActivity.kt and layout file activity_main.xml.
  2. In the layout file, add one EditText and Button as like below :
<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraint_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="Hide Keyboard"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>

Run the program and it will produce one screen like below :

Android kotlin hide keyboard

If you click on the EditText that is showing ‘Name’ will pop up the keyboard. Now, we will add one click listener to this button that will hide the keyboard.

Open your MainActivity.kt file and update it as like below :

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Button

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<Button>(R.id.button).setOnClickListener {
            hideSoftKeyboard(it)
        }
    }

    private fun hideSoftKeyboard(view: View) {
        val manager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        manager.hideSoftInputFromWindow(view.windowToken, 0)
    }
}

That’s it. Re-run the program and if you click on the button, it will close the keyboard.

Explanation :

  1. Here, we are getting the System service InputMethodManager.
  2. hideSoftInputFromWindow method hides the soft keyboard. It hides the soft keyboard from the context of the window that is currently accepting input. This method is available since API level 3, so it will work on all devices.

You might also like: