Android hide keyboard

In Android the soft keyboard is displayed automatically to allow text entry when an EditText is focused. When you build an application that implements the search functionality, you might want to hide the keyboard automatically after the user clicked the Search button. The hidden keyboard appears again when the EditText is focused. This task can be done by using the InputMethodManager class. The InputMethodManager class has a method called hideSoftInputFromWindow that can be used to hide the keyboard. The hideSoftInputFromWindow() has two arguments: the token of the window that the EditText is attached to and an operating flag (0). The token is returned by the getWindowToken() method of the EditText. You can get an instance of the InputMethodManager by using the getSystemService() method of the current activity. You have to pass Activity.INPUT_METHOD_SERVICE value to this method.


Now to have a workable example application on hiding keyboard, create a new Android project. Then modify the activity_main.xml file to add one EditText and one Button. The EditText allows the user to input text and the Button is clicked to hide the keyboard.
<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"  
   tools:context=".MainActivity"
   android:orientation="horizontal"
   >
  
   <EditText
      android:id="@+id/txt"
      android:layout_width="50dp"
      android:layout_height="wrap_content"
      android:textSize="20sp"
      android:hint="Search"
      android:layout_weight="2"
      android:inputType="text"          
      
      />
   <Button
      android:id="@+id/bt_search"
      android:layout_width="20dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:onClick="searchIt"
      android:text="Search"
      
      />
  
</LinearLayout>


In the MainActivity class, you will add a method called searchIt() as shown below. The searchIt() method is called to hide the soft keyboard when you click the Search button.
package com.example.androidexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
public void searchIt(View view){
//hide keyboard
   EditText edit=(EditText)findViewById(R.id.txt);  
InputMethodManager imm = (InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(), 0);
//place your code to search something here
}
}

Android-hide-keyboard

Posted by: Dara | post date: 09-16-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.