Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getColorStateList has been deprecated

I'm having a problem here. I've just updated from sdk 22 to 23, and the previous version of "getColorStateList()" has been deprecated.

My code was like this

seekBar.setProgressTintList(getResources().getColorStateList(R.color.bar_green));
valorslide.setTextColor(getResources().getColorStateList(R.color.text_green));

The older "getColorStateList" was

getColorStateList(int id)

And new one is

getColorStateList(int id, Resources.Theme theme)

How do I use the Theme variable? Thanks in advance

like image 636
fkchaud Avatar asked Sep 02 '15 17:09

fkchaud


2 Answers

While anthonycr's answer works, it is a lot more compact to just write

ContextCompat.getColorStateList(context, R.color.haml_indigo_blue);
like image 196
user1354603 Avatar answered Nov 05 '22 00:11

user1354603


The Theme object is the theme that is used to style the color state list. If you aren't using any special theming with individual resources, you can either pass null or the current theme as follows:

TextView valorslide; // initialize
SeekBar seekBar; // initialize
Context context = this;
Resources resources = context.getResources();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
    seekBar.setProgressTintList(resources.getColorStateList(R.color.bar_green, context.getTheme()));
    valorslide.setTextColor(resources.getColorStateList(R.color.text_green, context.getTheme()));
} else {
    seekBar.setProgressTintList(resources.getColorStateList(R.color.bar_green));
    valorslide.setTextColor(resources.getColorStateList(R.color.text_green));
}

If you don't don't care about the theme, you can just pass null:

getColorStateList(R.color.text_green, null)

See the documentation for more explanation. Note, you only need to use the new version on API 23 (Android Marshmallow) and above.

like image 42
anthonycr Avatar answered Nov 05 '22 00:11

anthonycr