Question

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"));
Was it helpful?

Solution

Use this

ColorStateList colorStateList = new ColorStateList(
            new int[][] { new int[] { R.dimen.padding_large } },
            new int[] {Color.parseColor("#e3bb87")});

OTHER TIPS

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

ColorStateList.valueOf(Color.parseColor("#e3bb87"))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top