Changing color of a Checkbox

Asked

Viewed 1,756 times

0

It is possible to change the color of a Checkbox via program (Runtime)?

inserir a descrição da imagem aqui

I would like to change the white color of the box highlighted in the image.

1 answer

2


This color is defined by the theme you use, so a first output would be to change the theme in Runtime - kind of weird, but possible.

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    // ...

    // chame setTheme antes de criar qualquer View

    setTheme(android.R.style.Theme_Dark);

    // ...

    setContentView(R.layout.main);
}

If you want to change the theme after the creation of Activity, you will have to recreate the activity and inform the new theme to be used.

If it seems very inefficient, you can also change the buttonTintList of CheckBox. For that, just call CheckBox#setButtonTintList(ColorStateList) and you change how the Checkbox will be drawn in each state. There are two ways to create a ColorStateList:

Code

private static final int[][] CHECK_BOX_STATES = new int[][] {
    new int[] {-android.R.attr.state_enabled}, // desabilitado
    new int[] { android.R.attr.state_checked}, // marcado
    new int[] {-android.R.attr.state_checked}, // desmarcado
    new int[] {} // default
};

private static final int[] CHECK_BOX_COLORS = new int[] {
    Color.GRAY,
    Color.CYAN,
    Color.MAGENTA,
    Color.MAGENTA
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // ...

    CheckBox checkBox = (CheckBox) findViewById(R.id.cb_id);

    // limpa buttonTintList
    checkBox.setButtonTintList(null);
    checkBox.setButtonTintList(new ColorStateList(CHECK_BOX_STATES, CHECK_BOX_COLORS));

    // ...
}

XML

Assets/checkbox_style.xml

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

    <item android:color="#ffff00" />

    <item android:state_checked="true"
            android:color="#00ffff" /> 

    <item android:state_checked="false"
            android:color="#ff00ff" />
</selector>

Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // ...

    CheckBox checkBox = (CheckBox) findViewById(R.id.cb_id);

    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);

        XmlPullParser parser = factory.newPullParser();
        parser.setInput(getAssets().open("checkbox_style.xml"), "UTF-8");

        checkBox.setButtonTintList(ColorStateList.createFromXml(getResources(), parser));
    } catch (XmlPullParserException | IOException e) {
        e.printStackTrace();
    }

    // ...
}

In neither of the two cases I was able to change the color of the Checkbox definitively. Via code the color changes only once for each state (for example: changes when check, but when unchecked does not return the color of the unchecked state), already via XML the emulator throws a bizarre exception complaining that the tag item need to have an attribute android:color.

For more information look at the documentation: Checkbox, CompoundButton#setButtonTintList(ColorStateList), Colorstatelist.

I hope I’ve helped.

  • I used the first part of his reply. As the user chooses the theme q he wants, it does not get as inefficient. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.