I need to get a Drawable object to display on an image button. Is there a way to use the code below (or something like it) to get an object from the android.R.drawable.* pac
From API 21 getDrawable(int id) is deprecated. So now you need to use
ResourcesCompat.getDrawable(context.getResources(), R.drawable.img_user, null)
But Best way to do is : You should create one common class for get drawable and colors because if any thing change or deprecate in future then you no need to change everywhere in your project.You just change in this method
object ResourceUtils {
fun getColor(context: Context, color: Int): Int {
return ResourcesCompat.getColor(context.getResources(), color, null)
}
fun getDrawable(context: Context, drawable: Int): Drawable? {
return ResourcesCompat.getDrawable(context.getResources(), drawable, null)
}
}
use this method like :
Drawable img=ResourceUtils.getDrawable(context, R.drawable.img_user)
image.setImageDrawable(img);