Last active
November 3, 2023 17:50
-
-
Save faruktoptas/e9778e1f718214938b00c2dcd2bed109 to your computer and use it in GitHub Desktop.
A layout that detects Soft Keyboard is visible or not.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class KeyboardSensitiveRelativeLayout extends RelativeLayout { | |
private OnKeyboardShowHideListener listener; | |
public KeyboardSensitiveRelativeLayout(Context context) { | |
super(context); | |
} | |
public KeyboardSensitiveRelativeLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public KeyboardSensitiveRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public KeyboardSensitiveRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
} | |
public void setKeyboardListener(OnKeyboardShowHideListener listener) { | |
this.listener = listener; | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
final int proposedHeight = MeasureSpec.getSize(heightMeasureSpec); | |
final int actualHeight = getHeight(); | |
if (actualHeight != proposedHeight && listener != null) { | |
if (actualHeight > proposedHeight) { | |
listener.onKeyboardShowHide(true); | |
} else { | |
listener.onKeyboardShowHide(false); | |
} | |
} | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
} | |
public interface OnKeyboardShowHideListener { | |
void onKeyboardShowHide(boolean visible); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you share full trace?