Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.pedro.example;
- import android.content.Context;
- import android.support.constraint.ConstraintLayout;
- import android.util.AttributeSet;
- import android.util.Log;
- /**
- * A common ConstraintLayout with a listener callback that notifies when the soft keyboard
- * is shown or hidden, as for now there is no such feature in the android framework
- */
- public class SoftKeyboardAwareConstraintLayout extends ConstraintLayout {
- private SoftKeyboardVisibilityChangeListener callback;
- private boolean isKeyboardShown;
- public SoftKeyboardAwareConstraintLayout(Context context) {
- super(context);
- }
- public SoftKeyboardAwareConstraintLayout(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public SoftKeyboardAwareConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- if(callback != null) {
- final int proposedHeight = MeasureSpec.getSize(heightMeasureSpec);
- final int actualHeight = getHeight();
- if (actualHeight > proposedHeight) {
- if(!isKeyboardShown) {
- Log.d("KEYBOARD_EVENTS", "Keyboard is shown");
- callback.onSoftKeyboardShow();
- isKeyboardShown = true;
- }
- } else {
- if(isKeyboardShown) {
- Log.d("KEYBOARD_EVENTS", "Keyboard is hidden");
- callback.onSoftKeyboardHide();
- isKeyboardShown = false;
- }
- }
- }
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- }
- public void setCallback(SoftKeyboardVisibilityChangeListener callback) {
- this.callback = callback;
- }
- public interface SoftKeyboardVisibilityChangeListener {
- void onSoftKeyboardShow();
- void onSoftKeyboardHide();
- }
- }
Add Comment
Please, Sign In to add comment