Android - Softinput Keyboard open and close programmatically

Last updated Feb 18, 2020

In this post we are going to learn how to handle Softinput Keyboard open and close programmatically.

Step 1: Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project

Step 2: Add below code in xml file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="https://schemas.android.com/apk/res/android"
        xmlns:app="https://schemas.android.com/apk/res-auto"
        xmlns:tools="https://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id = "@+id/rootView"
        tools:context=".MainActivity">
    <EditText
            android:id = "@+id/editText"
            android:layout_width = "match_parent"
            android:layout_height = "wrap_content"
            tools:ignore="MissingConstraints">
        <requestFocus/>
    </EditText>
    <Button
            android:id = "@+id/btnButton"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:text = "Click here to hide"
            android:background="@color/colorPrimary"
            android:textColor="@color/white"
            android:minEms="10"
            android:textSize="18dp"
            android:layout_marginTop="150dp"
            app:layout_constraintLeft_toLeftOf = "parent"
            app:layout_constraintRight_toRightOf = "parent"
            app:layout_constraintTop_toTopOf = "parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

 

Step 3: Update Activity with below code

package com.rrtutors.androidsamples.softinput

import android.graphics.Rect
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.rrtutors.androidsamples.R
import android.widget.Toast
import android.view.ViewTreeObserver
import kotlinx.android.synthetic.main.activity_softinput.*
import android.content.Context
import android.view.inputmethod.InputMethodManager


class SoftinputActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(com.rrtutors.androidsamples.R.layout.activity_softinput)
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener {
            val r = Rect()
            rootView.getWindowVisibleDisplayFrame(r)
            val screenHeight = rootView.getRootView().getHeight()
            val keypadHeight = screenHeight - r.bottom
            if (keypadHeight > screenHeight * 0.15) {
                Toast.makeText(this@SoftinputActivity, "Keyboard is showing", Toast.LENGTH_LONG).show()
            } else {
                Toast.makeText(this@SoftinputActivity, "keyboard closed", Toast.LENGTH_LONG).show()
            }
        })

        btnButton.setOnClickListener(object: View.OnClickListener{
            override fun onClick(v: View?) {
                val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                inputMethodManager!!.hideSoftInputFromWindow(v?.getWindowToken(), 0)
            }

        })
    }
}

 Step 4: Let's run the application

Softinput Keyboard

Softinput Keyboard close

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

1802 Views