ToggleButton TextSize too small

rgately

Member
Licensed User
Longtime User
Greetings,

I noticed that the ToggleButton does not allow the text to be very large before it gets cropped. I can only get the text to be about 1/4 of the ToggleButton's height but I want the text to be about 3/4 of the ToggleButton's height.

Is there a way to make the text larger????
 

rgately

Member
Licensed User
Longtime User
You cannot change the ToggleButtons layout without changing its background drawable.

From your reply, it sounds like it is possible to expand the text area of the ToggleButton. I am not sure what you mean though and would appreciate it if you could point me in the right direction.

I tried making a ColorDrawable and assigning it to the ToggleButton.Background but that did not seem to do anything with regards to the button's text area.

Again, I want to make the text area on the ToggleButton be about as large as the button itself.

Thanks!
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You can define ColorDrawables to a ToggleButton.
But as you noticed it doesn't change the text area.
To change this you must change the Padding area (margins).

The code below shows how it can be done it shows two ToggleButtons :
- one with BitmapDrawables
- one ColorDrawables and an incearsed text area.
B4X:
Sub Globals
    Dim tb1, tb2 As ToggleButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
    ' load the layout file
    Activity.LoadLayout ("main")

    ' determine BitmapDrawables
    Dim bdwChecked, bdwUnChecked As BitmapDrawable 

    ' bitmap state
    bdwChecked.Initialize (LoadBitmap(File.DirAssets, "btncharsizedown.bmp"))
    bdwUnChecked.Initialize (LoadBitmap(File.DirAssets, "btncharsizeup.bmp"))
    Dim sld1 As StateListDrawable
    ' initialize StateListDrawable
    sld1.Initialize
    ' add states
    sld1.AddState(sld1.State_Checked, bdwChecked)
    sld1.AddState(sld1.State_Unchecked, bdwUnChecked)
    ' set background as state
    tb1.Background = sld1

    ' determine ColorDrawables
    Dim cdwChecked, cdwUnChecked As ColorDrawable
    'colors state
    cdwChecked.Initialize(Colors.Green, 10dip)
    cdwUnChecked.Initialize(Colors.Red, 10dip)
    Dim sld2 As StateListDrawable
    ' initialize StateListDrawable
    sld2.Initialize
    ' add states
    sld2.AddState(sld1.State_Checked, cdwChecked)
    sld2.AddState(sld1.State_Unchecked, cdwUnChecked)
    ' set background as state
    tb2.Background = sld2
    
    ' change the padding (margins) values
    Dim refl As Reflector
    refl.Target = tb2
    Dim args(4) As Object
    Dim types(4) As String
    args(0) = 2dip    ' left
    args(1) = 2dip    ' top
    args(2) = 2dip    ' right
    args(3) = 2dip    ' bottom
    types(0) = "java.lang.int"
    types(1) = "java.lang.int"
    types(2) = "java.lang.int"
    types(3) = "java.lang.int"
    refl.RunMethod4("setPadding", args, types)    
End Sub
Attached a small test program two ToggleButtons :

Best regards.
 

Attachments

  • ToggleState.zip
    9.3 KB · Views: 217
Upvote 0

rgately

Member
Licensed User
Longtime User
Klaus,

Your solution was perfect! All I needed was the part pertaining to Padding Area. In my test, I was able to set the Top padding to a negative value so that the large text would fit close to the top.

I'm so glad that there is a way to do this and so many other thing using Reflector. I'd like to learn more about it.

Thanks for the answer I was looking for!
 
Upvote 0
Top