Change the parent layout to Relative layout
public class SizeNotifierRelativeLayout extends RelativeLayout { private Rect rect = new Rect(); public SizeNotifierRelativeLayoutDelegate delegate; public abstract interface SizeNotifierRelativeLayoutDelegate { public abstract void onSizeChanged(int keyboardHeight); } public SizeNotifierRelativeLayout(Context context) { super(context); } public SizeNotifierRelativeLayout(android.content.Context context, android.util.AttributeSet attrs) { super(context, attrs); } public SizeNotifierRelativeLayout(android.content.Context context, android.util.AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); if (delegate != null) { View rootView = this.getRootView(); int usableViewHeight = rootView.getHeight() - AndroidUtilities.statusBarHeight - AndroidUtilities.getViewInset(rootView); this.getWindowVisibleDisplayFrame(rect); int keyboardHeight = usableViewHeight - (rect.bottom - rect.top); delegate.onSizeChanged(keyboardHeight); } }
Further in your activity or fragment contains the following codes
private SizeNotifierRelativeLayout sizeNotifierRelativeLayout; View rootView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); rootView = view.findViewById(R.id.root); sizeNotifierRelativeLayout = (SizeNotifierRelativeLayout) view.findViewById(R.id.chat_layout); sizeNotifierRelativeLayout.delegate = this; rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Rect r = new Rect(); rootView.getWindowVisibleDisplayFrame(r); int screenHeight = rootView.getRootView().getHeight(); int keypadHeight = screenHeight - r.bottom; Log.d("Height", "keypadHeight = " + keypadHeight); if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height. // keyboard is opened if (sizeNotifierRelativeLayout != null) { sizeNotifierRelativeLayout.setPadding(0, 0, 0, keypadHeight - 200); } } else { // keyboard is closed if (sizeNotifierRelativeLayout != null) { sizeNotifierRelativeLayout.post(new Runnable() { public void run() { if (sizeNotifierRelativeLayout != null) { sizeNotifierRelativeLayout.setPadding(0, 0, 0, 0); } } }); } try { } } catch (NullPointerException e) { e.printStackTrace(); System.out.print(e.getMessage()); } } } }); }
prithiviraj
source share