david ortiz steroids oral steroids what do steroids do to your body moon face steroids liver steroids steroids for bronchitis types of steroids

Android Hide Keyboard on Button Click

Last Updated on May 19, 2018

Hi and welcome to another tutorial from Codingdemos, today you will learn how in Android hide keyboard on Button click.

Android hide keyboard

By the end of this tutorial, you will have an app that looks like this. (Large preview)

In this tutorial we will be using the following:

    – Android studio version 3.0.1
    – Android emulator Nexus 5X with API 26
    – Minimum SDK API 16

1- Open up Android Studio and open any project that you have in your computer.

android studio welcome screen

Create new Android Studio project or open existing project. (Large preview)

2- Open up activity_main.xml file, here you need to add Android EditText and Button. You will give Android Button an ID as (btnHide) and a label as (Hide Keyboard).


< EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="text" />

  < Button
    android:id="@+id/btnHide"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hide Keyboard" />

3- Build and run the app and you will have the following output.

Android hide keyboard

Android EditText with Button. (Large preview)

4- Now open up MainActivity.java file, here you will reference both Android EditText and Button.


EditText editText = findViewById(R.id.editText);
Button hide = findViewById(R.id.btnHide);

5- Next you need to make that Button clickable by using setOnClickListener.


hide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                
      }
});

6- Inside Android Button’s onClick method add the following code.


editText.onEditorAction(EditorInfo.IME_ACTION_DONE);

Here you use EditorInfo.IME_ACTION_DONE to indicate that you are done working with EditText and you want to hide Android keyboard.

7- Now build and run the app to see the result.

Android hide keyboard

Android hide keyboard with Button. (Large preview)

8- I hope you find this tutorial helpful and if you have any question please post them in the comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>