Android java change drawable image

Okay, so when do I press a button? he will change the image to another image

my xml of the image I want to change when I click the button:

<ImageView android:id="@+id/bluebtn1" android:src="@drawable/buttonblue" android:layout_width="60dp" android:layout_height="60dp" android:layout_marginLeft="33dp" android:layout_marginTop="140dp"></ImageView> 

and here is my java code not working well when you click the button

  case R.id.redbtn1: if (lvl1.getText().equals("1")) { lvl1.setText("2"); // Here is where the code goes to change the image } else { Toast.makeText(main.this, "YOU LOSE!", Toast.LENGTH_SHORT).show(); } break; 
+4
source share
1 answer

Put this in your click listener.

 ImageView blueBtn = (ImageView)findViewById(R.id.bluebtn1); blueBtn.setImageResource(R.drawable.YOUR_NEWIMAGE); 

That should work.

+17
source

All Articles