简体   繁体   中英

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

在此处输入图片说明

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

在此处输入图片说明

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.

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.

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

JAVA CODE

public void addRadioButton(Context ctx,int num){

    RadioGroup radioGroup= (RadioGroup) alertInflatedView.findViewById(R.id.radiogroup);
    RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(
            RadioGroup.LayoutParams.MATCH_PARENT,
            RadioGroup.LayoutParams.WRAP_CONTENT);

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

        RadioButton radioButton  = new RadioButton(ctx);
        radioButton.setId(1+i);
        radioButton.setText("Radio"+i);
        radioButton.setTextSize(16);
        radioButton.setTextColor(getResources().getColor(R.color.black));
        radioButton.setButtonDrawable(R.drawable.radio_button_selector);
        radioButton.setPadding(80,0,0,0);
        radioButton.setGravity(Gravity.CENTER_VERTICAL);
        radioButton.setLayoutParams(layoutParams);
        radioGroup.addView(radioButton);

    }

}

XML RADIO BUTTON SELECTOR with checked and unchecked button image

<selector xmlns:android="http://schemas.android.com/apk/res/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 -->

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(R.id.radio_button);
    radioButton.setText("It Works!");

    ((ViewGroup)radioButton.getParent()).removeView(radioButton);
    return radioButton;
}

radio_buttom.xml

<RadioButton
    android:id="@+id/radio_button"
    style="@style/radio"
    android:background="@drawable/style_line" />

style.xml

<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>
</style>

by Ubirajara (México)

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

Then

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) .

Personally I think this is a better solution because it's more concise than creating styles and other dependencies elsewhere in the codebase. Whenever possible use the most concise and efficient approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM