Custom Android RadioButton
I was browsing StackOverflow and came across a question about making custom RadioButtons
and figured I could answer so I decided to take the plunge.
To implement a custom styled RadioButton you can follow the below steps or look at the complete example source here.
Create the drawable resources for your custom RadioButton. You should have at least 4 icons:
Pressed | Checked | |
---|---|---|
True | True | |
True | False | |
False | True | |
False | False |
Place your images in res/drawable
or if if you have a version for each screen density place them in their corresponding res/drawable-ldpi
, res/drawable-mdpi
, and res/drawable-hdpi
folders.
Then create a selector
type XML file in res/drawable. Here is my res/drawable/button_radio.xml
1 2 3 4 5 6 7 8 9 10 |
|
Setup your RadioGroup like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
I have specified dimension of 50dp as the dimension of my drawables are 50px x 50px. Also notice I am setting android:button
and not android:background
.
Hopefully this can serve as a nice starting point for anyone looking to create some new and interesting effects with simple RadioButtons
.