Reputation: 83
I am working on a small project in Android Studio.
I have drawable added to res/drawable folder.
However I cannot get it from the code. I have tried different approaches:
ContextCompat.getDrawable(getActivity(), R.drawable.ic_training);
and
val drawable: Drawable? = ContextCompat.getDrawable(context.applicationContext, R.drawable.ic_training )
val drawable2: Drawable = R.drawable.ic_training
Both of them return an error unresolved reference. Here is my XML file:
<vector
android:id = "@+id/ic_training"
android:alpha="0.85" android:height="24dp"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"
android:name="ic_training" >
<path android:fillColor="#FF000000" android:pathData="M22,21c-1.11,0 -1.73,-0.37 -2.18,-0.64 -0.37,-0.22 -0.6,-0.36 -1.15,-0.36 -0.56,0 -0.78,0.13 -1.15,0.36 -0.46,0.27 -1.07,0.64 -2.18,0.64s-1.73,-0.37 -2.18,-0.64c-0.37,-0.22 -0.6,-0.36 -1.15,-0.36 -0.56,0 -0.78,0.13 -1.15,0.36 -0.46,0.27 -1.08,0.64 -2.19,0.64 -1.11,0 -1.73,-0.37 -2.18,-0.64 -0.37,-0.23 -0.6,-0.36 -1.15,-0.36s-0.78,0.13 -1.15,0.36c-0.46,0.27 -1.08,0.64 -2.19,0.64v-2c0.56,0 0.78,-0.13 1.15,-0.36 0.46,-0.27 1.08,-0.64 2.19,-0.64s1.73,0.37 2.18,0.64c0.37,0.23 0.59,0.36 1.15,0.36 0.56,0 0.78,-0.13 1.15,-0.36 0.46,-0.27 1.08,-0.64 2.19,-0.64 1.11,0 1.73,0.37 2.18,0.64 0.37,0.22 0.6,0.36 1.15,0.36s0.78,-0.13 1.15,-0.36c0.45,-0.27 1.07,-0.64 2.18,-0.64s1.73,0.37 2.18,0.64c0.37,0.23 0.59,0.36 1.15,0.36v2zM22,16.5c-1.11,0 -1.73,-0.37 -2.18,-0.64 -0.37,-0.22 -0.6,-0.36 -1.15,-0.36 -0.56,0 -0.78,0.13 -1.15,0.36 -0.45,0.27 -1.07,0.64 -2.18,0.64s-1.73,-0.37 -2.18,-0.64c-0.37,-0.22 -0.6,-0.36 -1.15,-0.36 -0.56,0 -0.78,0.13 -1.15,0.36 -0.45,0.27 -1.07,0.64 -2.18,0.64s-1.73,-0.37 -2.18,-0.64c-0.37,-0.22 -0.6,-0.36 -1.15,-0.36s-0.78,0.13 -1.15,0.36c-0.47,0.27 -1.09,0.64 -2.2,0.64v-2c0.56,0 0.78,-0.13 1.15,-0.36 0.45,-0.27 1.07,-0.64 2.18,-0.64s1.73,0.37 2.18,0.64c0.37,0.22 0.6,0.36 1.15,0.36 0.56,0 0.78,-0.13 1.15,-0.36 0.45,-0.27 1.07,-0.64 2.18,-0.64s1.73,0.37 2.18,0.64c0.37,0.22 0.6,0.36 1.15,0.36s0.78,-0.13 1.15,-0.36c0.45,-0.27 1.07,-0.64 2.18,-0.64s1.73,0.37 2.18,0.64c0.37,0.22 0.6,0.36 1.15,0.36v2zM8.67,12c0.56,0 0.78,-0.13 1.15,-0.36 0.46,-0.27 1.08,-0.64 2.19,-0.64 1.11,0 1.73,0.37 2.18,0.64 0.37,0.22 0.6,0.36 1.15,0.36s0.78,-0.13 1.15,-0.36c0.12,-0.07 0.26,-0.15 0.41,-0.23L10.48,5C8.93,3.45 7.5,2.99 5,3v2.5c1.82,-0.01 2.89,0.39 4,1.5l1,1 -3.25,3.25c0.31,0.12 0.56,0.27 0.77,0.39 0.37,0.23 0.59,0.36 1.15,0.36z"/>
<path android:fillColor="#FF000000" android:pathData="M16.5,5.5m-2.5,0a2.5,2.5 0,1 1,5 0a2.5,2.5 0,1 1,-5 0"/>
</vector>
Upvotes: 7
Views: 24951
Reputation: 1861
Didn't see this here, and it's what worked for me:
var myShape = ContextCompat.getDrawable(
applicationContext,
R.drawable.shape_rectangle //<-- name of xml file in res folder
)
Upvotes: 0
Reputation: 4671
Also in Kotlin you can use this way: (SDK 31)
val shape: Drawable? =
ResourcesCompat.getDrawable(resources, R.drawable.custom_progress_bar, null)
where null : drawables without theme attributes
However, This link is show google doc about it: https://developer.android.com/guide/topics/resources/drawable-resource#kotlin
Upvotes: 3
Reputation: 652
Android SDK 30
ContextCompat.getDrawable(this, R.drawable.name)
Upvotes: 13
Reputation: 2056
resources.getDrawable(R.drawable.ic_training, theme) is deprecated use this
ContextCompat.getDrawable(getActivity(), R.drawable.name)
Upvotes: 2
Reputation: 296
at first, check that drawable in the root drawable, not in xxx or something else
in your Activity do
val drawble = resources.getDrawable(R.drawable.ic_training,theme)
if in Fragment
val drawble = context!!.resources.getDrawable(R.drawable.ic_training,context!!.theme)
Upvotes: 6