Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an AlertDialogue object called dialog. I am attempting to add an icon to it. I see that this syntax is now deprecated:

dialog.setIcon(getResources().getDrawable(R.drawable.myImage);

I'm reading everywhere that this should work:

dialog.setIcon(ContextCompat.getDrawable(context, R.drawable.myImage));

However, the ContextCompat syntax is not being recognized by Android Studio. Is there something that I should be importing? Thank you.

***Update: Thank's to @Sharj for the correct answer below. I made a quick video too if you guys need a visual: https://www.youtube.com/watch?v=eFiaO0srQro&feature=youtu.be

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
506 views
Welcome To Ask or Share your Answers For Others

1 Answer

ContextCompat is part of support library v4. Have you added support library 4 to your project?

android.support.v4.content.ContextCompat

You can include support library to your build.gradle file under app folder if you haven't already

dependencies {
// other stuff here
    compile 'com.android.support:support-v4:23.0.0'
// update the 23.0.0 to latest version available

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...