2

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.

enter image description here

enter image description here enter image description here

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);
            }
        }
    }
Harshad Pansuriya
  • 17,218
  • 7
  • 58
  • 86
jordan
  • 1,343
  • 1
  • 18
  • 45

3 Answers3

2

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

Community
  • 1
  • 1
Ramit
  • 416
  • 3
  • 8
  • Thank you. :) I modified my code with ContextCompat. I'm not getting the warning anymore and program is still working. – jordan Aug 28 '16 at 04:49
1

getDrawable() is deprecated in API level 22.

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

Community
  • 1
  • 1
Harshad Pansuriya
  • 17,218
  • 7
  • 58
  • 86
  • Thanks. It's working. Now I don't have to use deprecated syntax / method. I appreciate the help. – jordan Aug 28 '16 at 04:54
  • @p3ace glad to help you. If you have any problem ask free. – Harshad Pansuriya Aug 28 '16 at 04:56
  • 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-) – jordan Aug 28 '16 at 04: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 '16 at 05: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 '16 at 05:14
  • @Ramit sorry for the late reply ya say what you ask. – Harshad Pansuriya Aug 28 '16 at 05:17
  • @Ramit whenever you are working some code is deprecated you have check for the Version as I mention in the answer so it is good practice to work with it. – Harshad Pansuriya Aug 28 '16 at 05:19
  • @Ironman check the internal code on ContextCompat and will get to know that it has all necessary check. I already read your comment that you edited. It is the fair way? Please think yourself your doing right to accept your answer. You know very well that edit history also maintained by stackoverflow. I will request moderator to do necessary action against such acts. Thanks nothing personal. – Ramit Aug 28 '16 at 05:22
0

It worked for me.

ResourcesCompat.getDrawable(getResources(), R.drawable.name, null));

More details

Victor Sam VS
  • 51
  • 1
  • 4