How to automatically adjust RadioButton size, based on its text

sterlingy

Active Member
Licensed User
Longtime User
Hi folks,

I'm going to try and make this simple.

I have a panel with 4 RadioButtons. The text associated with the buttons is provided from a database, so there is no fixed amount of textual lines. Sometimes a button might only have one line of text or sometimes four or more lines.

So there are two problems:

1: The height of the RadioButton needs to adjust, depending on the amount of text that is associated with it.

2: Each subsequent button, will need to move, based on the height of the buttons above it.

If you use Java and RadioGroup, this is all done automatically.

Is there an easy way to do this?

Cheers,

:sign0085: Sterling
 

kanaida

Active Member
Licensed User
Longtime User
Usually I make a variable at the top holding the height of all the stuff i'm adding.

As I go along I keep adding to that variable to keep track. Then using it as the top value of the next thing i add... It's a little medival but it works. If you're lucky and know the height in advance, I create a sub that creates the set of things I want in a panel of a certain size, and just pass it the ItemNumber and the panel i'm adding it to as a parameter, then multitply it times the fixed height size to get my top value.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Perhaps you can give this a try, though I think that you will have to manually adjust spacing.
B4X:
Sub loadRadioButtons
Dim aw As Int, ph As Int  
aw=Activity.Width-50dip: ph=0 
Dim txtSize
Dim spacing
Dim s As String 
txtSize=12dip
spacing=20dip
s="":For l=0 To 99:s=s & "x" :Next
For k=0 To 3
Dim rbtxt As String 
Dim rbh
Dim rb As RadioButton
Dim rbL As Int 
rb.Initialize("radioButtons")
rb.text =s.SubString (Rnd(0,100))
rb.TextSize =txtSize
Dim cnv As Canvas
cnv.Initialize(Activity)
Dim wdth As Float,hght As Float 
wdth=cnv.MeasureStringWidth (rb.Text,Typeface.DEFAULT ,txtSize)
hght=cnv.MeasureStringheight (rb.Text,Typeface.DEFAULT ,txtSize)
rbL=wdth/aw+1
rbh=rbL*hght+spacing
Activity.AddView (rb,0,ph,aw,rbh)
ph=ph+rbh
Next
End Sub
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
You can set the width and height to -2. This is the constant value of WRAP_CONTENT.
B4X:
Sub Globals
   Dim cb As CheckBox
End Sub

Sub Activity_Create(FirstTime As Boolean)
   cb.Initialize("cb")
   Activity.AddView(cb, 10dip, 10dip, -2, -2)
   cb.Text = "this is a long text" & CRLF & "....." & CRLF & "...."
End Sub

Erel,

Your code works, but when I apply it to my RadioButtons, my text remins on one long line, running off the edge of the screen. I should point out that the RadioButtons are children of a Panel, which is a child of another Panel.


I should also point out that I used the designer to create these buttons, and I just entered -2 in the height and width properties.

-Sterling
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
The code below works.
B4X:
Sub Globals
    Dim rbtTest1, rbtTest2 As RadioButton
    Dim btnTest1, btnTest2 As Button
    Dim rflTest1, rflTest2 As Reflector
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")

    rbtTest1.Height = -2
    rflTest1.Target = rbtTest1    ' initializes the reflection object
    
    rbtTest2.Height = -2
    rflTest2.Target = rbtTest2    ' initializes the reflection object

    UpdateScreen
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub rbtTest1_CheckedChange(Checked As Boolean)
    If rbtTest1.Checked = False Then
        rbtTest1.Text = "This is a short text"
        rbtTest2.Text = "This is a long text This is a long text This is a long text This is a long text This is a long text"
    Else
        rbtTest1.Text = "This is a long text This is a long text This is a long text This is a long text This is a long text"
        rbtTest2.Text = "This is a short text"
    End If
    UpdateScreen
End Sub

Sub rbtTest2_CheckedChange(Checked As Boolean)
    If rbtTest2.Checked = False Then
        rbtTest2.Text = "This is a short text"
        rbtTest1.Text = "This is a long text This is a long text This is a long text This is a long text This is a long text"
    Else
        rbtTest2.Text = "This is a long text This is a long text This is a long text This is a long text This is a long text"
        rbtTest1.Text = "This is a short text"
    End If
    UpdateScreen
End Sub

Sub btnTest1_Click
    rbtTest1.Text = "This is a short text"
    rbtTest2.Text = "This is a short text"
    UpdateScreen
End Sub

Sub btnTest2_Click
    rbtTest1.Text = "This is a long text This is a long text This is a long text This is a long text This is a long text"
    rbtTest2.Text = "This is a long text This is a long text This is a long text This is a long text This is a long text"
    UpdateScreen
End Sub

Sub UpdateScreen
    Dim rbtTestHeight As Int

    DoEvents    ' needed to get the updated heights
    rbtTestHeight = rflTest1.RunMethod("getHeight")
    rbtTest2.Top = rbtTest1.Top + rbtTestHeight + 10dip

    rbtTestHeight = rflTest2.RunMethod("getHeight")
    btnTest1.Top = rbtTest2.Top + rbtTestHeight + 10dip
    btnTest2.Top = btnTest1.Top + btnTest1.Height + 10dip
End Sub
There are different actions to change the text length just for testing.
I had to set the RadioButton heights to -2 in the code. Setting the height values to -2 in the Designer doesn't work without setting them again in the code doesn't work. To adjust the positions ot the views we need the Reflection library to get the real height because rbtHeigt returns -2.

Best regards.
 

Attachments

  • RadioButtonHeight.zip
    6.9 KB · Views: 355
Upvote 0
Top