Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android java change drawable image

Tags:

java

android

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;
like image 316
Hailei Edwards Avatar asked Dec 01 '22 01:12

Hailei Edwards


1 Answers

Put this in your click listener.

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

This should work.

like image 168
blessanm86 Avatar answered Dec 03 '22 16:12

blessanm86