An imageview receive image according to the selected option

Asked

Viewed 668 times

0

I wish you could enlighten me on a situation. I want to change the image of an Imageview. I can even do it through the:

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

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

REMEMBERING THAT IN MY CASE THE BUTTON AND IMAGEVIEW ARE IN DIFFERENT ACTIVITIES

1 answer

0

Arrow one onClickListener on each button.

When entering this method, arrow the image.

For example, in the onClickListener of button 1:

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

In button 2:

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

And so on and so forth.

  • I ran two tests.. one with a button inside the Activity and it worked, another test was with the button in another Activity and in this case it didn’t work... you know why this happened?

  • That’s another question... But, if you want to press the button in an Action to change in another, you will have to create a class and set a property.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.