set specific color to ColorStateList programmatically

Raymond Chenon picture Raymond Chenon · Mar 13, 2013 · Viewed 7.4k times · Source

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"));

Answer

AnujMathur_07 picture AnujMathur_07 · Mar 13, 2013

Use this

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