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"?><LinearLayout   Â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
packageorg.neveropen.gfgHideKey   Âimportandroid.content.Context;importandroid.os.Bundle;importandroid.view.View;importandroid.view.inputmethod   Â.InputMethodManager;importandroid.widget.EditText;importandroid.widget.TextView;ÂÂpublicclassMainActivity   ÂextendsAppCompatActivity {   ÂprivateTextView textViewResult;   ÂprivateEditText editTextInput;   Â@Override   ÂprotectedvoidonCreate(       Â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 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:
