How to programmatically hide Android soft keyboard Last Updated : 23 Feb, 2021 Summarize Comments Improve Suggest changes Share Like Article Like 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: 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: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" 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> 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: Comment More infoAdvertise with us Next Article How to Disable Dark Mode Programmatically in Android? M madhavmaheshwarimm20 Follow Improve Article Tags : Android Android-Misc Similar Reads 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 2 min read How to Detect Tablet or Phone in Android Programmatically? A Mobile is a portable electronic device that allows you to make calls, send messages, and access the internet, among other functions. A tablet is a mobile computing device with a touchscreen display and typically a larger screen size than a smartphone. Both devices are designed to be portable and a 3 min read How to Disable Dark Mode Programmatically in Android? In this article, we will learn how to disable Dark Mode programmatically in Android. Changing the theme of your app is a simple task that includes changing the code in the XML file. Step by Step Implementation Step 1: Create a New Project in Android Studio Create a new project by clicking on the fil 2 min read 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 programmatica 3 min read How to Quit Android Application Programmatically? When we want to implement an exit AlertDialog in our android application we have to programmatically exit our android application. In this article, we will take a look at How to Quit the Android application programmatically. We will be adding a button and on clicking on that button we will be closin 2 min read How to Quit Android Application Programmatically? When we want to implement an exit AlertDialog in our android application we have to programmatically exit our android application. In this article, we will take a look at How to Quit the Android application programmatically. We will be adding a button and on clicking on that button we will be closin 2 min read Like