
- Android Basics
- Android - Home
- Android - Overview
- Android - Environment Setup
- Android - Architecture
- Android - Application Components
- Android - Hello World Example
- Android - Resources
- Android - Activities
- Android - Services
- Android - Broadcast Receivers
- Android - Content Providers
- Android - Fragments
- Android - Intents/Filters
- Android - User Interface
- Android - UI Layouts
- Android - UI Controls
- Android - Event Handling
- Android - Styles and Themes
- Android - Custom Components
- Android Advanced Concepts
- Android - Drag and Drop
- Android - Notifications
- Location Based Services
- Android - Sending Email
- Android - Sending SMS
- Android - Phone Calls
- Publishing Android Application
- Android Useful Examples
- Android - Alert Dialoges
- Android - Animations
- Android - Audio Capture
- Android - AudioManager
- Android - Auto Complete
- Android - Best Practices
- Android - Bluetooth
- Android - Camera
- Android - Clipboard
- Android - Custom Fonts
- Android - Data Backup
- Android - Developer Tools
- Android - Emulator
- Android - Facebook Integration
- Android - Gestures
- Android - Google Maps
- Android - Image Effects
- Android - ImageSwitcher
- Android - Internal Storage
- Android - JetPlayer
- Android - JSON Parser
- Android - Linkedin Integration
- Android - Loading Spinner
- Android - Localization
- Android - Login Screen
- Android - MediaPlayer
- Android - Multitouch
- Android - Navigation
- Android - Network Connection
- Android - NFC Guide
- Android - PHP/MySQL
- Android - Progress Circle
- Android - ProgressBar
- Android - Push Notification
- Android - RenderScript
- Android - RSS Reader
- Android - Screen Cast
- Android - SDK Manager
- Android - Sensors
- Android - Session Management
- Android - Shared Preferences
- Android - SIP Protocol
- Android - Spelling Checker
- Android - SQLite Database
- Android - Support Library
- Android - Testing
- Android - Text to Speech
- Android - TextureView
- Android - Twitter Integration
- Android - UI Design
- Android - UI Patterns
- Android - UI Testing
- Android - WebView Layout
- Android - Wi-Fi
- Android - Widgets
- Android - XML Parsers
- Android Useful Resources
- Android - Questions and Answers
- Android - Useful Resources
- Android - Discussion
How to capture virtual keyboard show/hide events in Android ?
Introduction
Many times in android applications a keyboard has popped when the user clicks on any text input field. In some cases we have to restrict users to enter the data into the text input field via a menu. In that case we have to hide the keyboard for that text input field explicitly. In this article we will take a look on How to capture show/hide events for the virtual keyboard within our Android application.
Implementation
We will be creating a simple application in which we will be displaying a simple text view in which we will be displaying the heading of our application. After this text view we will be creating an edit text which we will be using to open the virtual keyboard within our application. On opening and closing of the keyboard we will be displaying the toast message. We will be following a step by step guide to implement this.
Step 1 : Creating a new project in Android Studio
Navigate to Android studio as shown in below screen. In the below screen click on New Project to create a new Android Studio Project.

After clicking on New Project you will get to see the below screen.

Inside this screen we have to simply select Empty Activity and click on Next. After clicking on next you will get to see the screen below.

Inside this screen we have to simply specify the project name. Then the package name will be generated automatically.
Note − Make sure to select the Language as Java.
After specifying all the details click on Finish to create a new Android studio project.
Once our project has been created we will get to see 2 files which are open i.e activity_main.xml and MainActivity.java file.
Step 3 : Working with activity_main.xml.
Navigate to activity_main.xml. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>activity_main.xml to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/idCLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <!-- text view for displaying heading of the application --> <TextView android:id="@+id/idTVHeading" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/idEdtMessage" android:layout_margin="10dp" android:padding="5dp" android:text="Detect Show/Hide events for Virtual Keyboard" android:textAlignment="center" android:textAllCaps="false" android:textColor="@color/black" android:textSize="18sp" android:textStyle="bold" /> <!-- creating an edit text to open and close virtual keyboard --> <EditText android:id="@+id/idEdtMessage" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="10dp" android:hint="Enter your message" /> </RelativeLayout>
Explanation − In the above code we are creating a Relative layout as a root layout and inside that we are creating our TextView which is used to display the heading of our application. After this text view we will be creating an edit text on touching this edit text the keyboard will be opened.
Step 4 : Working with MainActivity.java file
Navigate to MainActivity.java. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>MainActivity.java to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.
package com.example.androidjavaapp; import android.graphics.Rect; import android.os.Bundle; import android.view.ViewTreeObserver; import android.widget.RelativeLayout; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { // on below line creating a variable for button and edit text. private RelativeLayout relativeLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // on below line we are initializing variables for relative layout. relativeLayout = findViewById(R.id.idCLayout); // on below line we are calling tree observer to get the tree. relativeLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // on below line we are creating a variable for rect Rect rect = new Rect(); // on below line getting frame for our relative layout. relativeLayout.getWindowVisibleDisplayFrame(rect); // on below line getting screen height for relative layout. int screenHeight = relativeLayout.getRootView().getHeight(); // on below line getting keypad height. int keypadHeight = screenHeight - rect.bottom; // on below line we are checking if keypad height is greater than screen height. if (keypadHeight > screenHeight * 0.15) { // displaying toast message as keyboard showing. Toast.makeText(MainActivity.this, "Keyboard is showing", Toast.LENGTH_LONG).show(); } else { // displaying toast message as keyboard closed. Toast.makeText(MainActivity.this, "keyboard closed", Toast.LENGTH_LONG).show(); } } }); } }
Explanation − In the above code firstly we are creating variables for our Relative Layout. Inside the onCreate method we are initializing the listview variable with the id which we have given in our activity_main.xml file.
After that we are adding a Global Layout Listener for our relative layout. Inside the Global Layout method we are creating a variable for rect. Then we are adding a condition to check whether the keypad height is greater than screen height. If it is greater means that the keyboard is visible in that case we are displaying a message as the keyboard is visible. In the else condition we are displaying a toast message as a keyboard hidden.
After adding the above code now we have to simply click on the green icon in the top bar to run our application on a mobile device.
Note − Make sure you are connected to your real device or emulator.
Output

Conclusion
In the above tutorial we have taken a look on How we can capture the virtual keyboard show and hide within our android application.
- Related Articles
- How to close or hide the virtual keyboard on Android?
- How to check visibility of virtual keyboard on Android?
- How to hide soft KeyBoard on android after clicking outside edittext?
- How to capture events on Tkinter child widgets?
- How to hide a soft keyboard on android after clicking outside EditText using Kotlin?
- How to show soft Keyboard based on Android EditText is focused?
- How to show a Soft Keyboard based on Android EditText focused using Kotlin?
- How do you hide the onscreen keyboard in iOS App?
- How to hide the keyboard in Swift by pressing the return key?
- How to Show and hide a View with a slide up/down animation in android?
- How do I hide and show a menu item in the Android ActionBar?
- How to hide action bar in android?
- How to hide status bar in Android?
- How to hide/show HTML elements in JavaScript?
- How to show and hide widgets in Tkinter?
