An imageview receives an image according to the selected option

0

I wish you could enlighten me in one situation. I want to change the image of an ImageView. I can even do it through:

final ImageView imageView = (ImageView) findViewById(R.id.imageChm);
imageView.setImageResource(R.drawable.img1);

However, I have some more images (img1, img2, img3, img4 ... img 50) I would like them to use the same space, so I decide to adapt all the imageView only, in my case the imageChm, each image would be indicated by a button. Ex: when clicking the button1 the image that would appear would be img1, when clicking the button2 the image will be img2 and so on. How can I change the address of the image (R.drawable.img1) de forma dinamica ao clicar nos botões respectivos.

REMEMBER THAT IN MY CASE THE BUTTON AND IMAGEVIEW ARE IN ACTIVITY DIFFERENT

    
asked by anonymous 27.09.2017 / 17:34

1 answer

0

Move a onClickListener on each button.

When you enter this method, you will see the image.

For example, in% button% 1:

   btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        imageView.setImageResource(R.drawable.img1);
    }
});  

On button 2:

    btn2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        imageView.setImageResource(R.drawable.img2);
    }
});

And so on.

    
27.09.2017 / 18:27