android - How can I convert String to Drawable -


I have several icons in the Drawable folder and I have string names. How can I access a worthy folder and change background image (or any view) Dynamically use these names Thanks

This can be done by reflection:

  string name =" your_drawable "; Final Field Field = R.drawable.getField (Name); Int id = field. GetInt (empty); Drawable Drauble = Miller Resource (). Drawer (id);  

or using the resource .getIdentifier () :

  string name = "your_drawable"; Int id = getResources (). GetIdentifier (name, "drauble", getPackageName ()); Drawable Drauble = Miller Resource (). Drawer (id);  

Then use this to set the drawables in any case:

  view.setBackground (dromb)  
< / Div>

Comments