Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if keyboard (dis)appears in Android?

I have an EditText and want to give it more lines when the keyboard appears. So i am looking for something like a "OnKeyboardAppearsListener" but can't find it. I think it must exist, but perhaps in a different way...

like image 335
PitjDroid Avatar asked May 11 '11 09:05

PitjDroid


People also ask

How do I know if my keyboard is visible on Android?

Android provides no direct way to determine if the keyboard is open, so we have to get a little creative. The View class has a handy method called getWindowVisibleDisplayFrame from which we can retrieve a rectangle which contains the portion of the view visible to the user.

What is soft keyboard in Android?

The soft keyboard (also called the onscreen keyboard) is the main input method on Android devices, and almost every Android developer needs to work with this component at some point.


1 Answers

You have to @Override onConfigurationChanged to be able to handle runtime changes:

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

    // Checks whether a hardware or on-screen keyboard is available
    if (newConfig.keyboardHidden == Configuration.KEYBOARDHIDDEN_NO) {
        Toast.makeText(this, "Keyboard visible", Toast.LENGTH_SHORT).show();
    } else if (newConfig.keyboardHidden == Configuration.KEYBOARDHIDDEN_YES) {
        Toast.makeText(this, "Keyboard hidden", Toast.LENGTH_SHORT).show();
    }
}

Example taken from here. Take a look here for keyboard related (among others) fields you might want to use.


Edit (RivieraKid): Changed to take account of hard or on-screen keyboard.

like image 102
Joao Avatar answered Sep 29 '22 12:09

Joao