How to hide soft keyboard in android studio programmatically

Everyone wants their app to be perfect. To make it Perfect we try our best, and we put in every effort. But sometimes, we need some features that are not familiarized or we can say that you don’t know how to place them in the right way. Don’t worry about helping you out there we are here. In this article, we are going to look at How to hide a soft keyboard in android studio programmatically, to hide an android soft keyboard programmatically in android studio, or hide an android soft keyboard from your app.

Hello everyone, want to know about android studio hide soft keyboard then you are in right place. Bear with me to the end and you will be mastered in hiding soft input from the android app.

Why do we need to hide the keyboard in android?

There are many reasons, and one of them is to make the app more user-friendly. Some want their app to be advanced that is why we do so. For example, if a user finished inputting the data and now he/she wants to send it. The app can do it automatically when they stop writing. Another example is, when someone wants an output after inputting the data, the keyboard stays there automatically even when the app is showing the output, to overcome these situations sometimes we need it.

Few Things that you should know before diving into the main course:

  • This Tutorial is to hide the android soft keyboard on button click, on Focus change, from EditText or TextBox.
  • Logic is the same for all the conditions, you just need to decide on which operation you want to hide or show it.
  • This blog also covers how to show an android soft keyboard or how Toggle (hide/show) an android soft keyboard.
  • Contact us in case you have problems, issues, or doubts regarding the article. We will help you at the earliest.

Let’s deep dive and learn How to hide a soft keyboard in android studio programmatically

To hide a soft keyboard in android you need to follow these steps:

Step 1: Decide when you want to hide the keyboard like on button click, on Focus change, from EditText, or from Textbox.

Step 2: Make a Logic according to your choice which you decide in the first step.

Step 3: Create a Method to hide the keyboard. It is always preferable to make methods for code reusability and easy implementation.

Step 4: Start or initialize the method on the event listener.

Understanding the tutorial in Picture:

hide soft keyboard in android
click the image to enlarge
public void hideKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);                 
        if (imm.isActive()) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

In the public void hideKeyboard(View view) we created a method named (hideKeyboard) which is accepting one View parameter on which we want to hide the android soft keyboard.

Then we imported the class InputMethodManager and gave it a name inm to call it. After that Explicitly defining it that it is InputMethodManager class, so that system doesn’t think it of another thing. Then we are calling system services on Activity to get input method service in this line of code getSystemService(Activity.INPUT_METHOD_SERVICE);.

Here, if a condition is used for checking, if soft input is active or not? means the keyboard is displaying or not on screen. If soft input service (inm) is active then it will hide the keyboard using code on the next line.

To show the soft keyboard in android

we just need to change the one line of code in if condition.

public void showKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        if (!imm.isActive()) {
            imm.showSoftInput(view, 0);
        }
    }

To Toggle (hide/show) the soft keyboard in android

In the Toggle situation, if the keyboard is hidden then it will be visible, if it is visible then switched to hide.

public void toggle() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    }

For example to hide a soft keyboard in android studio programmatically on button click

activity_main2.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hi, I'm Activity 2"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="480dp"
        android:text="Hide Keyboard"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editTextTextPersonName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="120dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity2.java

package com.startechcity.switchactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;

public class MainActivity2 extends AppCompatActivity {
Button hide;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        hide = findViewById(R.id.button3);

        hide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hideKeyboard(hide);
            }
        });
    }

        public void hideKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
    /*public void showKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        if (!imm.isActive()) {
            imm.showSoftInput(view, 0);
        }
    }
    public void toggle() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    }*/
}

Result:

hide soft keyboard in android

Subscribe to my Youtube channel

Please support us, if you like our content. We are giving our 100% to provide you with high-quality content.

Watch the short video on Youtube:

Leave a Comment