Hailei Edwards
Hailei Edwards

Reputation: 111

Android java change drawable image

Alright so how when i click a button? it will change the a image to a different image

my xml of the image i want to change when button press:

<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 ill be doing when you press 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;

Upvotes: 4

Views: 16086

Answers (1)

blessanm86
blessanm86

Reputation: 31789

Put this in your click listener.

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

This should work.

Upvotes: 19

Related Questions