The Wayback Machine - https://web.archive.org/web/20240619223021/https://www.geeksforgeeks.org/how-to-programmatically-hide-android-soft-keyboard/
Open In App

How to programmatically hide Android soft keyboard

Last Updated : 23 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 is shown below:-

Approach:

  1. Now add the following code in the activity_main.xml file. The below code adds a textview, edittext and a button in activity_main. The button when clicked invokes the setText function in MainActivity class.

    activity_main.xml




    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="16dp"
        tools:context=".MainActivity">
      
        <TextView
            android:textStyle="bold"
            android:textColor="#219806"
            android:id="@+id/text_view_result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="GeeksForGeeks"
            android:textSize="22sp" />
      
        <EditText
            android:layout_marginTop="20dp"
            android:id="@+id/edit_text_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
      
        <Button
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:onClick="setText"
            android:text="Set Text" />
      
    </LinearLayout>

    
    

  2. Now add the following code in the MainActivity.java file. Here we define the setText and closeKeyboard function. The setText function is invoked when the user clicks the button. It takes the input from edittext and replaces it in the textview. Then it calls the closeKeyboard function and clears the value of edittext. The closeKeyboard function hides the keyboard.

    MainActivity.java




    package org.geeksforgeeks.gfgHideKey
      
        import android.content.Context;
    import android.os.Bundle;
    import android.view.View;
    import android.view.inputmethod
        .InputMethodManager;
    import android.widget.EditText;
    import android.widget.TextView;
      
    public class MainActivity
        extends AppCompatActivity {
        private TextView textViewResult;
        private EditText editTextInput;
      
        @Override
        protected void onCreate(
            Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            textViewResult
                = findViewById(
                    R.id.text_view_result);
            editTextInput
                = findViewById(
                    R.id.edit_text_input);
        }
      
        public void setText(View v)
        {
            String newText
                = editTextInput
                      .getText()
                      .toString();
            textViewResult.setText(newText);
      
            closeKeyboard();
            editTextInput.setText("");
        }
      
        private void closeKeyboard()
        {
            // this will give us the view
            // which is currently focus
            // in this layout
            View view = this.getCurrentFocus();
      
            // if nothing is currently
            // focus then this will protect
            // the app from crash
            if (view != null) {
      
                // now assign the system
                // service to InputMethodManager
                InputMethodManager manager
                    = (InputMethodManager)
                        getSystemService(
                            Context.INPUT_METHOD_SERVICE);
                manager
                    .hideSoftInputFromWindow(
                        view.getWindowToken(), 0);
            }
        }
    }

    
    

Output:



Similar Reads

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 cre
3 min read
How to Disable Soft Keyboard Suggestions in Android?
In Android, we must have observed that in any application, if we are supposed to type in some input, we generally get suggestions for words and sentences from the keyboard. The keyboard helps us type incorrect words or sentences when we start typing. Similarly, it helps auto-correct if mistakenly type wrong spellings. This function is can be change
2 min read
Implement IME Action Buttons in Soft Keyboard in Android using Jetpack Compose
In Android, IME (Input Method Action) Action Button is a key on the Soft Keyboard that represents different actions that can be displayed. In the below image, we have located the IME Action Buttons of two different types. In the below image, you can see the different types of actions that can be displayed in the IME Action Button on the Soft Keyboa
3 min read
How to Invoke Keyboard Programmatically in Android?
Android System by defaults shows an on-screen keyboard when any UI element such as an Input Text element receives focus. For a better experience, a developer can explicitly specify the desired characteristics or any methods to invoke. Desired characteristics could be characters such as allowing only numerals, allowing only alphabets, or any other s
2 min read
How to Change App Icon of Android Programmatically in Android?
In this article, we are going to learn how to change the App Icon of an App on the Button Click. This feature can be used when we have an app for different types of users. Then as per the user type, we can change the App Icon Dynamically. Step by Step Implementation Step 1: Create a New Project To create a new project in Android Studio please refer
3 min read
Different Ways to Programmatically Restart an Android App on Button Click
Sometimes we want to refresh an Activity, but there it is not possible to refresh. In that place, if we restart our app then it automatically gets refreshed. Also, we can use this to restart our app whenever it crashes. Most of the time when we open any app then it fetches all the data currently available. But if in the meantime if more data is upd
3 min read
How to Control Lottie Animations Programmatically in Android?
Lottie is a mobile library for Android and iOS that parses Adobe After Effects animations exported as JSON with Bodymovin and renders them natively on mobile. The Lottie animations are free to use vector animation files. Many famous applications use this such as Uber, Netflix, Google, Airbnb, Shopify, etc. Using Lottie, one can put animations insid
3 min read
How to Programmatically Enable/Disable Wi-Fi in Android?
In Android Phone, it is very much easy to enable/disable WiFi by using the WiFi icon, but have you wondered how to do this task programmatically in Android. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language. Steps for Programmatically
3 min read
How to Programmatically Enable/Disable Bluetooth in Android?
In Android Phone, it is very much easy to enable/disable Bluetooth by using the Bluetooth icon, but have you wondered how to do this task programmatically in Android. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language. Steps for Program
3 min read
How to Adjust the Volume of Android Phone Programmatically from the App?
As stated in the title of this article let's discuss how to adjust the volume of an Android Phone programmatically from the App. Basically, control the volume in the app mean Increase or Decrease the Volume without the Volume Bar UIIncrease or Decrease the Volume with the Volume Bar UIMute or Unmute the device So we are going to discuss all these t
5 min read
Article Tags :
Practice Tags :