Android Question View overlaying View

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I have a floating keyboard on the screen and want to know if it is covering any part of another field I have on the screen.

Sure I have seen a post of something like this, but I must be searching wrong.

Does anyone have a routine that detects if on field (View) is overlaying any part of another field (View)

BobVal
 

Semen Matusovskiy

Well-Known Member
Licensed User
Let's name Left1, Top1, Width1, Height1 for first view.
The second view - Left2, Top2, Width2, Height2 .

Easy to understand when views do not cover each other.

1) Horizontal direction: (Left1 + Width1) <= Left2 or (Left2 + Width2) <= Left1

2) Vertical direction: (Top1 + Height1) <= Top2 or (Top2 + Height2) <= Top1

So, common formula:
B4X:
If (((Left1 + Width1) <= Left2) Or ((Left2 + Width2) <= Left1)) And (((Top1 + Height1) <= Top2) Or ((Top2 + Height2) <= Top1)) Then ' Do not cover each other

Width / height (for example, Panel1.Width, Button1.Height) are absolute. Left and top are relative.
For example, Panel1 is a child of Activity. Panel1.Left = 50dip. Button1 is a child of Panel1. Button1.Left = 70dip (relative to Panel1). For our calculations we need absolute left (120dip).

To retrieve left/top screen coordinates it's possible to add inline java
B4X:
#If Java
import android.view.View;

public int getScreenLocationX (View view)
    {
    int [] location = new int [2];
    view.getLocationOnScreen (location);
    return (location [0]);
    }

public int getScreenLocationY (View view)
    {
    int [] location = new int [2];
    view.getLocationOnScreen (location);
    return (location [1]);
    }
#End If

and to use (for example)
B4X:
    Sleep (0) ' Wait, if you resize/move inside this subroutine

    Dim jo As JavaObject
    jo.InitializeContext
    Left1 = jo.RunMethod ("getScreenLocationX", Array (Button1)))
    Top1 = jo.RunMethod ("getScreenLocationY", Array (Button1)))
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I had just converted some old C++ code to do this (new I saw or had it somewhere).
Will give what you suggested a look see.

Thanks
 
Upvote 0
Top