I had the same problem in an application which i wrote some time ago. It is discontiuned now but that's not your question xD.
In fact there is no option to track such a operation, but i've found a great (more or less) solution to add such a functionality. It's quite simple in theory but it's a "hack" too i think.
So what you will need is a custom linear layout which contains your application, or a special region. After that you have to add it a listener. And this will work only in portrait mode.
So here to the code: (i'm sorry but i can't remember the source)
The custom layout:
LinearLayoutThatDetactsSoftwarekeyboard.java (that's the original name of the layout xD)
package com.tundem.people.Layout;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.LinearLayout;
/*
* LinearLayoutThatDetectsSoftKeyboard - a variant of LinearLayout that can detect when
* the soft keyboard is shown and hidden (something Android can't tell you, weirdly).
*/
public class LinearLayoutThatDetectSoftkeyboard extends LinearLayout {
public LinearLayoutThatDetectSoftkeyboard(Context context, AttributeSet attrs) {
super(context, attrs);
}
public interface Listener {
public void onSoftKeyboardShown(boolean isShowing);
}
private Listener listener;
public void setListener(Listener listener) {
this.listener = listener;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = MeasureSpec.getSize(heightMeasureSpec);
Activity activity = (Activity) getContext();
Rect rect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;
int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
int diff = (screenHeight - statusBarHeight) - height;
if (listener != null) {
listener.onSoftKeyboardShown(diff > 128); // assume all soft
// keyboards are at
// least 128 pixels high
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
And how you add the listener:
final LinearLayoutThatDetectSoftkeyboard lltodetectsoftkeyboard = (LinearLayoutThatDetectSoftkeyboard) findViewById(R.id.LinearLayout_SoftKeyboard);
lltodetectsoftkeyboard.setListener(new Listener() {
public void onSoftKeyboardShown(boolean isShowing) {
if (actMode == MODE_SMS && isShowing) {
findViewById(R.id.LinearLayout_bottomnavigationbar).setVisibility(View.GONE);
} else {
findViewById(R.id.LinearLayout_bottomnavigationbar).setVisibility(View.VISIBLE);
}
}
});
The LinearLayout adds a Listener which will be called each time the layoutheight changes by at least 128 pixels. It's a trick and it won't work with keyboards that are smaller than 128 pixels (but i think each keyboard has such a height)
If the LayoutHeight has Changed you will get notified if it's showing now or not.
I hope my answer was useful. Perhaps you find the real source here on StackOverFlow again. I won't steal someones genius so. Credits goes to the unknown person ;)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…