Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawable resource from custom attribute

Tags:

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?

like image 770
Procurares Avatar asked Jul 29 '13 22:07

Procurares


People also ask

What are drawable resources?

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.

Which function is used to load a drawable image resource?

Drawable myImage = ResourcesCompat. getDrawable(res, R. drawable. my_image, null);

How do you reference drawable android?

In your Android/Java source code you can also refer to that same image like this: Resources res = getResources(); Drawable drawable = res. getDrawable(R.


2 Answers

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

like image 178
EdgarK Avatar answered Oct 20 '22 13:10

EdgarK


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)

like image 33
loeschg Avatar answered Oct 20 '22 13:10

loeschg