I have a TextView with rounded corners.
Here is my code:
float radius = 10f; ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel() .toBuilder() .setAllCorners(CornerFamily.ROUNDED,radius) .build(); MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel); ViewCompat.setBackground(textView,shapeDrawable);
Now, I want to change programmatically the background color of the textView.
When I do :
shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color.design_default_color_background));
it works; the background color is changed.
Now, I want to change the background color with an int color like Color.RED or any random color defined with Color.RGB(r, g, b, a) or Color.RGB(r, g, b).
How can I do that? Should I use shapeDrawable.setFillColor or another method?
Thanks.
Advertisement
Answer
ANSWER to my question.
Here is the code I added in order to be able to set any background color :
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]);
Crazy to have to write so much code just to change a background color…
Thanks all for your help !