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
Installation Failed with Message Invalid File
Android Toolbar Adding Menu Items for Different Fragments
Android: How to Convert Whole Imageview to Bitmap
How to Perform a Fade Animation on Activity Transition
View with Horizontal and Vertical Pan/Drag and Pinch-Zoom
Google Maps API Version Difference
Cursorloader Not Updating After Data Change
Failure [Install_Failed_Invalid_Apk]
Android - Hide All Shown Toast Messages
Android Seekbar with Two Thumbs
Why Do I Get a Emulator-5554 Disconnected Message
How to Use Bluetooth in Android Emulator
Disable or Prevent Multitouch in Activity
Android - Receiving Long Sms (Multipart)
How to Change the Android Startactivity() Transition Animation
Android - Apply Selectableitembackground in Xml with Support V7
How to Correctly Use Google Plus Sign in with Multiple Activities