Skip to content

Instantly share code, notes, and snippets.

@juliomarcos
Created March 7, 2015 04:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliomarcos/8ca307cd7eca607c8547 to your computer and use it in GitHub Desktop.
Save juliomarcos/8ca307cd7eca607c8547 to your computer and use it in GitHub Desktop.
Keyboard "Open/Closed Event dispatcher" LinearLayout
package corporation.view;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
/**
* Created by juliorodrigues on 07/03/15.
*/
public class KeyboardAwareLinearLayout extends LinearLayout {
private OnKeyboardChangedListener listener;
public KeyboardAwareLinearLayout(Context context) {
super(context);
}
public KeyboardAwareLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public KeyboardAwareLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setOnKeyboardChangedListener(OnKeyboardChangedListener listener) {
this.listener = listener;
}
public interface OnKeyboardChangedListener {
public void onClosed();
public void onOpened();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (listener == null) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
return;
}
final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
final int actualHeight = getHeight();
if (actualHeight > proposedheight){
listener.onOpened();
} else {
listener.onClosed();
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment