Android Code Snippet Change Radiobutton Appearance

SubName: SetRadioButtonDrawable

Description: as an addon to this thread, as it has been asked for a few times, this is an example of changing the RadioButton graphic. You could make the drawable to be anything you want, or load an image for each if preferred.

B4X:
'Change the size and color of a RadioButton graphic.
'Pass the required colors for the attributes, size should be at most the smallest side of the view.
Sub SetRadioButtonDrawable(RBtn As RadioButton,CircleColor As Int,Size As Int,SelectedColor As Int,DisabledColor As Int,StrokeWidth As Int)
    Dim Enabled,Selected,Disabled,DisabledSelected As BitmapDrawable
    Dim EnabledJO,SelectedJO,DisabledJO,DisabledSelectedJO As JavaObject
    Dim SLD As StateListDrawable
    SLD.Initialize

    'Changing the padding also determines how far away the text is from the drawing
    Dim Padding As Int = Size / 100 * 50
    Dim Radius As Int = (Size - Padding) / 2
    Dim InnerPadding As Int = Max(1Dip,1)

    Dim BMEnabled,BMSelected,BMDisabled,BMDisabledSelected As Bitmap
    BMEnabled.InitializeMutable(Size,Size)
    BMSelected.InitializeMutable(Size,Size)
    BMDisabled.InitializeMutable(Size,Size)
    BMDisabledSelected.InitializeMutable(Size,Size)

    'Draw Enabled State
    Dim CNV As Canvas
    CNV.Initialize2(BMEnabled)
    CNV.DrawCircle(Radius + InnerPadding,Size / 2,Radius,CircleColor,False,StrokeWidth)
    Enabled.Initialize(BMEnabled)
    EnabledJO = Enabled
    EnabledJO.RunMethod("setAntiAlias",Array As Object(True))
    Enabled.Gravity = Gravity.left

    'Draw Selected state
    Dim CNV1 As Canvas
    CNV1.Initialize2(BMSelected)
    CNV1.DrawCircle(Radius + InnerPadding,Size / 2,Radius,CircleColor,False,StrokeWidth)
    'Draw the Selected state centered in the box
    CNV1.DrawCircle(Radius + InnerPadding,Size / 2,(Radius - InnerPadding) / 2,SelectedColor,True,StrokeWidth)
    Selected.Initialize(BMSelected)
    SelectedJO = Selected
    SelectedJO.RunMethod("setAntiAlias",Array As Object(True))
    Selected.Gravity = Gravity.left

    'Draw Disabled Selected State
    Dim CNV2 As Canvas
    CNV2.Initialize2(BMDisabledSelected)
    CNV2.DrawCircle(Radius + InnerPadding,Size / 2,Radius,DisabledColor,False,StrokeWidth)
    'Draw the Disabled Selected state centered in the box
    CNV2.DrawCircle(Radius + InnerPadding,Size / 2,(Radius - InnerPadding) / 2,DisabledColor,True,StrokeWidth)
    DisabledSelected.Initialize(BMDisabledSelected)
    DisabledSelectedJO = DisabledSelected
    DisabledSelectedJO.RunMethod("setAntiAlias",Array As Object(True))
    DisabledSelected.Gravity = Gravity.left

    'Draw disabled State
    Dim CNV3 As Canvas
    CNV3.Initialize2(BMDisabled)
    CNV3.DrawCircle(Radius + InnerPadding,Size / 2,Radius,DisabledColor,False,StrokeWidth)
    Disabled.Initialize(BMDisabled)
    DisabledJO = Disabled
    DisabledJO.RunMethod("setAntiAlias",Array As Object(True))
    Disabled.Gravity = Gravity.left

    'Add To the StateList Drawable
    SLD.AddState2(Array As Int(SLD.State_Disabled,SLD.State_Checked),DisabledSelected)
    SLD.AddState(SLD.State_Disabled,Disabled)
    SLD.AddState(SLD.State_Checked,Selected)
    SLD.AddState(SLD.State_Enabled,Enabled)
    SLD.AddCatchAllState(Enabled)
    'Add SLD To the CheckBox
    Dim JO As JavaObject = RBtn
    JO.RunMethod("setButtonDrawable",Array As Object(SLD))
End Sub

DependsOn: JavaObject

Call As: where RB5 is a RadioButton
B4X:
SetRadioButtonDrawable(RB5,Colors.LightGray,Min(RB5.Width,RB5.Height),Colors.Cyan,DisabledColor,2Dip)

Tags: Change RadioButton graphic image

A simple project is attached to show the difference with the original RadioButton images.
 

Attachments

  • RBTest.zip
    8.1 KB · Views: 1,028
Last edited:
Top