Dynamic Resource Loading Android

Dynamic Resource Loading Android

I haven't used it with raw files or xml layout files, but for drawables I use this:

getResources().getIdentifier("fileX", "drawable","com.yourapppackage.www");

to get the identifier (R.id) of the resource. You would need to replace drawable with something else, maybe raw or layout (untested).

How to access resource with dynamic name in my case?

int id = getResources().getIdentifier(imageName, 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.

Using only the name parameter:

You can also include all the 3 info in the "name" parameter using the following format: "package:type/image_name", something like:

int id = getResources().getIdentifier("com.my.app:drawable/my_image", null, null);

This is useful when you're working with external components or libraries that you can't, or don't want to, change how getIdentifier() is called. e.g.: AOSP Launcher3

What does dynamically modify resource tables exactly mean in Android Application

The resource table is the file named resources.arsc in an APK. Some tools may edit this file after it's been generated by the build system for various purposes.

Because the Android App Bundles contains this file under another format, these tools may not work if they haven't added support for the App Bundle. That's what this message means.

Dynamic loading of images R.Drawable using variable

to find the control:

ImageView image = (ImageView) findViewById(R.id.rockId);

To dynamicly load an image from drawable i use this function

    public static int getDrawable(Context context, String name)
{
Assert.assertNotNull(context);
Assert.assertNotNull(name);

return context.getResources().getIdentifier(name,
"drawable", context.getPackageName());
}

this will return the id of your drawable, now all you need to to is set the image in the control:

image.setImageResource(int Id);


Related Topics



Leave a reply



Submit