Android Question LabelView width autosize

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello all,

I saw many posts in the forum about how to automatically resize the font size inside a label in order to dynamically fit it, but I have a different situation here.
I need to add views in runtime to a activity (labels), as in the code :
B4X:
            For i = 0 To Cursor1.RowCount -1
                Cursor1.Position = i
                lblMenuItem(i).Initialize("lblMenuItem")
                lblMenuItem(i).TextColor = Colors.Gray
                lblMenuItem(i).Text = Cursor1.GetString("name").Trim
                If I = 0 Then
                    lblMenuItem(i).TextColor = Colors.Red
                Else
                    lblMenuItem(i).TextColor = Colors.Black
                End If
                lblMenuItem(i).Tag = i
                'lblMenuItem(i).Typeface = Typeface.STYLE_BOLD
                'itemMenuWidth = lblMenuItem(i).Text.Length * 12
                lblMenuItem(i).TextSize = 18
                lblMenuItem(i).SingleLine = True
                menusPosH(i) = hPos
                scrlMenus.Panel.AddView(lblMenuItem(i),hPos,scrlMenus.Height * 0.1,itemMenuWidth,scrlMenus.Height)
                'SetAutoSizeBasedOnText(lblMenuItem(i))
                hPos = hPos + itemMenuWidth + 10

            Next

The text size is fixed (18) and I need to add labels in a horizontal scroll view . I don't know previously the text that will be written inside the label and I need to define the variable itemMenuWidth to fit each menu item.
I tried to multiply the textlength by a fixed value but depending on the device's screen resolution this doesn't work... I think that the solution must to be something which consider the device screen size and pixel density but I didn't realized how to do this yet.

Anybody could help?

Thanks!
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…