Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

Though the getDrawable() is deprecated, but it could still work right now, so maybe something wrong in your code somewhere else – Harlan Jul 22, 2016 at 14:42 yes you can and you can also use getActivity() if it is fragment or yourActivity.this or getApplicationContext() if activity. – Deepak Goyal Jul 22, 2016 at 11:41 But this doesn't work either. :( Maybe I have a problem with my code, but I don't think so. – Andrei Pascale Jul 22, 2016 at 11:46 weatherIcon.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),R.drawable.rainy)); – Andrei Pascale Jul 22, 2016 at 12:08
 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    return context.getResources().getDrawable(resource);
} else {
    return context.getResources().getDrawable(resource, null);

may helps you

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.