Tengo un TextView con esquinas redondeadas.
Aquí está mi código:
float radius = 10f; ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel() .toBuilder() .setAllCorners(CornerFamily.ROUNDED,radius) .build(); MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel); ViewCompat.setBackground(textView,shapeDrawable);Ahora, quiero cambiar mediante programación el color de fondo de textView.
Cuando lo hago :
shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color.design_default_color_background));funciona; se cambia el color de fondo.
Ahora, quiero cambiar el color de fondo con un color int como Color.RED o cualquier color aleatorio definido con Color.RGB(r, g, b, a) o Color.RGB(r, g, b).
¿Cómo puedo hacer eso? ¿Debo usar shapeDrawable.setFillColor u otro método?
Gracias.
RESPUESTA a mi pregunta.
Aquí está el código que agregué para poder establecer cualquier color de fondo:
int[][] states = new int[][] { new int[] { android.R.attr.state_hovered}, // hovered }; int[] colors = new int[] {color}; ColorStateList myColorList = new ColorStateList(states, colors); shapeDrawable.setFillColor(myColorList); shapeDrawable.setState(states[0]);Es una locura tener que escribir tanto código solo para cambiar un color de fondo...
Gracias a todos por su ayuda !
Puede definir el valor global para su color así
public static final int RED = 0xffff0000;Y usar así.
shapeDrawable.setFillColor(ContextCompat....(this, RED));El método setFillColor funciona con ColorStateList .
Puedes usar algo como:
int[][] states = new int[][] { new int[] { android.R.attr.state_focused}, // focused new int[] { android.R.attr.state_hovered}, // hovered new int[] { android.R.attr.state_enabled}, // enabled new int[] { } // }; int[] colors = new int[] { Color.BLACK, Color.RED, Color...., Color.... }; ColorStateList myColorList = new ColorStateList(states, colors);