How to detect the if soft keyboard is visible on the screen

81,860 views
Skip to first unread message

Akshay Kulkarni

unread,
Apr 19, 2011, 4:49:40 AM4/19/11
to android-...@googlegroups.com
Hi,

Am trying to detect if the soft keyboard is active from the ImputMethodManager, and the piece of code is as below ;


 InputMethodManager imm  = (InputMethodManager)getActivity().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
       
       
        Log.e(TAG, "Result :"+imm.isActive());   

But the above displays the result as "true" always.

Is there any way to find if the keyboard is active?


Thanks in advance.

Akshay

Akshay Kulkarni

unread,
Apr 20, 2011, 12:15:14 AM4/20/11
to android-...@googlegroups.com
Is there any way to find if the keyboard is active or visible?




Akshay

Shachar Shemesh

unread,
Apr 20, 2011, 12:47:37 AM4/20/11
to android-...@googlegroups.com

On 19/04/11 11:49, Akshay Kulkarni wrote:

Hi,

Am trying to detect if the soft keyboard is active from the ImputMethodManager, and the piece of code is as below ;

I believe you are on the wrong list. You need to try the development support list (I think it's called android-developers - see http://developer.android.com/community/index.html).

Shachar

 InputMethodManager imm  = (InputMethodManager)getActivity().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
       
       
        Log.e(TAG, "Result :"+imm.isActive());   

But the above displays the result as "true" always.

Is there any way to find if the keyboard is active?


Thanks in advance.

Akshay
--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To post to this group, send email to android-...@googlegroups.com.
To unsubscribe from this group, send email to android-platfo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.


-- 
Shachar Shemesh
Lingnu Open Source Consulting Ltd.
http://www.lingnu.com

Mariusz Saramak

unread,
Apr 19, 2011, 3:27:06 PM4/19/11
to android-...@googlegroups.com
Hi.
I hope I don't have right, but it is not possible.

I tried use several ways to check if software keyboard is on:

1) android:configChanges="keyboardHidden|keyboard" properties in activity AndroidManifest.xml, but it never handle onConfigurationChanged. (this properties working only for hardware properties.)

2) I tried onMeasure method. (not working - but

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

please read http://developer.android.com/resources/articles/on-screen-inputs.html because I used only
>> android:windowSoftInputMode=adjustPan
>> <activity name="EditContactActivity"
>>       
android:windowSoftInputMode="stateVisible|adjustResize">
>>        ...
>>   
</activity>
3) I found one way.
http://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html#showSoftInput%28android.view.View,%20int,%20android.os.ResultReceiver%29
Methods hideSoftInputFrom*  return boolean value, true if close keyboard is finished success, then I know if I have closed keyboard or not. If keyboard was opened. (For me this working because I need info only if keyboard was opened.).

Please write information if you will find better way to recognize keyboard state or which solution you choose.
BR.



--

Dianne Hackborn

unread,
Apr 20, 2011, 12:25:21 PM4/20/11
to android-...@googlegroups.com
Why do you want to know it is shown?  The IME being shown has little meaning, since exactly how the IME behaves is up to it -- it may be a transparent overlay and not impact the application, a small strip, or all other kinds of things.

Due to this, the main way you interact with the IME is by setting your softInputMode to be resizeable so when the IME says it wants to occlude part of the screen your app's UI will get resized to take that into account if needed.
--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Quiring, Sam

unread,
Apr 20, 2011, 12:43:55 PM4/20/11
to android-...@googlegroups.com
Knowing if the IME is visible or not is a big deal if you are trying to test apps on the device with automation.  Causing a field to become visible like a user would before typing into it requires extra effort if you suspect/know the IME is being displayed.
When you click in a text field you need to wait for the keyboard to appear before starting to type -- how long do you wait if there is no way to explictily test?
When you hit the back button to ask the IME to go away, how long do you wait to know it is gone?
 
Yes, different IMEs exist, but during testing you can cause a particular one to come up and know its characteristics (transparent overlay, etc.)
 
-Sam


From: android-...@googlegroups.com [mailto:android-...@googlegroups.com] On Behalf Of Dianne Hackborn
Sent: Wednesday, April 20, 2011 9:25 AM
To: android-...@googlegroups.com
Subject: Re: How to detect the if soft keyboard is visible on the screen

Dianne Hackborn

unread,
Apr 20, 2011, 1:49:02 PM4/20/11
to android-...@googlegroups.com
Okay, wanting something for unit testing is very different than for regular app development.

And honestly -- why do you want this?  Why do you care about what happens when you send events to the IME?  Well not that you can anyway.  If you want to test input, you should just directly send input to the app.

Lado Kumsiashvili

unread,
Apr 20, 2011, 2:01:00 PM4/20/11
to android-...@googlegroups.com
Hi.
I need this feature too. In an IME I'd like to use hardware keys, as example volume up and down keys for say to navigate through the text. But if ime is not being used, the keys must be passed through to be handeled further.

Lado
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

Dianne Hackborn

unread,
Apr 20, 2011, 3:27:54 PM4/20/11
to android-...@googlegroups.com
On Wed, Apr 20, 2011 at 11:01 AM, Lado Kumsiashvili <herr...@gmail.com> wrote:
I need this feature too. In an IME I'd like to use hardware keys, as example volume up and down keys for say to navigate through the text. But if ime is not being used, the keys must be passed through to be handeled further.

Sorry, this doesn't make any sense to me.  IMEs don't consume volume keys.  The application wouldn't care whether or not it was shown w.r.t. to volume key dispatching.  Can you explain in more detail what you are trying to do?

Lado Kumsiashvili

unread,
Apr 20, 2011, 6:26:30 PM4/20/11
to android-...@googlegroups.com
Hi.

Well, I'd like to use this feature optionaly to enable KEY_LEFT or KEY_RIGHT via Volume up/down keys. I think it could be quite usefull for some users, which do not have
a D-Pad or hardware keyboard on their devices. Also the camera hardware keyboard could be used say as "Paste" key.

Here is the issue for it in AnySoftKeyboard

http://code.google.com/p/softkeyboard/issues/detail?id=248

Lado

Dianne Hackborn

unread,
Apr 20, 2011, 7:52:31 PM4/20/11
to android-...@googlegroups.com
Wait are you saying you are writing an IME and want to do this key translation?  If so...  um...  it's your IME, you know when it is displayed, you have everything you need to know when to do what.

Akshay Kulkarni

unread,
Apr 20, 2011, 11:45:39 PM4/20/11
to android-...@googlegroups.com
Hi,

There is a method isInputViewShown in InputMethodService which can be used to check if the soft keyboard is visible or not. But can anyone tell how it can be used in the code as unable to get the instance of the InputMethodService and use the isInputViewShown method.

AK

bmjg

unread,
Apr 20, 2011, 6:52:28 PM4/20/11
to android-...@googlegroups.com

The Android platform comes with codecs for e.g. MP3 and AAC and sometimes handset manufacturers/mobile phone providers add e.g. a WMA codec to the platform.

 

Did they also take care of any license fees or is a developer of a audio / video recorder / player or soundtrack enriched game using any of those codecs still required to take care of licensing themselves?

 

Thanks.

 

- Benny

Shri

unread,
May 25, 2011, 6:39:57 PM5/25/11
to android-platform
You can detect change in window size changed in #onMeasure with the
code sample at http://stackoverflow.com/questions/2150078/android-is-software-keyboard-shown.

I was also looking for a way to detect the virtual keyboard because I
wanted to disable a timer to refresh a WebView if the user is entering
text in any of the text fields in the web page. However, I realized I
could use View#setOnKeyListener to listen for actual key presses since
that is what I actually care about - whether the user has typed in
text, not just whether the virtual keyboard is showing. Checking for
the virtual keyboard would be incorrect if the user attached an
external keyboard. I could try to handle all the cases of keyboard
types changes, but checking for key presses seems much more direct.

On Apr 20, 8:45 pm, Akshay Kulkarni <ak.androidm...@gmail.com> wrote:
> Hi,
>
> There is a method isInputViewShown in InputMethodService which can be used
> to check if the soft keyboard is visible or not. But can anyone tell how it
> can be used in the code as unable to get the instance of the
> InputMethodService and use the isInputViewShown method.
>
> AK
>
> On Thu, Apr 21, 2011 at 5:22 AM, Dianne Hackborn <hack...@android.com>wrote:
>
> > Wait are you saying you are writing an IME and want to do this key
> > translation?  If so...  um...  it's your IME, you know when it is displayed,
> > you have everything you need to know when to do what.
>
> > On Wed, Apr 20, 2011 at 3:26 PM, Lado Kumsiashvili <herrl...@gmail.com>wrote:
>
> >>  Hi.
>
> >> Well, I'd like to use this feature optionaly to enable KEY_LEFT or
> >> KEY_RIGHT via Volume up/down keys. I think it could be quite usefull for
> >> some users, which do not have
> >> a D-Pad or hardware keyboard on their devices. Also the camera hardware
> >> keyboard could be used say as "Paste" key.
>
> >> Here is the issue for it in AnySoftKeyboard
>
> >>http://code.google.com/p/softkeyboard/issues/detail?id=248
>
> >> Lado
>
> >> On 20/04/11 21:27, Dianne Hackborn wrote:
>
> >> On Wed, Apr 20, 2011 at 11:01 AM, Lado Kumsiashvili <herrl...@gmail.com>wrote:
>
> >>> I need this feature too. In an IME I'd like to use hardware keys, as
> >>> example volume up and down keys for say to navigate through the text. But if
> >>> ime is not being used, the keys must be passed through to be handeled
> >>> further.
>
> >>  Sorry, this doesn't make any sense to me.  IMEs don't consume volume
> >> keys.  The application wouldn't care whether or not it was shown w.r.t. to
> >> volume key dispatching.  Can you explain in more detail what you are trying
> >> to do?
>
> >>  --
> >> Dianne Hackborn
> >> Android framework engineer
> >> hack...@android.com
>
> >> Note: please don't send private questions to me, as I don't have time to
> >> provide private support, and so won't reply to such e-mails.  All such
> >> questions should be posted on public forums, where I and others can see and
> >> answer them.
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "android-platform" group.
> >> To post to this group, send email to android-...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> android-platfo...@googlegroups.com.
> >> For more options, visit this group at
> >>http://groups.google.com/group/android-platform?hl=en.
>
> >>  --
> >> You received this message because you are subscribed to the Google Groups
> >> "android-platform" group.
> >> To post to this group, send email to android-...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> android-platfo...@googlegroups.com.
> >> For more options, visit this group at
> >>http://groups.google.com/group/android-platform?hl=en.
>
> > --
> > Dianne Hackborn
> > Android framework engineer
> > hack...@android.com

litchie

unread,
Jun 6, 2013, 9:29:03 AM6/6/13
to android-...@googlegroups.com
+1 

On iOS, there is keyboardDidAppear / keyboardDidDisappear, which are very useful, and obviously implemented for a good reason.

To the Google dev,  *STOP* questioning why developers want these, if you are so stupid to understand, then just IMPLEMENT THE DAMN THING!

Владислав Ермолин

unread,
Mar 17, 2014, 4:11:02 AM3/17/14
to android-...@googlegroups.com
Hi. For example Youtube app replaces ActionBarDrawerToggle with up navigation key when keyboard is visible. I don't know the way how to do it without detecting that keyboard is visible. Opening DrawerLayout behind the soft keyboard looks very ugly.

среда, 26 декабря 2012 г., 20:43:10 UTC+2 пользователь apro...@accucode.com написал:
I actually need this functionality so I can preserve the keyboard state when the user is navigating between activities or there is an orientation change, etc. We had to hide the keyboard in the onPause() method due to a bug where if the soft key is open, then you open a new activity that doesn't need the use of the soft key (no fields are present) it opens the new activity as if it was sized with a soft key open. There is an ugly black space in this case. 

Granted if that was fixed I wouldn't even need this functionality however getSoftKeyState() should exist regardless of this bug frankly.


On Saturday, July 14, 2012 7:44:24 PM UTC-6, Kalin Maldjanski wrote:
It is a bit old reply but Dear Dianne, in your answers i mostly see Why do you want to this or that this way or the other way. This is not actually a response.
The fact is that:
1) When IME is active the application can behave differently, especially on KeyEvent.KEYCODE_DPAD_UP/DOWN. And as somebody said this is really important in testing and not only 'unit' testing as you suggested.
2) There is no real straight forward way/method to find out if IME is active or not. you have to monitor various events to be able to find this out.

Alfred Copper

unread,
Jun 19, 2014, 5:34:21 PM6/19/14
to android-...@googlegroups.com
We have a complex app with several activities, some with fragments, some have edit fields and the keyboard is just behaving nuts. Often it stays open when you press the back button, sometimes even when you leave the app. It would be very great, if we could A) check the status of the keyboard, so we can decide if we take action or do adjustments in the interface and B) modify the status of the keyboard without any nasty hacks that don't work half the time.

Also a big problem is that you can't close the keyboard without a reference to a view, so a general close keyboard function would be great.

Please implement the following functionality:

- boolean isSoftKeyboardVisible()
- void hideKeyboard()  // no params
- void showKeyboard() // no params
- void showKeyboard(View v) //focus the view v for editing

Thx for listening <.<

Subhash Bhardwaj

unread,
Oct 26, 2014, 6:22:33 PM10/26/14
to android-...@googlegroups.com
I need to perform text manipulation in the background which I need to stop if the soft keypad shows up.  For this reason, I need to check for the appearance of the keypad during the background processing.  Please add a method to detect the soft keypad.

David Martínez Rodríguez

unread,
Nov 11, 2014, 6:04:07 AM11/11/14
to android-...@googlegroups.com
Dear Dianne,

I subscribe what many people posted here, especially what Alfred Copper said on June, 19th, 2014.

In our case, if the user is typing in an EditText and locks the device, once he gets back the EditText has lost focus. It would be great to know if the soft-keyboard is being shown in order to request focus again on that EditText. We could be requesting focus anyway, but if the keyboard is not shown, users would be getting an awkward experience, with the cursor blinking for no reason.

Kind regards,

David

Piyush Gupta

unread,
Jan 27, 2015, 5:26:34 AM1/27/15
to android-...@googlegroups.com
hello dear, you have to make InputMethodManager object and if InputMethodManager isAcceptingText then means keybord is open else not open.this snipped has tested more then 2 times....


InputMethodManager imm = (InputMethodManager) getActivity()
            .getSystemService(Context.INPUT_METHOD_SERVICE);

    if (imm.isAcceptingText()) {
        Log.d(TAG,"Software Keyboard was shown");
    } else {
        Log.d(TAG,"Software Keyboard was not shown"); 
} 

yanghong shen

unread,
May 31, 2016, 10:50:31 AM5/31/16
to android-platform


在 2015年1月27日星期二 UTC+8下午6:26:34,Piyush Gupta写道:
Reply all
Reply to author
Forward
0 new messages