set specific color to ColorStateList programmatically
Asked Answered
D

2

9

I created StateListDrawable from my question Add Color "#e3bb87" to StateListDrawable programmatically , but TextView.setTextColor does not take StateListDrawable (strange it works in layout) but ColorStateList . I read this change statelistdrawable text color android button

In the constructor of ColorStateList, it only accepts arrays of int

ColorStateList colorStateList = new ColorStateList(
            new int[][]{
                    new int[]{R.attr.state_pressed},
                    new int[]{R.attr.state_selected},
                    new int[]{-R.attr.state_selected},
            },
            new int[]{
                    Color.GREEN,
                    Color.BLUE,
                    Color.RED});

The color is not defined in colors.xml because I download this color attribute. How can I define like this ?

ColorStateList colorStateList = new ColorStateList(
            new int[][]{
                    new int[]{R.attr.state_pressed}
            },
            **getThisColor**("#e3bb87"));
Dogeatdog answered 13/3, 2013 at 11:37 Comment(0)
T
11

Use this

ColorStateList colorStateList = new ColorStateList(
            new int[][] { new int[] { R.dimen.padding_large } },
            new int[] {Color.parseColor("#e3bb87")});
Throwback answered 13/3, 2013 at 11:47 Comment(2)
Fortunately I was first. :D :)Throwback
What is the significance of dimen.padding_large here?Grassquit
F
10

You can use the valueOf() method of ColorStateList that returns a ColorStateList containing a single color:

ColorStateList.valueOf(Color.parseColor("#e3bb87"))
Faculty answered 7/8, 2020 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.