Gexo Gexo - 4 months ago 8
Android Question

Randomly select one of two ImageViews and update ResourceImage

I have created in my layout two ImageViews, let's call them

imageviewTop
and
imageviewBottom
.

I saved two images into the drawable (green_image.png and red_image.png).

I also added a button and want I would like to do is, when the button is clicked, one of the ImageViews will get selected randomly and from the green_image it will change to the red_image.

I already tried with creating a switch/case statement and generating a random number, like 1 or 2.

Based on this number the case statement would update either the top or bottom image.

This is working fine for 2 ImageViews, but in case I would have 100, I would need to create 100 cases in code.

I am searching for a more dynamic option.

I know how to update the image for the ImageView, I am struggling with the part, on how to select one ImageView randomly, if it is possible.

Here is the code:

public class MainActivity extends Activity {

ImageView imagevieTop, imageviewBottom;
Button randomButton;

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

imagevieTop = (ImageView) findViewById(R.id.imageViewTop);
imageviewBottom = (ImageView) findViewById(R.id.imageViewBottom);
randomButton = (Button) findViewById(R.id.buttonRandom);

randomButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// randomly select one of the two imageviews
// for example: randomly selected imageviewTop
// set imageresource red_image to imageviewTop

//at next start up it would select either top or bottom, 50%-50% and then assign the image to it
}
});
}
}

Answer

You could just use one ImageView, and randomize the picture you draw.

Alternatively, you could adjust this to use an array of ImageViews. Your choice.

The line you want, though, is int index = random.nextInt(imgs.length); to get a random index from the list.

public class MainActivity extends Activity {

    int[] imgs = new int[] { R.drawable.green_image, R.drawable.red_image };
    Button randomButton;

    private final Random random = new Random();

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

        final ImageView imgView = (ImageView) findViewById(R.id.imageView);
        randomButton = (Button) findViewById(R.id.buttonRandom);

        randomButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int index = random.nextInt(imgs.length);

                // randomly select one of the two drawables
                int resId = imgs[index];

                // set imageresource imgView
                Drawable d = getResources().getDrawable(resId);
                imgView.setImageDrawable(d);

            }
        });
    }
}

To handle the "100 ImageViews" problem, I'd recommend not copying 100 lines of code, and instead looping over reasonable ID values.

List<ImageView> imgViews = new ArrayList<ImageView>();
for (int i = 0; i < 100; i++) {
    int resId = getResources().getIdentifier("imgView" + i, "id", getPackageName());
    ImageView nextImg = (ImageView) findViewById(resId);
    imgViews.add(nextImg);
}