Skip to content

Instantly share code, notes, and snippets.

@faruktoptas
Last active November 3, 2023 17:50
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save faruktoptas/e9778e1f718214938b00c2dcd2bed109 to your computer and use it in GitHub Desktop.
Save faruktoptas/e9778e1f718214938b00c2dcd2bed109 to your computer and use it in GitHub Desktop.
A layout that detects Soft Keyboard is visible or not.
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);
}
}
@969rishi
Copy link

how to use this can you explain. I tried to use this but getting error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xx.android/com.xx.android.activity.posts.FullScreenStatusActivity}: android.view.InflateException: Binary XML file line #2 in com.xx.android:layout/post_activity_image_server: Binary XML file line #2 in com.xx.android:layout/post_activity_image_server: Error inflating class KeyboardSensitiveRelativeLayout

@faruktoptas
Copy link
Author

Can you share full trace?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment