MohanRaj S
MohanRaj S

Reputation: 2008

cannot SetBackground Resource image for android buttons

I want button with two states:

  1. OFF
  2. ON(or click).

I have set normal image in the button background and I am trying to change image(pressed) from onClick method, but it doesn't change.

final Button button1 = (Button) findViewById(R.id.button1);

button1.setOnClickListener(new OnClickListener() {

private int flag;

@Override
public void onClick(View v) {
    if(flag==1)
    {
      button1.setBackgroundResource(R.drawable.on);
          flag=0;
    }
    if(flag==0)
    {
          button1.setBackgroundResource(R.drawable.off);
          flag=1;
    }

}
});

Upvotes: 1

Views: 9829

Answers (6)

vir us
vir us

Reputation: 10735

There is another option: you can use android.widget.ToggleButton with selectors:

Code:

ToggleButton toggleButton = (ToggleButton) view.findViewById(R.id.toggleButton);
toggleButton.setChecked(true/false);

Main layout with snippet:

....
<ToggleButton android:id="@+id/toggleButton"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textOn=""
              android:textOff=""
              android:background="@drawable/toggle_states"/>
....

res/drawable/toggle_states.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/icon_alarm_on"></item>
    <item android:state_checked="false" android:drawable="@drawable/icon_alarm_off"></item>
</selector>

Android will set drawable/icon_alarm_on or drawable/icon_alarm_off automatically depending on state which was set with ToggleButton.setChecked() method

Upvotes: 4

Amrit
Amrit

Reputation: 2333

As you are already passing View in the function call you may directly use the view rather than call button1. It is more accurate this way. I used it in my application:

@Override
public void onClick(View v) {
if(flag==1)
{
      v.setBackgroundResource(R.drawable.on);
      flag=0;
}
else 
{
      v.setBackgroundResource(R.drawable.off);
      flag=1;
}
}

Upvotes: 2

Scott Helme
Scott Helme

Reputation: 4799

The below is more along the lines of what you need but there are some other considerations.

Do you need to remember the state across app restarts/config changes. If so you need to use shared preferences to store the state and retrieve it.

The flag variable, I think, needs to be declared outside the listener or it will be reset every time a click event occurs and you will lose the state. Have a play around with it. Should get you closer:

final Button button1 = (Button) findViewById(R.id.button1);
private int flag = 0;

button1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    if(flag==1)
    {
      button1.setBackgroundResource(R.drawable.on);
           flag=0;
    }else if(flag==0)
    {
          button1.setBackgroundResource(R.drawable.off);
          flag=1;
    }

}
});

Upvotes: 2

TNR
TNR

Reputation: 5869

Your requirements suits to ToggleButton and not just Button. you can change your button to Toggle button and setOnCheckedChangedListener() for handling on and off states.

Use below code

<ToggleButton 
        android:id="@+id/toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/check"   //check.xml
        android:textOn=""
        android:textOff=""/>

and prepare a drawable xml and store it in drawable folder. name the xml file check.xml

your check.xml code should be as below.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/your_switched_on_image"
          android:state_checked="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawableyour_switched_off_image"
        android:state_checked="false"/>

 </selector>

Upvotes: 3

Sunil Kumar
Sunil Kumar

Reputation: 7082

you can try with else part

 @Override
public void onClick(View v) {
    if(flag==1)
    {
      button1.setBackgroundResource(R.drawable.on);
          flag=0;
    }
    else 
    {
          button1.setBackgroundResource(R.drawable.off);
          flag=1;
    }
}
});

Upvotes: 2

Blackbelt
Blackbelt

Reputation: 157457

  if(flag==1)
    {
      button1.setBackgroundResource(R.drawable.on);
          flag=0;
    }
    else if(flag==0)
    {
          button1.setBackgroundResource(R.drawable.off);
          flag=1;
    }

you need an else if , otherwise when flag = 1, you set the background to R.drawable.on and flag=0;. The next instruction will be if (flag == 0) which is true, and you put again button1.setBackgroundResource(R.drawable.off);

Upvotes: 4

Related Questions