Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radio Button style programmatically

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!

XML

<RadioGroup
            android:id="@+id/radiogroup"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical"
            android:animationCache="false">

            <RadioButton
                android:text="RadioButton 1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton3" />

            <RadioButton
                android:text="RadioButton 2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton4" />


</RadioGroup>

RESULT

enter image description here

JAVA CODE

This code is put in onCreateView in the fragment

public void addRadioButton(Context ctx,int num){

    RadioGroup radioGroup= (RadioGroup) alertInflatedView.findViewById(R.id.radiogroup);

    for (int i = 1; i <= num; i++) {
        RadioButton radioButton  = new RadioButton(ctx);
        radioButton.setId(1+i);
        radioButton.setText("Radio " + radioButton.getId());
        radioButton.setTextColor(getResources().getColor(R.color.black));

        radioGroup.addView(radioButton);

    }

}

RESULT

enter image description here

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

like image 972
Cristian Avatar asked Feb 24 '17 08:02

Cristian


People also ask

How to create radio button in XML?

Open “res/layout/main. xml” file, add “RadioGroup“, “RadioButton” and a button, inside the LinearLayout . Radio button selected by default. To make a radio button is selected by default, put android:checked="true" within the RadioButton element.

How do you programmatically determine whether a RadioButton is checked?

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.

How to add Dynamic radio button in android?

For creating dynamic RadioButton, we need to use android. view. ViewGroup. LayoutParams which configures the width and height of views and implements setOnCheckedChangeListener() method of RadioGroup class.

How do I get radio buttons side by side on android?

Use the android:orientation="horizontal" -attribute of the RadioGroup.


1 Answers

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

drawable/null_selector.xml

  <?xml version="1.0" encoding="utf-8"?> 
  <selector xmlns:android="http://schemas.android.com/apk/res/android" > 
         <item android:drawable="@android:color/transparent" /> 
  </selector> 

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);
  radioButton.setText(Integer.toString(i));
  radioButton.setGravity(Gravity.CENTER); 
  radioButton.setButtonDrawable(R.drawable.null_selector); 

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

like image 91
Horrorgoogle Avatar answered Oct 02 '22 00:10

Horrorgoogle