Dynamically Get Drawables by Id

Dynamically get drawables by ID

Use getResources().getIdentifier() from your Context (e.g., Activity), but please cache the result if you will use it more than once. getIdentifier() is implemented on Resources.

For example:

int drawableId=getResources().getIdentifier("foo"+index, "drawable", getPackageName());

would return the value of R.drawable.fooN, where N is the number given by index.

For more, see this and this and this.

Get Drawable id for dynamically generated Resource Name/String

The method you are looking for is getIdentifier of the Resource class:

int i = datafab1.getInt("Level", 1);
int id= getResources().getIdentifier("fablvl"+i, "drawable", getPackageName());

How to get Android resource ID from dynamically-created (in Java) layer-list / LayerDrawable?

There is no such thing as an ID for a Drawable created at runtime. Those IDs refer to int fields in the R class, automatically created from the xml files.

Since the LayerDrawable constructor requires just a Drawable array, you can provide those Drawables made from any method. An example, would be the static method Drawable.createFromStream(InputStream is, String srcName).

http://developer.android.com/reference/android/graphics/drawable/Drawable.html#createFromStream%28java.io.InputStream,%20java.lang.String%29

dynamically getting all image resource id in an array

you can Use Reflection to achieve this.

import the Field class

import java.lang.reflect.Field;

and then write this in your code

Field[] ID_Fields = R.drawable.class.getFields();
int[] resArray = new int[ID_Fields.length];
for(int i = 0; i < ID_Fields.length; i++) {
try {
resArray[i] = ID_Fields[i].getInt(null);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

resArray[] now holds references to all the drawables in your application.

Drawable not displayed when id is loaded dynamically and application signed

Work Around

Hashmap of string names to R's ids

Reasoning

It seems there may have been some permission not available when trying to read the files (Only occurs in signed - even though these are app images). To work around this, instead of using a json file to load image files by name, A hashmap was created with strings to match R's ids.

Sample Code

//create hashmap
private HashMap<String,Integer> resources;

//fill hash map with resources
resources.put("FIRST",R.drawable.example_img_1);
resources.put("SECOND",R.drawable.example_img_2);

//simple method to access resources
public Integer getResourceId(String key){
if(resources.containsKey(key))
return resources.get(key);
return R.drawable.default_image;
}

How to set the image from drawable dynamically in android?

Try this:

String uri = "@drawable/myresource";  // where myresource (without the extension) is the file

int imageResource = getResources().getIdentifier(uri, null, getPackageName());

imageview= (ImageView)findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);

Is it possible to access R.drawable variables dynamically?

int id = getResources().getIdentifier(drawableName , type, package);

This will get you the ID of the resource you are looking for. With it, you can then access the resource from the R class.

How can i get images dynamically from drawable folder?

This solution feels a little bit hacky not only because it uses reflection but also because it relies heavily on being able to recognize your own drawable resources by matching their names against a certain pattern (so you should try to use really unique names for the pictures)

That being said, you can change your code as follows:

Keep the private int[] image; but initialize it in the Adapter's constructor:

SwipeAdapter(Context cx){
this.cx=cx;
buildImageArray();
}

with a new method (note: you have to import java.lang.reflect.Field;)

private void buildImageArray() {

// this will give you **all** drawables, including those from e.g. the support libraries!
Field[] drawables = R.drawable.class.getDeclaredFields();
SparseIntArray temp = new SparseIntArray();
int index = 0;
for (Field f : drawables) {
try {
System.out.println("R.drawable." + f.getName());
// check the drawable is "yours" by comparing the name to your name pattern
// this is the point where some unwanted drawable may slip in,
// so you should spend some effort on the naming/ matching of your pictures
if (f.getName().startsWith("image")) {
System.out.println("R.drawable." + f.getName() + "==========================================");
int id = cx.getResources().getIdentifier(f.getName(), "drawable", cx.getPackageName());
temp.append(index, id);
index++;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
image = new int[index];
for (int i = 0; i < index; i++) {
image[i] = temp.valueAt(i);
}
}


Related Topics



Leave a reply



Submit