1

I have a mute button on my Android app. I would like to have 2 states for it: 1. sound on 2. sound off

For each state there is a different background:

Sound On

Sound Off

So when pressed I would like the Sound off image to be displayed and HELD. When pressed a second time I would like the Sound On image to display again.

Here is my code so far:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/soundoff"></item>
<item android:state_checked="true" android:drawable="@drawable/soundoff"></item>
<item android:drawable="@drawable/soundon"></item>

</selector>

The problem I am having is that although the background changes when the button is selected, I would like that background to be held until the next selection.

Mustafa
  • 755
  • 1
  • 7
  • 16

2 Answers2

1

OK I have solved this problem. Instead of reffering to drawable xml to ammend the button state I have coded the following in the class itself which now gives the desired result.

dmute.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) {
      if(!mutestatus){
        mutestatus = true;
        dmute.setBackgroundResource(R.drawable.soundoff);
      }
      else {
        mutestatus = false;
        dmute.setBackgroundResource(R.drawable.soundon);
      }
  }});

Thanks for everyone who helped.

PLNech
  • 3,087
  • 1
  • 23
  • 52
Mustafa
  • 755
  • 1
  • 7
  • 16
0

Are you sure the checked state of the button is being changed? Also I think that your selector should probably look more like this.

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:state_checked="false" android:drawable="@drawable/soundoff_pressed"></item>
<item android:state_pressed="true" android:state_checked="true" android:drawable="@drawable/soundon_pressed"></item>
<item android:state_pressed="false" android:state_checked="false" android:drawable="@drawable/soundoff"></item>
<item android:state_pressed="false" android:state_checked="true" android:drawable="@drawable/soundon"></item>

Bostwickenator
  • 380
  • 2
  • 11
  • Hi, yes defo the image is changing but just for a second. I tried your above code but it didn't work, it brings the sound off image to the front and when pressed it stays the same - no change to image. – Mustafa Jan 09 '13 at 00:02
  • @Mustafa the code above wouldn't work because `android:state_checked` isn't applicable to a `Button`. `android:state_selected` is the appropriate state you are looking for. – Abhijit Jan 09 '13 at 02:30
  • @Abhijit Hi, tried that also, the state does not change, it remains on Sound On. – Mustafa Jan 09 '13 at 12:14
  • I assumed you were using a http://developer.android.com/reference/android/widget/CheckBox.html go do that and my set will work. Don't use selected! Selected is as the name implies for views that have a user selection state (no not focus). – Bostwickenator Jan 10 '13 at 01:11
  • @Bostwickenator - I believe you are wrong when you say 'Don't use selected!'. This answer completely contradicts your statement - [ImageButton with selected state](http://stackoverflow.com/questions/2604599/android-imagebutton-with-a-selected-state) – Abhijit Jan 10 '13 at 15:20
  • @Abhijit You can use it. It will work. But selected is intended for lists of selected items. For example when you select 3 photos in a galley view or items in a list view. A CompoundButton http://developer.android.com/reference/android/widget/CompoundButton.html is the correct control to use here. Likewise checked is the right property. – Bostwickenator Jan 10 '13 at 22:38
  • Additionally look at the comment on the question you linked to, Andras suggests the same thing. – Bostwickenator Jan 10 '13 at 22:42
  • 1
    @Bostwickenator Yes, agreed. Your solution is more correct and elegant. Thanks for correcting me. – Abhijit Apr 16 '13 at 15:30