Skip to content Skip to sidebar Skip to footer

Android Resouce By Id / Change Image Onclick / No Change Of Imageview

I have a problem with my imageview not changing after picking an image from an array (xml resource), but falling back into the default value. Content of my MeasurementActivity.java

Solution 1:

You are defining your array wrong. and because your app can't find the correct item it goes back to the default image. a drawable is a id and an ID is an integer (you have strings) try this:

<?xml version="1.0" encoding="utf-8"?><resources><arrayname="random_imgs"><item>@drawable/tests1</item><item>@drawable/tests2</item><item>@drawable/tests3</item></array></resources>

And for retreiving the drawable

TypedArrayimgs= getResources().obtainTypedArray(R.array.random_imgs);

Randomr=newRandom();
inti= random.nextInt(imgs.size() -1);

// get resource ID by index
imgs.getResourceId(i, R.color.colorPrimaryDark)

// or set you ImageView's resource to the id
mImgView1.setImageResource(imgs.getResourceId(i, R.color.colorPrimaryDark));

// recycle the array
imgs.recycle();

Solution 2:

int chosenImageNumber = (int) (Math.random() * images.length());

I think the above line is the problem. You should use:

Match.random(images.length(-1));

Post a Comment for "Android Resouce By Id / Change Image Onclick / No Change Of Imageview"