Detects the pop-up and off of a soft keyboard in Android

Source: Internet
Author: User

The Android system does not provide obvious APIs to listen for and close the soft keyboard. However, in some cases, we still have a way to detect and close the soft keyboard.
I found a good method from stackoverflow. However, this method is only applicable when Android: windowsoftinputmode = "adjustresize" is set for the target activity in manifest.
Adjustresize indicates the activity's main window is always resized to make room for the soft keyboard on screen.
Then we need to set the ID of the Root View of the activity to determine whether to close the dialog box.

Layout sample code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"     android:id="@+id/root_layout"    >     <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:padding="@dimen/padding_medium"        android:text="@string/hello_world"        tools:context=".MainActivity" />     <EditText         android:layout_width="fill_parent"        android:layout_height="wrap_content"         /> </RelativeLayout>

The logic code for the judgment dialog box to appear or close is as follows:

@ Override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); final view activityrootview = findviewbyid (R. id. root_layout); activityrootview. getviewtreeobserver (). addongloballayoutlistener (New ongloballayoutlistener () {private int preheight = 0; @ overridepublic void ongloballayout () {int heightdiff = activityrootview. getrootv Iew (). getheight ()-activityrootview. getheight (); system. out. println ("height differ =" + heightdiff); // reduce the number of duplicate messages sent when data is consistent. Because the ongloballayout method is called multiple times when the input method appears. If (preheight = heightdiff) {return;} preheight = heightdiff; If (heightdiff> 100) {// system. Out. println ("input method shown! "); Toast. maketext (getapplicationcontext (), "keyboard is shown", toast. length_short ). show ();} else {toast. maketext (getapplicationcontext (), "keyboard is hidden", toast. length_short ). show ();}}});}


Give your activity's Root View a known ID, say '@ + ID/activityroot', hook a globallayoutlistener into the viewtreeobserver,
And from there calculate the size diff between your activity's view root and the window size

Set an ID for the root view of your activity, for example, "@ + ID/activityroot". Set a globallayoutlistener on viewtreeobserver. Then calculate the difference between the Root View and window size of your activity
If the interpolation value is greater than 100 (this is a relatively reasonable value), it is considered as a dialog box pop-up. Otherwise, it is hidden in the dialog box.

Link: http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.