Android Question Get Elevation of view [Solved]

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All, today's dumb question.

I am seeking to find the elevation of buttons, I have searched but can't find anything to get elevation but SetElevation gives a bit of a clue.
Below are a couple of attempts using javaobject and reflection libraries without success.
Question 1. Is this possible in B4A? Java has a GetElevation but I might be looking at a dead end in B4A.
Question 2. If it is possible, can someone please point at what I have done wrong.

Regards Roger


With Reflection library.
Crashes on "Launch", seems to say there is no such thing as getElevation.
Error occurred on line: 56 (Main)
java.lang.NoSuchMethodException: java.lang.Object.getElevation []
at java.lang.Class.getMethod(Class.java:2072)
at java.lang.Class.getDeclaredMethod(Class.java:2050)
at anywheresoftware.b4a.agraham.reflection.Reflection.runmethod(Reflection.java:214)
at anywheresoftware.b4a.agraham.reflection.Reflection.RunMethod(Reflection.java:802)

B4X:
Sub GetElevation
    For Each v As View In Activity.GetAllViewsRecursive
        If v Is Button Then
            Private r As Reflector
            Private F1 As Float
            r.Target =  r.GetActivity
            F1 = r.RunMethod("getElevation")
            Log(F1)
        End If
    Next
End Sub


With JavaObject library
Crashes on "Launch" like reflection library.

Error occurred on line: 54 (Main)
java.lang.RuntimeException: Method: getelevation not found in: android.widget.Button
at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:366)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:119)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)

B4X:
Sub GetElevation
    For Each v As View In Activity.GetAllViewsRecursive
        If v Is Button Then
            Private jo As JavaObject = v
            Private F1 As Float
            Private F1 As Float = jo.RunMethod("getelevation", Null)
            Log(F1)
        End If
    Next
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Everything that is possible in Java is possible in B4A.
B4X:
Dim jo As JavaObject = Button2
Log(jo.RunMethod("getElevation", Null))

It will return 0 as the way the buttons are structured is more complicated. You don't need all of this. The button elevation is 2dip. This is the only view that has built-in elevation.
You can access the panels elevation.
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Everything that is possible in Java is possible in B4A.
B4X:
Dim jo As JavaObject = Button2
Log(jo.RunMethod("getElevation", Null))

It will return 0 as the way the buttons are structured is more complicated. You don't need all of this. The button elevation is 2dip. This is the only view that has built-in elevation.
You can access the panels elevation.


Thanks Erel,

That sorted out the code, much appreciated.
I understood that the button elevation should be 2dip but in the early hours my OCD kicked in, speculating that clicking a button could result in an elevation 2+dip causing buttons to show through a panel. Yes, I know you sorted that out the day before but, at 3AM the cat and I were researching ways to check.

The simple answer is, no it doesn't. Attached is a simple code that demo's the button show-through [solved] issue and logs the elevations. It logs the elevations as 5.25 which changes to 2.625 if I change the panel elevation to 1. 5.25 must equate to 2dip on a Galaxy A70.
Tonight I will sleep and leave the questions to the cat. Thanks again for the feedback.

Attached is the simpledemo.

Regards Roger
 

Attachments

  • simpleexample.zip
    7.6 KB · Views: 179
Upvote 0

Similar Threads

Top