17

is there a way to tell if the softkeyboard is shown in an activity or not?

I tried

InputMethodManager manager = (InputMethodManager) 
getSystemService(getApplicationContext().INPUT_METHOD_SERVICE);
manager.isActive(v)

but isActive returns false only before the first time the keyboard is shown, but if the kb appears and then dismissed, isActive returns true also.

so is there any other method to check for this issue.

thanks

Reno
  • 32,851
  • 11
  • 85
  • 99
Mina Samy
  • 10,466
  • 13
  • 86
  • 149

4 Answers4

13

According to this POST

You cannot detect if soft keyboard is shown or not, but you can indirectly know that a soft key board is shown by knowing that the View of your activity is resized.

Imagine you have a ListView and at the bottom an EditText, you want to go to the bottom of the list when a soft keyboard is shown after user clicks the EditText.

You need to implement a subclass of ListView, then use it in your ListActivity or Activity or View.

public class ThreadView extends ListView {

    public ThreadView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    @Override
    protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {
        super.onSizeChanged(xNew, yNew, xOld, yOld);

        if (yOld > yNew) {
            setSelection(((ListAdapter) getAdapter()).getCount() - 1);
        }
    }
}

Hope this helps

PS. "check Configuration Changes" only works for hand keyboard.

KarenAnne
  • 2,553
  • 1
  • 24
  • 21
DiveInto
  • 1,869
  • 3
  • 23
  • 43
  • I believe this is the only solution to this issue – Mina Samy May 01 '11 at 12:08
  • 1
    This is the way to go, at least on Android 2.2 and to date. It's a real pain that there is no API for detecting a soft keyboard (and for that matter, getting a handle on its slide up animation). Thanks for the answer! – Mason Lee Sep 01 '11 at 01:43
  • Why did I see this only just now?! Spent a lot of time trying to use onMeasure, onLayoutChange, and so on and so forth.. And this is the only solution that worked. The simplest too! Thanks a lot Doraemon, oh I mean, @DiveInto. :p – KarenAnne Mar 10 '14 at 07:08
  • Thanks, you saved my day. Stuck on this one for hours in creating chat Application using recyclerView. – Ali Azhar Aug 11 '15 at 04:58
4

You can detect the state AND coordinates of the software keyboard, using dumpsys shell command.

Because dumpsys requires permission.android.DUMP, which is a system application permission, you have two options: 1. use a rooted device to grant this permission. 2. override the problem using adb as described in my other answer.

Now, run the following command: dumpsys window InputMethod | grep "mHasSurface" to get the data you are looking for.

Community
  • 1
  • 1
Elist
  • 4,985
  • 3
  • 30
  • 66
-1

This is my idea. It is untested.

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);   

        // Checks whether a keyboard is available which is not hard keyboard
        if ((newConfig.keyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO)&&(newConfig.keyboardHidden != Configuration.KEYBOARDHIDDEN_NO)) {
            Toast.makeText(this, "soft keyboard visible", Toast.LENGTH_SHORT).show();
        } else if (newConfig.keyboardHidden == Configuration.KEYBOARDHIDDEN_YES) {
        Toast.makeText(this, "soft keyboard hidden", Toast.LENGTH_SHORT).show();
        }
    }
-2

Please check Configuration Changes for your Activity

This for your AndroidManifest.xml

and this for your Activity class http://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged(android.content.res.Configuration)

You will need to @Override the public method onConfigurationChanged(android.content.res.Configuration) of your Activity to be able to handle, for example, these values:
hardKeyboardHidden,
keyboard,
keyboardHidden

For all possible values check http://developer.android.com/reference/android/content/res/Configuration.html

You will see there something like this:

HARDKEYBOARDHIDDEN_NO   
HARDKEYBOARDHIDDEN_UNDEFINED    
HARDKEYBOARDHIDDEN_YES  
KEYBOARDHIDDEN_NO   
KEYBOARDHIDDEN_UNDEFINED    
KEYBOARDHIDDEN_YES  
KEYBOARD_12KEY  
KEYBOARD_NOKEYS 
KEYBOARD_QWERTY 
KEYBOARD_UNDEFINED

Also there you will be able to read something like this:

public int  hardKeyboardHidden // A flag indicating whether the hard keyboard 
                               // has been hidden.
public int  keyboard    The kind of keyboard attached to the device.
public int  keyboardHidden  A flag indicating whether any keyboard is available.

UPDATE:

Here is a specific example of what I´m talking about:

http://developer.android.com/guide/topics/resources/runtime-changes.html

    
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
    // Checks whether a hardware keyboard is available
    if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
        Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
    } else if (newConfig.hardKeyboardHidden == 
          Configuration.HARDKEYBOARDHIDDEN_YES) {
        Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
    }
}
Jeff Axelrod
  • 25,625
  • 29
  • 137
  • 239
yeradis
  • 4,985
  • 5
  • 23
  • 25
  • 1
    Thanks for response, but it did not capture any change when the kb appeared or disappeared – Mina Samy Mar 28 '11 at 18:00
  • Hi, you should search and example to see that when on your manifest you set android:configChanges="keyboardHidden" this will trigger the onConfigurationChanged method in any Activity you call. If you override the method you can pass new values for your activity or you can fetch current ones. I mean, that using this you can make your application listen for events like keyboard visibility changes and handle them within your Activity. – yeradis Mar 29 '11 at 07:04
  • I just updated the response to add a sample coming from http://developer.android.com/guide/topics/resources/runtime-changes.html you will see there a better information on "Handling the Configuration Change Yourself" – yeradis Mar 29 '11 at 07:10
  • Thanks yeradis, I'll check and tell you later – Mina Samy Mar 29 '11 at 08:54
  • 2
    @yeradis configChanges only works for hard keyboard and orientation change,it won't work for soft keyboard – DiveInto Mar 29 '11 at 12:21
  • in that sample, YES but what about if you replace hardKeyBoardHidden by keyboardHidden, public int keyboardHidden A flag indicating whether ANY keyboard is available. – yeradis Mar 29 '11 at 15:26
  • Thanks guys for response, but the event is not fired unless I specify orientation as a parameter of config changes, which I don't want to cause my entire application has orientation set to portrait (not changed) – Mina Samy Mar 29 '11 at 16:08
  • I also tried the above approach (in Android 2.2), and the soft keyboard does not cause keyboard or keyboardHidden configuration events to fire. – Mason Lee Sep 01 '11 at 01:32