Cambiar colorControlActivated color programmically
He leído algunos temas sobre el color, pero todos ellos tienen que establecer a través de style.xml.
Por ahora estoy usando esto para determinar el color.
- Cómo aplicar un estilo personalizado a SwitchCompat
- SwitchCompat en barra de acción, con fragmento
- Cree su propia preferencia de SwitchCompat
- OnCheckedChanged se llama automáticamente
- Cuadro de selección alrededor de android.support.v7.widget.SwitchCompat
<style name="Color1SwitchStyle"> <item name="colorControlActivated">#0e8488</item> </style>'
¿Es posible cambiar el color de un SwitchCompat / Checkbox sin utilizar XML, por ejemplo, utilizando código?
- Android AppCompat v21 proporciona SwitchCompat no proporciona SwitchCompatPerefrence
- Android: SwitchCompat, problemas de relleno y color
- Cómo cambiar el color de un SwitchCompat
- Cómo cambiar el ancho de SwitchCompat thumb
- Android SwitchCompat no está alineado
- Cómo cambiar el color de la pista de un SwitchCompat
En realidad, no es difícil de hacer.
Ejemplo:
int[][] states = new int[][] { new int[] {-android.R.attr.state_checked}, new int[] {android.R.attr.state_checked}, }; int[] thumbColors = new int[] { Color.BLACK, Color.RED, }; int[] trackColors = new int[] { Color.GREEN, Color.BLUE, }; SwitchCompat switchCompat = (SwitchCompat) findViewById(R.id.switchControl); AppCompatCheckBox checkBox = (AppCompatCheckBox) findViewById(R.id.checkbox); checkBox.setSupportButtonTintList(new ColorStateList(states, thumbColors)); DrawableCompat.setTintList(DrawableCompat.wrap(switchCompat.getThumbDrawable()), new ColorStateList(states, thumbColors)); DrawableCompat.setTintList(DrawableCompat.wrap(switchCompat.getTrackDrawable()), new ColorStateList(states, trackColors));