Corinto Fernando Luli
Corinto Fernando Luli

Reputation: 13

How do I store several .jpg and .png files in an array? Android Studio

So here is a full explanation. Currently, I'm building an Android app wherein there will be 3 to 5 images at a time. Then, after pressing an imagebutton (like ¨Forward¨ or ¨Next¨), another set of 3 to 5 images will appear. The set of images will be as follows: first, a .jpg image at the background; second, an smaller .png image in the middle; third, an even smaller .png image up front; along with an imagebutton. And so on.

I'm using Android Studio. I know that I should connect an array in the .java file with ImageView in the .xml file using an object. But, I still can't picture the code in my head.

I thought of using an array, otherwise my .xml file will be massive. I assume. I will do some trial-and-error and post my results, but any help is welcomed.

Upvotes: 1

Views: 4301

Answers (3)

funcoder
funcoder

Reputation: 1975

At first, create a list of all image resources:

List<Integer> myImages = Arrays.asList(R.drawable.image1, R.drawable.image2, R.drawable.image3);

Wherever in your code, just access a random or a specific image as follows:

int currentIndex = 0; // your index
myImageView.setImageResource(bgImageList.get(currentIndex));

Upvotes: 0

Sash_KP
Sash_KP

Reputation: 5591

Store the images inside res/drawable folder. Suppose the name of the images will be , a.jpg a1.png a2.png. Now in your java code you can create an integer array like

 int icons[] = {R.drawable.a, R.drawable.a1, R.drawable.a2};

Now you can use these values according to your requirements.

Upvotes: 1

nvinayshetty
nvinayshetty

Reputation: 3237

save the image names which are there in res/drawable folder to an array of strings, and set the ImageView src by the names present in that array.

Upvotes: 0

Related Questions