Reputation: 232
Have a question! getDrawable() is deprecated in API 22. So, if I make an app with the min API 16, how can I set an image?
I saw that I can use getDrawable(int id, theme) , but this was added in API 21, so I can't use it.
I've tried setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.drawableName, null))
but this doesn't work either. Also with ContextCompat or getApplicationContext() it doesn't work.
private ImageView weatherIcon;
weatherIcon=(ImageView)findViewById(R.id.weatherIcon);
if(weatherReportList.size()>0){
DailyWeatherReport report=weatherReportList.get(0);
switch (report.getWeather()){
case DailyWeatherReport.WEATHER_TYPE_CLOUDS:
weatherIcon.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.cloudy, null));
weatherIconMini.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.cloudy, null));
case DailyWeatherReport.WEATHER_TYPE_RAIN:
weatherIcon.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.rainy));
weatherIconMini.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.rainy, null));
default:
weatherIcon.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.sunny, null));
weatherIconMini.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.sunny, null));
}
This is not a duplicate of that post. I've tried all the methods from there but none on them worked.
Upvotes: 3
Views: 15412
Reputation: 232
It seems that my project has a bug, but I can't identify it. If I try on other projects, it works with any of the answers above.
Upvotes: 0
Reputation: 4917
you can use
yourImageView.setImageDrawable(ContextCompat.getDrawable(context, /*your drawable like R.drawable.drawableName*/));
Upvotes: 7
Reputation: 4345
You can try this way,
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return context.getResources().getDrawable(resource);
} else {
return context.getResources().getDrawable(resource, null);
}
may helps you
Upvotes: 2