Android Hide Soft Keyboard and Show Soft Keyoard
- Home
- chevron_right
- Solutions
- chevron_right
- Android Hide Soft Keyboard and Show Soft Keyoard
In this tutorial I will present you how to hide the android soft keyboard and how to show it too. You might need to hide the keyboard, if, for example, you will have to put a limit of characters that can be introduced into the EditText and want to hide the keyboard when the number of characters had reached the limit (exactly this scenario I will present you in this tutorial). And also, you might need to make the soft keyboard to pop-up when….hmmm….I don’t know when … but surely you will need it someday 🙂 In this tutorial we will make the soft keyboard to show up when a button is clicked. So let’s start!
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/edit_text" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SHOW KEYBOARD"/> </LinearLayout>
3. Go to MyActivity class and introduce the following code:
package com.example; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; public class MyActivity extends Activity { private Context context; private EditText mEditText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); context = MyActivity.this; mEditText = (EditText)findViewById(R.id.edit_text); Button buttonShowKeyboard = (Button)findViewById(R.id.button); TextWatcher textWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { //To change body of implemented methods use File | Settings | File Templates. } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { // after you enter 4 characters into the EditText the soft keyboard must hide if (charSequence.length() == 4){ // HIDE the keyboard hideTheKeyboard(context, mEditText); } } @Override public void afterTextChanged(Editable editable) { //To change body of implemented methods use File | Settings | File Templates. } }; mEditText.addTextChangedListener(textWatcher); buttonShowKeyboard.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // SHOW the keyboard (you can use this method if you don't care about the physical keyboard) showTheKeyboard(context, mEditText); } }); } /** * Method for hiding the Keyboard * @param context The context of the activity * @param editText The edit text for which we want to hide the keyboard */ public void hideTheKeyboard(Context context, EditText editText){ InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN); } /** * Another method to hide the keyboard if the above method is not working. */ public void hideTheKeyboardSecond(EditText editText){ editText.setInputType(InputType.TYPE_NULL); } /** * Method for showing the Keyboard * @param context The context of the activity * @param editText The edit text for which we want to show the keyboard */ public void showTheKeyboard(Context context, EditText editText){ InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); } /** * Method for showing the Keyboard when a QWERTY (physical keyboard is enabled) * @param context The context of the activity * @param editText The edit text for which we want to show the keyboard */ public void showTheKeyboardWhenQWERTY(Context context, EditText editText){ InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.RESULT_UNCHANGED_SHOWN, 0); } }
NOTE: The first method for showing the keyboard is working only if a physical keyboard is disabled. So if you test it on your emulator you have to disable the physical keyboard like in the pictures below. And the second method is working even if a physical keyboard is enabled or not.
First go where you can edit the emulator. In Intellij, go to Tools – Android – AVD Manager and select your emulator and press the Edit button.