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

I researched online and found related but not very similar information about comparing images and referring to images inside the drawable folder.

However, by following one of the answers I found in stackoverflow, I was able to compare two images and switch between them on button click.

I'm just concerned of the deprecated message I'm getting for getDrawable(int) .

What is really the correct way or syntax for getting drawable images and comparing them? It appears that getDrawable() isn't the correct one on that position.

Here's the code and screenshot of my working program.

private class myButtonListener implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            if(myImageView.getDrawable().getConstantState() ==
                    MainActivity.this.getResources().getDrawable(R.drawable.corgi).getConstantState() ){
                myImageView.setImageResource(R.drawable.goldenretriever);
            }else{
                myImageView.setImageResource(R.drawable.corgi);

Screenshot of crossed out getDrawable() method.

Though my program is working, I want to make sure to avoid errors in the future for instances of deprecated methods.

I think this is important because like I mentioned I googled and got different information of which method or syntax to use.

Here's the modification solution for other users who may encounter the same problem.

 private class myButtonListener implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            if(myImageView.getDrawable().getConstantState().equals(
                    ContextCompat.getDrawable(MainActivity.this,R.drawable.corgi).getConstantState() ) ){
                myImageView.setImageResource(R.drawable.goldenretriever);
            }else{
                myImageView.setImageResource(R.drawable.corgi);

Yes you need to change deprecated code:-

ContextCompat.getDrawable(getActivity(), R.drawable.name);

For detail and other answer you check Android getResources().getDrawable() deprecated API 22

Thank you. :) I modified my code with ContextCompat. I'm not getting the warning anymore and program is still working. – heisenberg Aug 28, 2016 at 4:49

Use ContextCompat for that visit this : https://developer.android.com/reference/android/support/v4/content/ContextCompat.html#getDrawable(android.content.Context,%20int)

Use this way.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    return resources.getDrawable(id, context.getTheme());
} else {
    return resources.getDrawable(id);

As mention Here. Android getResources().getDrawable() deprecated API 22

Thanks. It's working. Now I don't have to use deprecated syntax / method. I appreciate the help. – heisenberg Aug 28, 2016 at 4:54 thanks a lot. I just started learning Android from Java Swing (which is my first language). I think I'll enjoy Android more than Swing because I can bring the app with me on the phone or tablet. Anyways, I'll ask if I get stuck with something. B-) – heisenberg Aug 28, 2016 at 4:58 I have no personal problem for your answer but can you please explain why your answer is better than ContextCompat.getDrawable(getActivity(), R.drawable.name); Both are doing same thing internally. I suggest a single line change in place of writing four lines. If android doing itself code for you why you suggesting to rewrite and reinvent the wheel. – Ramit Aug 28, 2016 at 5:03 @Ironman you are not replying means you agree with me. So your answer is not better. I will request to moderator to check history of this thread and please do necessary action. – Ramit Aug 28, 2016 at 5:14

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.