Correct way to use Reflection to set a view parameter

Tim Forrester

Member
Licensed User
Longtime User
I am having difficulty using the Reflector library correctly to set the appearance of a view (I do not think the capability to do what I want to do is currently in B4A). Below is the sub I put in a code module. I am very new to all of this so I appreciate your understanding if I am asking a stupid question.

The Android site I am looking at is ...
GradientDrawable | Android Developers

Thanks.

B4X:
Sub GetPanelFormat(pnl As Panel, pnlType As String)
   Dim gd As GradientDrawable
   Dim rfl1 As Reflector
   Dim arColor(2) As Int

   Select pnlType
      Case "input"
         arColor(0) = Colors.Blue
         arColor(1) = Colors.White
         gd.Initialize("LEFT_RIGHT",arColor )
         gd.CornerRadius = 20dip
         rfl1.Target = gd

'      THEN, CHANGE THE GRADIENT TYPE FROM THE DEFAULT LINEAR TO RECTANGLE

'      EXAMPLES of Reflector.RunMethod attemps that failed. Error message something like "NO METHOD FOUND".

'      rfl1.RunMethod("setGradientType(RECTANGLE)")
'      rfl1.RunMethod("GradientDrawable.setGradientType(RECTANGLE)")
'      rfl1.RunMethod("android.graphics.drawable.GradientDrawable$setGradientType(RECTANGLE)")
'      rfl1.RunMethod("android.graphics.drawable.GradientDrawable.setGradientType(RECTANGLE)")
'      rfl1.RunMethod("android.graphics.drawable.setGradientType(RECTANGLE)")

'      I picked "RunMethod" as the best way to implement a GET or SET method but I may be way off.
'      I mostly expect that I do not understand how to read Andrew's documentation and I am messing up the
'      parameters (i.e., "method as String" parameter).
   
         pnl.Background = gd

      Case "help"
         arColor(0) = Colors.Red
         arColor(1) = Colors.White
         gd.Initialize("LEFT_RIGHT",arColor )
         gd.CornerRadius = 10dip
         rfl1.Target = gd

'         CHANGE THE GRADIENT TYPE FROM THE DEFAULT LINEAR TO RECTANGLE

         pnl.Background = gd

      Case Else
         pnl.Color = Colors.Black
   End Select
End Sub
 

stevel05

Expert
Licensed User
Longtime User
Hi Tim,

You were close, the syntax you need for this is:

rfl1.RunMethod2("setGradientType",2,"java.lang.int")

Some of the shapes/types don't appear to do what you would expect (although I haven't read the documentation so don't really know what to expect). but 2 'SWEEP_GRADIENT' does something different at least. The other constants are listed on the Android Developers page which you've probably seen.

To call a java method the parameters passed have to match the type it expects, otherwise it will complain that there is no such method (as you had).

The run method you choose depends on how many parameters you need to pass.

Have another look through the documentation, hopefully it will now make more sense.

Steve
 
Last edited:
Upvote 0

Tim Forrester

Member
Licensed User
Longtime User
Thank you

Thanks for help Steve. It works. Also helps me understand a little more of what parameters are expected in the Reflection functions.

BTW, I appreciate the coaching tone of your reply. Very encouraging :)
 
Upvote 0
Top