How to Close or Hide Android Soft Keyboard with Kotlin?
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: