Panos Bariamis
Panos Bariamis

Reputation: 4663

How to get a drawable identifier from code

Having a drawable i would like to find the identifier

Layout

<TextView
    ...
    android:drawablePadding="5dp"
    android:drawableLeft="@drawable/account_lock"
/> 

Activity

...
TextView tv = ...;
Drawable d = tv.getCompoundDrawables()[0];
int id = d.???????    

i have looked at getResources().getIdentifier(name, defType, defPackage) but i do not think it helps me to find it;

Upvotes: 0

Views: 134

Answers (2)

Stefan
Stefan

Reputation: 4705

The id should be accessible as R.drawable.account_lock

Upvotes: 1

Plasma
Plasma

Reputation: 1961

This is how I've done it. I don't know whether you need it to be in a subfolder of res as I have, though.

//In this example, I'm getting an id from the res/raw folder.
    int id = *activity.this*.getApplicationContext().getResources().getIdentifier(*filename*, "raw", *packagename*);

Upvotes: 0

Related Questions