Android: Change the color of RadioButtons and checkboxes programmatically

前端 未结 6 1206
轮回少年
轮回少年 2020-12-15 08:11

I created RadioButton and CheckBox in LinearLayout programatically. But, now I want to change radio button\'s color and check boxes\'s

相关标签:
6条回答
  • 2020-12-15 08:40

    I am continuing the answer of ywwynm.

    Google made the setSupportButtonTintList restricted so it is not possible to use it.

    The workaround is to use the button as a TintableCompoundButton interface, in which the method is not restricted.

    Works in API 19+ for AppCompatRadioButton. AppCompatCheckbox implements the same interface so it should work as well in theory but I haven't tested it.

    Have fun :)

    public static void setAppCompatRadioButtonColor(AppCompatRadioButton radioButton, int uncheckedColor, int checkedColor) {
        ColorStateList colorStateList = new ColorStateList(
                new int[][] {
                        new int[] { -android.R.attr.state_checked }, // unchecked
                        new int[] {  android.R.attr.state_checked }  // checked
                },
                new int[] {
                        uncheckedColor,
                        checkedColor
                }
        );
        ((TintableCompoundButton) radioButton).setSupportButtonTintList(colorStateList);
    }
    
    0 讨论(0)
  • 2020-12-15 08:41

    Try this

    AppCompatRadioButton newRadioButton = new AppCompatRadioButton(this);
    AppCompatCheckBox newCheckBox = new AppCompatCheckBox(this);
    

    Insted of

    RadioGroup newRadioButton = new RadioGroup(this);
    CheckBox newCheckBox = new CheckBox(this);
    
    0 讨论(0)
  • 2020-12-15 08:41

    You can create dynamic RadioButton like this:

    RadioButton male = new RadioButton(ReservationContact.this);
    male.setTextColor(Color.BLACK);
    male.setText("Male");
    male.setSelected(true);
    male.setId(0);
    

    This is used for changing default color of circle of RadioButton.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        male.setButtonTintList(ColorStateList.valueOf(ContextCompat.getColor(ReservationContact.this, R.color.background)));
    }
    male.setHighlightColor(getResources().getColor(R.color.background));
    
    0 讨论(0)
  • 2020-12-15 08:42

    Use AppCompatCheckBox and AppCompatRadioButton instead of CheckBox and RadioButton. Your xml will have :

    <android.support.v7.widget.AppCompatCheckBox
        android:id="@+id/cbSelected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/colorAccent" //This to set your default button color
        android:checked="true"/>
    
    <android.support.v7.widget.AppCompatRadioButton
        android:id="@+id/rb"
        app:buttonTint="@color/colorAccent" //This to set your default button color
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    

    Now for the java code : Create ColorStateList

            ColorStateList colorStateList = new ColorStateList(
                    new int[][]{
                            new int[]{android.R.attr.state_enabled} //enabled
                    },
                    new int[] {getResources().getColor(R.color.colorPrimary) }
            );
    

    To change the color programatically for AppCompatRadioButton or AppCompatCheckBox call setSupportButtonTintList.

    AppCompatRadioButton radioButton = (AppCompatRadioButton) findViewById(R.id.rb);
    radioButton.setSupportButtonTintList(colorStateList);
    
    AppCompatCheckBox cbSelected = (AppCompatCheckBox) findViewById(R.id.cbSelected);
    cbSelected.setSupportButtonTintList(colorStateList);
    
    0 讨论(0)
  • 2020-12-15 08:48
       RadioButton rb=(RadioButton) findViewById(R.id.radioButton1);
        rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                rb.setButtonDrawable(R.drawable.image);
                rb.setHighlightColor(Color.parseColor("#0c83bd"));
    
    
            }
        });
      }
    
    0 讨论(0)
  • 2020-12-15 08:53

    For CheckBox, replace your CheckBox with AppCompatCheckBox and call following method:

    public static void setCheckBoxColor(AppCompatCheckBox checkBox, int uncheckedColor, int checkedColor) {
        ColorStateList colorStateList = new ColorStateList(
                new int[][] {
                        new int[] { -android.R.attr.state_checked }, // unchecked
                        new int[] {  android.R.attr.state_checked }  // checked
                },
                new int[] {
                        uncheckedColor,
                        checkedColor
                }
        );
        checkBox.setSupportButtonTintList(colorStateList);
    }
    

    I think it should be similar for RadioButton. You can check declared attributes in android.R.attr and change the code.

    0 讨论(0)
提交回复
热议问题