AndreasK AndreasK - 1 year ago 119
Android Question

Set variable for R.drawable

At the moment I set a marker image for google maps on android like this:


is the name of the corresponding to a image called
in drawable folder.

How can I do it like this:

String imagename="blablaimage";


Answer Source

It is not possible to do what you suggested.

Instead, a possible workaround might be to use the following function:

public int getDrawableId(String name){
        try {
            Field fld = R.drawable.class.getField(name);
            return fld.getInt(null);
        } catch (Exception e) {
        return -1;

and use like:

 String imagename="blablaimage";

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download