android - How to dynamically define name of R.drawable -
this question has answer here:
- how image drawable folder in android? 4 answers
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
Post a Comment