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 trialmichaelpavle
555 PointsHow 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
7,877 PointsHi 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 :)
michaelpavle
555 PointsThanks!!!
michaelpavle
555 Pointsmichaelpavle
555 Pointsthanks!!