How to Close or Hide Android Soft Keyboard with Kotlin?
Last Updated :
21 Dec, 2022
Many times there is a need in which we have to close the android soft keyboard programmatically when the user has typed some text within the edit text. This type of functionality is generally required in four digits pins in which after users type 4 digit pin the keyboard will be closed programmatically within the android application. We will be creating a sample application in which we will be displaying an edit text and a button. When the user clicks on the button we will be hiding our keyboard. In this article, we will take a look at How to Close/Hide the Android soft keyboard programmatically in Android.
Note: If you want to implement the same operation using Java then check out the following article: How to programmatically hide Android soft keyboard
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--button to hide our key board-->
<Button
android:id="@+id/idBtnHide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/idEdtMsg"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:text="Hide Key Board"
android:textAllCaps="false" />
<!--edit text to display key board-->
<EditText
android:id="@+id/idEdtMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:hint="Enter your message" />
</RelativeLayout>
Step 3: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.gtappdevelopers.bottomnavbar
import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
// on below line creating variables
lateinit var hideBtn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// on below line wea re initializing our variables.
hideBtn = findViewById(R.id.idBtnHide)
// on below line adding click listener for button.
hideBtn.setOnClickListener {
// on below line getting current view.
val view: View? = this.currentFocus
// on below line checking if view is not null.
if (view != null) {
// on below line we are creating a variable
// for input manager and initializing it.
val inputMethodManager =
getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
// on below line hiding our keyboard.
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0)
// displaying toast message on below line.
Toast.makeText(this, "Key board hidden", Toast.LENGTH_SHORT).show()
}
}
}
}
Now run your project to see the output of it.
Output:
Similar Reads
How to programmatically hide Android soft keyboard In this article, we will learn about how to hide soft keyboard programmatically. The keyboard generally hides but there are certain instances when it does not hide. So for better user experience, the keyboard is hidden programmatically. Without using below approach, the default response of the app i
2 min read
How to Check the Visibility of Software Keyboard in Android? In some cases, we should determine if the keyboard is displayed or not in a given activity. In this example, we can see how to verify the presence of a virtual keyboard on Android. With the release of Android 11 preview 2 and here, it appears that platform behavior for listening to keyboard changes
2 min read
How to Convert Kotlin Code to Java Code in Android Studio? Java programming language is the oldest and most preferred language for Android app development. However, during Google I/O 2017, Kotlin has been declared as an official language for Android development by the Google Android Team. Kotlin has gained popularity among developers very quickly because of
3 min read
How to Convert Java Code to Kotlin Code in Android Studio? In Google I/O 2017, Kotlin has been declared as an official language for Android app development. This language gains popularity among developers very quickly because of its similarities as well as interoperable with Java language. One can mix code of Java and Kotlin while designing an Android proje
4 min read
How to Create Option Menu in Android using Kotlin? In this article, we will learn how to create an options menu in the Android app using Kotlin. To have an options menu in an Activity, we need to create a new menu XML file and inflate it using menuInflator.inflate( ) method. In menu.xml we will design the options menu as the requirement of the app.
2 min read