android - How to dynamically define name of R.drawable -


this question has answer here:

is there way generate r.drawable.... name on fly? want set background image dynamically generated name ( usable r.drawable.example_graphic)

so want find way assign string $abc -> btn.setbackgroundimage(r.drawable.$abc);

i don't want create new drawable, want use existing one.

yes. drawables constants @ compile time: know exists. so, in application object, create map:

public static final map<string, integer> named_resources; static {   map<string, integer> m = new hashmap<string, integer>();   m.put(key1, r.drawable.abc);   m.put(key2, r.drawable.def);   // ...   named_resources = collections.unmodifiablemap(m); } 

now can:

btn.setbackgroundimage(application.named_resources.get($abc));


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -