Android Question Rect in a rect

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Is there a routine that will check if a rectangle is within another rectangle?

I have a popup keyboard and want to make sure it does not popup over the field the editing will occur in

BobVal
 

Roycefer

Well-Known Member
Licensed User
Longtime User
You should check if any of the lines that make the edges of Rect1 intersect any of the lines that make the edges of Rect2. This is especially easy in the case of screen rectangles because all the lines are parallel to either the x or y axis.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
if you are sure the text field is always above the keyboard you could use something simple as this

B4X:
keyboard.top=editField.top+editfield.height+extraspacing
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I only want to move the keyboard if after the user tabs to another field if the keyboard is anyway covering the field.

Otherwise I want to leave the keyboard alone (has a drag tab to let user move)

I am going to try and use the reflector library to use the java command rect (http://developer.android.com/reference/android/graphics/Rect.html)

There is an intersect sub routine

UPDATE:

I found the code for rectangle: http://developer.classpath.org/doc/java/awt/Rectangle-source.html

(a lot of useful functions there) and converted the intersects code to B4A

B4X:
Public  Sub Intersects(Field1 As Rect, Field2 As View) As Boolean
       If Field1.Right > 0 And Field1.Bottom > 0 And Field2.Width > 0 And Field2.Height > 0    And     _
        Field1.Left  < (Field2.Left + Field2.Width) And Field1.Right > Field2.Left        And  _
        Field1.Top  < (Field2.Top  + Field2.Height) And Field1.Bottom > Field2.Top      Then Return True
       
       Return False
End Sub

Code to test below

B4X:
Dim RelativeY As Int = GetRelativeTop(FieldEntry.Field)
Dim RelativeX As Int = GetRelativeLeft(FieldEntry.Field)

Dim FieldRect As Rect
     
FieldRect.Initialize(RelativeX, RelativeY, (RelativeX + FieldEntry.Field.Width), (RelativeY + FieldEntry.Field.Height))
     
Log("Intersects: " &Intersects(FieldRect, sID_KB_Frame))

This seems to be working fine

BobVal
 
Last edited:
Upvote 0

sorex

Expert
Licensed User
Longtime User
why don't you use the focuschanged event and then use a modified version of the code above?

if edittext.top+edittext.height+keyboad.height > 100%Y then you set the top to 0 or edittext.top-keyboard.height.
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Because I am not using EditText fields. I am using Labels - do not like the way edittext fields look or work.
Also I do not believe this should be a field problem but rather a keyboard problem

Thanks for the help. The intersects code works just fine
 
Last edited:
Upvote 0
Top