Android Question Using my own images for checkboxes and radio buttons

wimpie3

Well-Known Member
Licensed User
Longtime User
How can I use my own images for checkboxes and radio buttons? Is there someone who did this and is willing to share his code?
 

giga

Well-Known Member
Licensed User
Longtime User
Upvote 0

klaus

Expert
Licensed User
Longtime User
You can define a BitmapDrawable object and set it to the radiobuttons.
B4X:
Dim bdw As BitmapDrawable
bdw.Initialize(LoadBitmap(File.DirAssets, "Image.png")
rbtTest.Background = bdw
You can do this directly in the Designer.
Add the images.
Select in Drawable : BitmapDrawable
And select the image.

Best regards.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Attached you find a small example code.

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

    AddStatelistRadioButton(rbtTest1, "Flag_GB_1.png", "Flag_GB_2.png", "Flag_GB_3.png", Gravity.CENTER)
    AddStatelistRadioButton(rbtTest2, "Flag_F_1.png", "Flag_F_2.png", "Flag_F_3.png", Gravity.CENTER)
  
End Sub

Sub AddStatelistRadioButton(rbt As RadioButton, ImageEnabled As String, ImageChecked As String, ImagePressed As String, mGravity As Int)
    Dim sdw As StateListDrawable
    sdw.Initialize
    Dim bdwEnabled, bdwChecked, bdwPressed As BitmapDrawable
    bdwEnabled.Initialize(LoadBitmap(File.DirAssets, ImageEnabled))
    bdwEnabled.Gravity = mGravity
    bdwChecked.Initialize(LoadBitmap(File.DirAssets, ImageChecked))
    bdwChecked.Gravity = mGravity
    bdwPressed.Initialize(LoadBitmap(File.DirAssets, ImagePressed))
    bdwPressed.Gravity = mGravity
    sdw.AddState(sdw.State_Checked, bdwChecked)
    sdw.AddState(sdw.State_Pressed, bdwPressed)
    sdw.AddCatchAllState(bdwEnabled)
    rbt.Background = sdw
End Sub
If you don't want the 'pressed' state you can remove the relevant lines.

Best regards.
 

Attachments

  • RadioButtonStatelistDrawable.zip
    14 KB · Views: 440
Upvote 0
Top