Android Question How can i find the value of Gravity

Scantech

Well-Known Member
Licensed User
Longtime User
B4X:
'    Gravity.NO_GRAVITY                = 0
'    Gravity.CENTER_HORIZONTAL        = 1
'    Gravity.LEFT                    = 3
'    Gravity.RIGHT                    = 5
'    Gravity.CENTER_VERTICAL            = 16
'    Gravity.CENTER                    = 17
'    Gravity.TOP                        = 48
'    Gravity.BOTTOM                    = 80
'    Gravity.FILL                    = 119

    Dim x As Int
   
    x = Gravity.BOTTOM + Gravity.LEFT


'how can i find x that contains Gravity.Left?
 

Cableguy

Expert
Licensed User
Longtime User
I dont understand the question!
what do you want to know?
X? or Gravity.Left?

Gravity.left = x - Gravity.botom
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
if you want to check if your view has gravity.left set you can use

B4X:
If Bit.And(l.Gravity,3)=3 then

or maybe

B4X:
If Bit.And(l.Gravity,gravity.left)=gravity.left then
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
if you want to check if your view has gravity.left set you can use

B4X:
If Bit.And(l.Gravity,3)=3 then

or maybe

B4X:
If Bit.And(l.Gravity,gravity.left)=gravity.left then

What is l?

You probably meant this
B4X:
If Bit.And(x, 3) = 3 Then

This will not work because the Gravity values is not bitmapped in 1,2,4,8,16,32,64,128.

Example why it will not work.
B4X:
    Dim x As Int
'   
    x = Gravity.BOTTOM + Gravity.FILL
    If Bit.And(x, 3) = 3 Then
        Log("YES")
    End If

It returns Yes.
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
I dont understand the question!
what do you want to know?
X? or Gravity.Left?

Gravity.left = x - Gravity.botom

That is not going to work. The x can have any of the Gravity combination and I want to detect Gravity.left(3) is being used.

I don't think there is a mathematical equation for this problem. I need to find other ways. Value 17 can represent (Gravity.Center) or (Gravity.Center_Vertical + Gravity.Center_Horizontal) and that example is why it will be impossible to detect which one is being used.
 
Last edited:
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
I just realized some of those gravity properties can not be mixed together. So If I want to detect Gravity.Left I can perhaps use this.

B4X:
    Select Case x
        case 3, 19, 51, 83: log("YES") 
    End Select

3 = Gravity.Left
19 = Gravity.Left + Gravity.Center_Vertical
51 = Gravity.Left + Gravity.Top
83 = Gravity.Left + Gravity.Bottom

Those are the only combinations allowed?
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I would say yes, since some gravity values affect both vertical as horizontal behaviours, like gravity.center

I would say that any combined value is unique and therefore no 2 different combinations can give the same value
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Supposing that the one who sets gravity does it "correctly", you can have
B4X:
 Dim gravityLeftSelected As Boolean=(x mod 16=3)
 
Upvote 0
Top