1

I have read many threads, but I was not very clear. How to capture the closing of the keyboard?

1 Answers1

3

you can do the following to detect if the hardware key board is shown/hidden

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

// if 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();
}
}

you can do the following Work around to detect if the virtual key board is shown/hidden

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
final int actualHeight = getHeight();

if (actualHeight > proposedheight){
    // Keyboard is shown
} else {
    // Keyboard is hidden
}

super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 }

To see how to implement , check this answer https://stackoverflow.com/a/7423586/864955

Community
  • 1
  • 1
rfsk2010
  • 8,571
  • 4
  • 32
  • 46