Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamically getting all image resource id in an array

Tags:

android

there are many images in drawable foder so instead manually creating array of all image resource ids , i want to get all images dynamically of drawable folder in array. currently i m using this code:

for(int i=1;i<=9;i++)
    {
            int imageKey = getResources().getIdentifier("img"+i, "drawable", getPackageName());
            ImageView image = new ImageView(this);  
            image.setId(imgId);
            image.setImageResource(imageKey);    
            image.setScaleType(ImageView.ScaleType.FIT_XY);
            viewFlipper.addView(image, new LayoutParams(LayoutParams.FILL_PARENT,              LayoutParams.FILL_PARENT));
            imgId++;
        }

but in that code i need to manually edit the image name to get the resource id but i want to get all image with any name..

like image 720
Kailash Dabhi Avatar asked Sep 05 '12 05:09

Kailash Dabhi


People also ask

How to create a dynamic array in C++?

In C++, we can create a dynamic array using the new keyword. The number of items to be allocated is specified within a pair of square brackets. The type name should precede this. The requested number of items will be allocated. The new keyword takes the following syntax: The pointer_variable is the name of the pointer variable.

How do you determine the size of a dynamic array?

In dynamic arrays, the size is determined during runtime. Dynamic arrays in C++ are declared using the new keyword. We use square brackets to specify the number of items to be stored in the dynamic array. Once done with the array, we can free up the memory using the delete operator.

How to declare a dynamic array in JavaScript?

In Javascript, Dynamic Array can be declared in 3 ways: 1. By using literal var array= ["Hi", "Hello", "How"]; 2. By using the default constructor var array= new Array (); 3. By using parameterized constructor

How do I initialize a dynamic array using an initializer list?

We can initialize a dynamic array using an initializer list. Let’s create an example that demonstrates this. Here is a screenshot of the code: Include the iostream header file into our program to use its functions. Include the std namespace in our program to use its classes without calling it.


1 Answers

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.

like image 185
Vinay W Avatar answered Sep 18 '22 18:09

Vinay W