Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Color "#e3bb87" to StateListDrawable programmatically

The reason I need to do this programmatically is that the text color is downloaded and not pre defined in the xml. I read this Replace selector images programmatically

I only need to know from

StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
    getResources().getDrawable(R.drawable.pressed));

how turn into

states.addState(new int[] {android.R.attr.state_pressed},**theMethodImLookingFor**("#e3bb87"));

forget about getResources().getColor(R.color.anycolor) , the color is not defined in xml

like image 458
Raymond Chenon Avatar asked Mar 29 '26 02:03

Raymond Chenon


2 Answers

You can use this:

states.addState(new int[] {android.R.attr.state_pressed},
    new ColorDrawable(Color.parseColor("#e3bb87")));
like image 142
npace Avatar answered Apr 02 '26 22:04

npace


I think you are looking for ColorDrawable

you can do something like this:

StateListDrawable states = new StateListDrawable();
int color = 0xff00ff00;
states.addState(new int[] {android.R.attr.state_pressed},
     new ColorDrawable(color)); 
like image 38
Mr.Me Avatar answered Apr 02 '26 23:04

Mr.Me