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
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity" >
< 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" />
< 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() {
lateinit var hideBtn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
hideBtn = findViewById(R.id.idBtnHide)
hideBtn.setOnClickListener {
val view: View? = this .currentFocus
if (view != null ) {
val inputMethodManager =
getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0 )
Toast.makeText( this , "Key board hidden" , Toast.LENGTH_SHORT).show()
}
}
}
}
|
Now run your project to see the output of it.
Output:
Share your thoughts in the comments
Please Login to comment...