Android Code Snippet Change RadioButton

I wanted an easy way to change the bitmaps of a radiobutton.

Code Function cRadioButton has a routine called LoadRadioButtonStateLists
'---------------------------------------------------------------------------------------------------------------' RB is the radiobutton you want to change
' Indent is how far you want the text to be indented
' ButtonStates Array of ButtonState Bitmaps / Filenames EnableUnSelected / EnableSelected / Disable
' NOTE: Only the first two states are required
'---------------------------------------------------------------------------------------------------------------Public Sub LoadRadioButtonStateLists(RB As RadioButton, Indent As Int, ButtonStates() As Object)

The array of Bitmap states is the bitmaps to use for the different states. This can be an Array of Bitmap's, BitmapDrawable's or File Names or any combination of. Made the routine to handle anything

I am attaching the whole test project in Zip File

Example:
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Private RadioButton1 As RadioButton
   Private RadioButton2 As RadioButton
   Private RadioButton3 As RadioButton
   Private RadioButton4 As RadioButton
   Private RadioButton5 As RadioButton
   Private RadioButton6 As RadioButton
   Private RadioButton7 As RadioButton
   Private RadioButton8 As RadioButton
   
   Private RedBitmap    As Bitmap
   Private BlueBitmap    As Bitmap
   Private UnselectedB    As Bitmap   
   
   Private UnselectedBD As BitmapDrawable
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("Main")

   RedBitmap.Initialize(File.DirAssets, "radiobutton_selected_red.png")
   BlueBitmap.Initialize(File.DirAssets, "radiobutton_selected_blue.png")
   
   UnselectedB.Initialize(File.DirAssets, "radiobutton_unselected.png")
   
   UnselectedBD.Initialize(UnselectedB)


   cRadioButton.LoadRadioButtonStateLists(RadioButton1, 10dip, Array As Object("radiobutton_unselected.png", "radiobutton_selected_red.png",      "radiobutton_unselected_disabled.png"))
   cRadioButton.LoadRadioButtonStateLists(RadioButton2, 10dip, Array As Object("radiobutton_unselected.png", "radiobutton_selected_blue.png",      "radiobutton_unselected_disabled.png"))
   cRadioButton.LoadRadioButtonStateLists(RadioButton3, 10dip, Array As Object(UnselectedBD,           "radiobutton_selected_green.png",    "radiobutton_unselected_disabled.png"))
   cRadioButton.LoadRadioButtonStateLists(RadioButton4, 10dip, Array As Object("radiobutton_unselected.png", "radiobutton_selected_red.png",      "radiobutton_unselected_disabled.png"))       
   
   cRadioButton.LoadRadioButtonStateLists(RadioButton5, 10dip, Array As Object("radiobutton_unselected.png",    RedBitmap,                "radiobutton_unselected_disabled.png"))
   cRadioButton.LoadRadioButtonStateLists(RadioButton6, 10dip, Array As Object("radiobutton_unselected.png",    BlueBitmap,              "radiobutton_unselected_disabled.png"))
   cRadioButton.LoadRadioButtonStateLists(RadioButton7, 10dip, Array As Object(UnselectedB,            "radiobutton_selected_green.png",    "radiobutton_unselected_disabled.png"))
   cRadioButton.LoadRadioButtonStateLists(RadioButton8, 10dip, Array As Object("radiobutton_unselected.png",    "radiobutton_selected_red.png",    "radiobutton_unselected_disabled.png"))       
   
End Sub

Hopes this helps someone out
 

Attachments

  • RBTest.zip
    15.5 KB · Views: 525
Top