dramaticlook
dramaticlook

Reputation: 713

Android: Accessing list of local resources in Android

How can I access the list of drawables under my project "res/drawables"? Im new to Android I've seen an example like this:

Field[] drawables = android.R.drawable.class.getFields();
for (Field f : drawables) {
    try {
        System.out.println("R.drawable." + f.getName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

But this lists all the default drawables, not what I want:( I want to access those files and have their names to generate an xml, because my layout will be dynamic (number of drawables may differ from time to time).

Thanks for any help.

Upvotes: 0

Views: 1297

Answers (3)

chedine
chedine

Reputation: 2374

One suggestion would be to follow a naming convention for all your drawables. Say prefix them with your app name 'app'.

for(Field f:drawables){
    if(f.getName().startsWith("app")){
       //Your drawable.
    }
}

Upvotes: 0

Mojo Risin
Mojo Risin

Reputation: 8142

Replace

android.R.drawable.class.getFields();

with

your.application.package.R.drawable.class.getFields();

to access your drawables.

Upvotes: 1

rf43
rf43

Reputation: 4405

You are probably wanting Context.getResources...

Accessing Resources

Upvotes: 0

Related Questions