Goofy
Goofy

Reputation: 6128

how to get images dynamically from drawable folder

I have an array like this.

int image[] = {R.drawable.d002_p001,R.drawable.d002_p002,R.drawable.d002_p003,
                   R.drawable.d002_p004,R.drawable.d002_p005,R.drawable.d002_p006};

Right now I have 6 images so I am statically given the name.

If I have some 50 images I cant give each and every file name in array so it needs to be dynamic how can I achieve this.

Upvotes: 49

Views: 91653

Answers (10)

Chirag Prajapati
Chirag Prajapati

Reputation: 55

Extension function for Kotlin

fun Activity.getDrawable(drawableName: String): Drawable? {
    val drawableId = resources
        .getIdentifier(drawableName, "drawable", packageName)

    if (drawableId != 0) {
        return ResourcesCompat.getDrawable(resources, drawableId, null)
    }
    return null
}

Upvotes: 0

dilipkaklotar
dilipkaklotar

Reputation: 1469

use this code to create array and later use that array

int NUM_OF_IMAGES = 50;
String images[] = new String[NUM_OF_IMAGES];
for (int i =0; i < NUM_OF_IMAGES; i++) {
    images[i] = "R.drawable.d002_p00" + i;
}

main thing you have to take care is the file name must start with "d002_p00" this and after there is digit 1 to 50

Upvotes: -10

Anwar shanib
Anwar shanib

Reputation: 21

package com.example.studio.snakes;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

int[] dices = {
        R.drawable.one,
        R.drawable.two,
        R.drawable.three,
        R.drawable.four,
        R.drawable.five,
        R.drawable.six,
};


public void rollTapped(View view){
 Log.i("Button","Button Tapped");
    Random rand = new Random();
    int randomnum = rand.nextInt(6);
    Log.i("Random","Random number is " + randomnum );
    ImageView dice=findViewById(R.id.imageView2);
    dice.setImageResource(dices[randomnum]);


}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
}

Upvotes: 1

Lakshmanan
Lakshmanan

Reputation: 1669

We can take advantage of Imageview setImageResource as this will efficient than drawable seems, refer below code for the same.

The below code can be used to show the image like gif incase if you have the multiple split image of gif. Just split the gif into individual png from a online tool and put image in the drawable like the below order

image_1.png, image_2.png, etc.

Have the handler to change the image dynamically.

int imagePosition = 1;
    Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            public void run() {
                updateImage();
            }
        };




    public void updateImage() {

                appInstance.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        int resId = getResources().getIdentifier("image_" + imagePosition, "drawable", appInstance.getPackageName());
                        gifImageViewDummy.setImageResource(resId);
                        imagePosition++;
    //Consider you have 30 image for the anim
                        if (imagePosition == 30) {
//this make animation play only once
                            handler.removeCallbacks(runnable);

                        } else {
    //You can define your own time based on the animation
                            handler.postDelayed(runnable, 50);
                        }

//to make animation to continue use below code and remove above if else
// if (imagePosition == 30)
//imagePosition = 1;
// handler.postDelayed(runnable, 50);
// 
                    }
                });
              }

Upvotes: 0

Abbath
Abbath

Reputation: 1012

String[] names = new String []{"yout names..."};
    for(String n: names) {
        Utils.GetDrawableByName(n,this);
    }

public class Utils {
public static Drawable GetDrawableByName(String name,Activity context){
    Resources res = context.getResources();
    return res.getDrawable(res.getIdentifier(name,"drawable",context.getPackageName()));
    }
}

Upvotes: 2

SAMD
SAMD

Reputation: 431

Use the following line for getting drawable dynamically:

Drawable drawable = this.getResources().getDrawable(R.drawable.yourDrawableID);

This will give you the desired Drawable.

Upvotes: 5

Ali Imran
Ali Imran

Reputation: 9217

public static Drawable getImage(Context context, String name) {
        return context.getResources().getDrawable(context.getResources().getIdentifier(name, "drawable", context.getPackageName()));
}

Upvotes: 2

Lalit Poptani
Lalit Poptani

Reputation: 67296

You can use getIdentifier()

for (int j = 1; j < 6; j++) {
   Drawable drawable = getResources().getDrawable(getResources()
                  .getIdentifier("d002_p00"+j, "drawable", getPackageName()));
}

Upvotes: 116

Sunil_Suthar
Sunil_Suthar

Reputation: 1094

You can also use this:

int res = getResources().getIdentifier("<your pakecgename>:drawable/abc", null, null);

Upvotes: 13

blessanm86
blessanm86

Reputation: 31789

Something like this could work

Field[] drawables = android.R.drawable.class.getFields();
for (Field f : drawables) {
    try {
        System.out.println("R.drawable." + f.getName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Upvotes: 7

Related Questions