Is there any possibility to get resource from drawable folder right in some custom attribute, so i can write:
<com.my.custom.View
android:layout_height="50dp"
android:layout_width="50dp"
...
my_custom:drawableSomewhere="@drawable/some_image" />
and then simple perform action with drawable inside my custom view class?
A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.
Drawable myImage = ResourcesCompat. getDrawable(res, R. drawable. my_image, null);
In your Android/Java source code you can also refer to that same image like this: Resources res = getResources(); Drawable drawable = res. getDrawable(R.
There is actually an attribute format called "reference". So you will get something like this in your custom views class:
case R.styleable.PMRadiogroup_images:
icons = a.getDrawable (attr);
break;
While having sometning like this in your attrs.xml:
<attr name="images" format="reference"/>
Where "a" is a TypedArray that you get from attributes taken from the views constructor.
There is a good similar answer here: Defining custom attrs
See EdgarK's answer; it's better. (I can't delete this since it's the accepted answer)
Does this answer your question?
"You can use format="integer", the resource id of the drawable, and AttributeSet.getDrawable(...)."
(From https://stackoverflow.com/a/6108156/413254)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With