Radio Button style programmatically

Related searches

I would to create a number of radio button dinamically in a fragment, I only have problem with style. If I put radiobutton code in the xml file, default style is applied correctly, but when I create radiobutton through a function I see different style!



                android:text="RadioButton 1"
                android:id="@+id/radioButton3" />

                android:text="RadioButton 2"
                android:id="@+id/radioButton4" />




This code is put in onCreateView in the fragment

public void addRadioButton(Context ctx,int num){

    RadioGroup radioGroup= (RadioGroup) alertInflatedView.findViewById(;

    for (int i = 1; i <= num; i++) {
        RadioButton radioButton  = new RadioButton(ctx);
        radioButton.setText("Radio " + radioButton.getId());





As you can see radio buttons have different style, someone could help me, if is possibile, to apply default style programmatically?

you have to create style on drawable or style.xml, as your requirement.


  <?xml version="1.0" encoding="utf-8"?> 
  <selector xmlns:android="" > 
         <item android:drawable="@android:color/transparent" /> 

Set each button to use it (and to center the text) like this (R.drawable.null_selector is selector XML):

Now, In your Activity, you must be implement such style.

  RadioButton radioButton = new RadioButton(ctx);

I think, this will help you for implementing custom style in Radio Button.

Applying a Theme to Dynamic Radio Buttons in Android, style.AppTheme);. The RadioButton is created and the text is styled properly but the checked marker disappears. Why? I� Right-justifies text in the button rectangle. However, if the button is a check box or radio button that does not have the BS_RIGHTBUTTON style, the text is right justified on the right side of the check box or radio button. BS_RIGHTBUTTON: Positions a radio button's circle or a check box's square on the right side of the button rectangle.

Thanks Dharma, I followed your suggestion, changing something, and I solved!


public void addRadioButton(Context ctx,int num){

    RadioGroup radioGroup= (RadioGroup) alertInflatedView.findViewById(;
    RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(

    for(int i=0; i<num; i++){

        RadioButton radioButton  = new RadioButton(ctx);



XML RADIO BUTTON SELECTOR with checked and unchecked button image

<selector xmlns:android="" >
<item android:state_checked="false" android:drawable="@drawable/unchekedradiobutton" />
<item android:state_checked="true" android:drawable="@drawable/checkedradiobutton" />
<item android:drawable="@drawable/unchekedradiobutton" /> <!-- default -->

Set Styles programmatically in Android – Bel�n Cruz, When creating a new radio button, the first approach would be to use the “new RadioButton” sentence. The constructor receives the style ID as� A set of radio buttons that can be added to a UI definition. Details. If you need to represent a "None selected" state, it's possible to default the radio buttons to have no options selected by using selected = character(0). However, this is not recommended, as it gives the user no way to return to that state once they've made a selection.

Use an Inflater instance to inflate a custom layout and easily get a custom Radiobutton

private RadioButton createCustomRadioButton(Context context){
    LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(R.layout.radio_button,null);
    RadioButton radioButton  = (RadioButton) v.findViewById(;
    radioButton.setText("It Works!");

    return radioButton;


    android:background="@drawable/style_line" />


<style name="radio">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_marginLeft">30dp</item>
    <item name="android:layout_marginRight">30dp</item>
    <item name="android:layout_marginTop">10dp</item>
    <item name="android:layout_marginBottom">10dp</item>
    <item name="android:padding">10dp</item>
    <item name="android:drawablePadding">5dp</item>
    <item name="android:textColor">@color/whiteColor</item>
    <item name="android:textColorHint">@color/hintColor</item>
    <item name="android:editTextColor">@color/whiteColor</item>

by Ubirajara (México)

Decorating RadioGroup/RadioButton in Android, You can check the current state of a radio button programmatically by using Apply this style to your RadioButton via android:theme attribute. September 11, 2017. RadioButton UI control can be used when only one option is allowed to be selected out of multiple options presented to users for an input field in your application. RadioButton like CheckBox is a two state button with checked and unchecked states. The main difference between CheckBox and RadioButton is once radio button is checked it can’t be unchecked where as CheckBox can be checked and unchecked.

Programmatically, I recommend setting a ColorStateList to each radio button in the loop like this: radioButton.setButtonTintList(getRadioButtonColors());


private ColorStateList getRadioButtonColors() {
        return new ColorStateList (
                new int[][] {
                        new int[] {android.R.attr.state_checked}, // checked
                        new int[] {android.R.attr.state_enabled} // unchecked
                new int[] {
                        Color.GREEN, // checked
                        Color.BLUE   // unchecked

Where android.R.attr.state_checked defines the color for the checked button (green) and android.R.attr.state_enabled defines the color for unchecked buttons (blue).

How to Dynamically Change an Android View's Style, And let's also assume you plan to programmatically change the style to to all views that have text (e.g., Button , Checkbox , RadioButton etc.)� Radio Group and Radio Button are two great components provided by the Android Framework, but unfortunately they often cannot satisfy all requirements. In this case there are several possible ways to get missing functionality, for example, use third-party libraries or create your own custom view components.

You can check the current state of a radio button programmatically by using isChecked () method. This method returns a Boolean value either true or false. if it is checked then returns true otherwise returns false. Below is an example code with explanation in which we checked the current state of a radio button.

The radio button and checkbox button all take the default accent color for selection from your application. Changing the layout to something that you want is not that simple, but is very easy if you follow through correctly.

I am also trying this solution but using this way radio group allowing me to select all radio button inside it. It should be only single selection of radio button within the same radio group. – Min2 Mar 15 '16 at 7:31

  • Possible duplicate of Setting styles of programmatically added Views