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.* package?
for example if drawableId was android.R.drawable.ic_delete
mContext.getResources().getDrawable(drawableId)
As of API 21, you should use the
getDrawable(int, Theme)
method instead ofgetDrawable(int)
, as it allows you to fetch adrawable
object associated with a particularresource ID
for the givenscreen density/theme
. Calling thedeprecated
getDrawable(int)
method is equivalent to callinggetDrawable(int, null)
.You should use the following code from the support library instead:
Using this method is equivalent to calling:
As of API 21, you could also use:
Instead of
ContextCompat.getDrawable(context, android.R.drawable.ic_dialog_email)
best way is
OR this for Drawable left and something like that for right etc.
and
getResources().getDrawable()
is now deprecated