Determine if the view is partially off-screen

I have a view that is being added dynamically. Sometimes the view is only partially visible, since its lower part is on the screen. In this case, I want to move the view up. However, I do not know how to determine if it is off-screen or not, and how much.

Edit: The context of this problem is that I have an edittext that I want to show a custom soft keyboard next to it. Here is the code that I use to move the user keyboard.

 public void moveKeyboardNextToView(View view) {
    int[] location = new int[]{0, 0};
    view.getLocationInWindow(location);
    Rect r = new Rect();
    view.getGlobalVisibleRect(r);

    int height = r.bottom - r.top;
    int newTop = r.bottom - view.getHeight() / 2 - this.mKeyboardView.getHeight() / 2;
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) this.mKeyboardView.getLayoutParams();
    params.setMargins(0, newTop, 0, 0);
    this.mKeyboardView.setLayoutParams(params);
    this.mKeyboardView.invalidate();

    this.mKeyboardView.post(new Runnable() {
        @Override
        public void run() {
            int[] location = new int[2];
            Rect r2 = new Rect();
            mKeyboardView.getLocalVisibleRect(r2);
            double abc = r2.bottom;
        }
    });
}
+4
source share
2 answers

The way I do this is to compute it with the following information:

, , . , (, dpis ..) .

+2

Source: https://habr.com/ru/post/1543527/


All Articles