1) Place the image (say cat1.jpg) under app/src/main/res/drawable folder
2) Add ImageView widget to the layout and edit the src attribute to point to the image file
app:srcCompat="@drawable/cat1"
Here, we ll demonstrate an example of changing the image file in the ImageView widget when a button is clicked.
1) Let the onClick function of the button widget be clickFunction
2) Let the id of the ImageView widget be imageView
3) Let the new image file name be cat2.jpg
boolean changed = false;
public void clickFunction(View view){
//Grab the ImageView widget
ImageView image = (ImageView) findViewById(R.id.imageView);
if(!changed) {
// Change attributes
image.setImageResource(R.drawable.cat2);
changed = true;
}else{
image.setImageResource(R.drawable.cat1);
changed = false;
}
}
Example of changing TextView widget (Currency Converter example)
TextView inr = (TextView) findViewById(R.id.editText2);
inr.setText("New text");