Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App (retired 2014) Learning the Language Objects and Random Numbers

How Do I Change An ImageView Another Image?

I don't know how to change the image on an ImageView and I want to know so I can make a button change the image when clicked.

2 Answers

Marcus Vieira
Marcus Vieira
7,877 Points

Hi Michael!

To change the image of an ImageView you can do the following:

ImageView imageView= (ImageView) findViewById(R.id.image_view);
imageView.setImageResource(R.drawable.my_image);

Now, in order to change the image when you click a button, you can do:

Button button = (Button) findViewById(R.id.imageButton1);

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
       imageView.setImageResource(R.drawable.new_image);
    }
});

I hope this helps :)

thanks!!

Thanks!!!