how to get images dynamically from drawable folder
You can use getIdentifier()
for (int j = 1; j < 6; j++) {
Drawable drawable = getResources().getDrawable(getResources()
.getIdentifier("d002_p00"+j, "drawable", getPackageName()));
}
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);
}
}
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);
How to put images in drawable folder dynamically
You can check density of the device by this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenDensity = metrics.densityDpi;
and choose image based on the density.
Edited
or you can also do this :
float density = getResources().getDisplayMetrics().density;
// return 0.75 if it's LDPI
// return 1.0 if it's MDPI
// return 1.5 if it's HDPI
// return 2.0 if it's XHDPI
// return 3.0 if it's XXHDPI
// return 4.0 if it's XXXHDPI
Setting an ImageView from the drawable folder dynamicly
Solution:
Just use this code in java to set the drawable image to ImageView dynamically:
your_image_view_object.setImageDrawable(ContextCompat.getDrawable(Your_Activity.this, R.drawable.your_drawable_image));
For Example:
imageView.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_profilepic));
That's it. Hope it helps.
Related Topics
Error:Cause: Unable to Find Valid Certification Path to Requested Target
How to Resolve Java.Util.Zip.Zipexception
How to Set Shadow to a View in Android
How to Get the Position Selected in a Recyclerview
Configuration with Name 'Default' Not Found. Android Studio
Install_Failed_Shared_User_Incompatible While Using Shared User Id
Android Toggle Button Custom Look
Android Resources$Notfoundexception: Resource Id #0X7F030027
Load an Image from Assets Folder
Using Listview:How to Add a Header View
How to Run Countdowntimer in a Service in Android
Why Does Alertdialog.Builder(Context Context) Only Accepts Activity as a Parameter
How to Handle Outofmemoryerror
Could Not Find Method Android() for Arguments
Passing Touch Events to the Parent View