Sultan Fikri
Sultan Fikri

Reputation: 23

Change image of button when pressed, make image of button not change when released

When I press the button image changes, but my problem is when I release the button, the image changes again (does not retain images). I am new to Java programming.

My layout :

 <Button
    android:background="@drawable/volume"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"/>

And this is my XML in drawable:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
    android:drawable="@drawable/volume_off" /> <!-- pressed -->

<item android:drawable="@drawable/volume_on" /> <!-- default -->

Upvotes: 1

Views: 202

Answers (4)

Quang Nguyen
Quang Nguyen

Reputation: 2660

In your case, you want to change your volume indicator, I think you should do programmatically like this.

  //At the top of your activity,declare a global variable
  boolean isVolumeOn = true;

  volumeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //check your volume status
                    if(isVolumeOn){
                        view.setBackgroundResource(R.drawable.volume_off);
                        isVolumeOn = false;
                        //Turn off your volume here

                    }else{
                        view.setBackgroundResource(R.drawable.volume_on);
                        isVolumeOn = true
                        //Turn on your volume here

                    }
                }
            });

Upvotes: 1

Sultan Fikri
Sultan Fikri

Reputation: 23

in my content_main.xml

 <Button
    android:id="@+id/button"
    android:background="@drawable/volume_on"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:onClick="click" />

in my MainActivity.java

public Class myActivity extends AppCompatActivity{

Button volume; boolean isPressed=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myactivity_layout);
    volume = (Button) findViewById(R.id.button);
}

public void click(View v){
if (isPressed) {
    volume.setBackgroundResource(R.drawable.volume_off);
    Toast.makeText(this, "Volume Off",
            Toast.LENGTH_SHORT).show();
}else{
    volume.setBackgroundResource(R.drawable.volume_on);
    Toast.makeText(this, "Volume On",
            Toast.LENGTH_SHORT).show();
}
    isPressed=!isPressed;
}

Upvotes: 0

Alessandro Argentieri
Alessandro Argentieri

Reputation: 3215

If you want to change the image of your button permanently when you click it, it convenient to do it programmatically, inside the onClick void of your button. For example if

image1: R.drawable.image_first image2: R.drawable.image_second

and in your XML layout file you have:

 <Button
    android:background="@drawable/volume"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:onClick="click"/>

in the Activity you can do:

public Class myActivity extends Activity{

Button mButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myactivity_layout);
    mButton = (Button) findViewById(R.id.button);
}

public void click(View v){
   mButton.setBackgroundResource(R.drawable.image_second);
}

}

If you want to come back to the previous image after clicking again, just save the state of your button (boolean clicked = true/false) and put an if statement inside the public void click.

Upvotes: 0

Benjamin
Benjamin

Reputation: 2286

You want to remove default image from selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/volume_off" /> <!-- pressed -->
</selector>

remove this line <!--<item android:drawable="@drawable/volume_on" /> remove default-->

it is set default when button state is changed.

<Button
android:background="@drawable/volume"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@drawable/volume_on"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>

Upvotes: 0

Related Questions