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
<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="16dp"tools:context=".MainActivity"><TextViewandroid: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"/><EditTextandroid:layout_marginTop="20dp"android:id="@+id/edit_text_input"android:layout_width="match_parent"android:layout_height="wrap_content"/><Buttonandroid: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>chevron_rightfilter_none - 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
packageorg.geeksforgeeks.gfgHideKeyimportandroid.content.Context;importandroid.os.Bundle;importandroid.view.View;importandroid.view.inputmethod.InputMethodManager;importandroid.widget.EditText;importandroid.widget.TextView;publicclassMainActivityextendsAppCompatActivity {privateTextView textViewResult;privateEditText editTextInput;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textViewResult= findViewById(R.id.text_view_result);editTextInput= findViewById(R.id.edit_text_input);}publicvoidsetText(View v){String newText= editTextInput.getText().toString();textViewResult.setText(newText);closeKeyboard();editTextInput.setText("");}privatevoidcloseKeyboard(){// this will give us the view// which is currently focus// in this layoutView view =this.getCurrentFocus();// if nothing is currently// focus then this will protect// the app from crashif(view !=null) {// now assign the system// service to InputMethodManagerInputMethodManager manager= (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);manager.hideSoftInputFromWindow(view.getWindowToken(),0);}}}chevron_rightfilter_none
Output:
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Different ways to hide Action bar in Android with Examples
- Android | AdMob Banner Ads for Android Studio
- Android | Android Application File Structure
- Android | AdMob Interstitial Ads for Android Studio
- Android | How to Create/Start a New Project in Android Studio?
- Android | Running your first Android app
- Android | How to add Radio Buttons in an Android Application?
- Android | How to Request permissions in Android Application?
- Difference between Android 1.1 and Android 4.0.1
- Difference between Android 1.1 and Android 2.3.4
- Difference between Android 1.0 and Android 1.1
- Difference between Android 1.0 and Android 7.1
- Difference between Android 1.0 and Android 2.3
- Difference between Android 1.0 and Android 2.2
- Difference between Android 1.0 and Android 1.6
- Difference between Android 1.0 and Android 2.1
- Difference between Android 1.0 and Android 2.0.1
- Difference between Android 1.0 and Android 2.0
- Difference between Android 1.0 and Android 1.5
- Difference between Android 1.0 and Android 2.3.3
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

