Android Question reflector.runMethod("getWidth") always returns 0

Hi all,
In a sub of a class module, I need to get a b4xview‘s width by using Reflector because the b4xview.width had been set to -2. But the reflector always returns 0.
The code is like:

illustrative code:
'MyCustomClass

sub initialize(parameters)
    for i=1 to 5
        private btn as b4xview
        private b as button
        private btn as b4xview=b
       
        mbase.add(btn,0,0,80dip,80dip)
        btn.text="HelloWorld"
        btn.width=-2 'set the width to fit the text length'
    next
end sub

private sub getActualWidth
    private b as mbase.getview(3)
    reflector.Target=b
    private ActualWidth as int= reflector.RunMethod("getWidth")
    log(ActualWidth)  ' having no clue why the output is always 0
end sub

Thanks for any idea you might have for troubleshooting.
 
Solution
Reflector will not help.

It will be difficult to get the actual size once you set the width to -2. You should instead use anchors and designer script to set the layout yourself.
Reflector will not help.

It will be difficult to get the actual size once you set the width to -2. You should instead use anchors and designer script to set the layout yourself.
Using anchors and a designer script is likely not a possible option for me because my customized class is intended to generate several buttons because the count of buttons might differ between instances and is specified outside the class. The spaces between two buttons will also vary according to the length of their texts.
After some thread search, I eventually decided to use b4xcanvas.MeasureTextWidth as an alternative, and I am happy that it solves the problem.
 
Upvote 0
Top