Cannot create SetBackground resource image for Android buttons

I want a button with two states:

  • Off
  • ON (or press).

I set the normal image on the background of the button, and I'm trying to change the image (click) from the onClick method, but it does not 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; } } }); 
+4
source share
6 answers

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); 

The main layout with a fragment:

 .... <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 / draw / 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 automatically set drawable / icon_alarm_on or drawable / icon_alarm_off depending on the state that was set using the ToggleButton.setChecked() method

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

you will need else if , otherwise, when flag = 1, you will set background R.drawable.on and flag=0; . The following statement will be if (flag == 0) , which is true, and you again set button1.setBackgroundResource(R.drawable.off);

+4
source

Your requirements match ToggleButton , not just Button . You can change your button to a Toggle button and setOnCheckedChangedListener() to control 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 flexible xml and save it in a folder with the ability to transfer. 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> 
+3
source

you can try with some more

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

Below is what you need, but there are other considerations.

You need to remember the state through application reboot / configuration changes. If so, you need to use the general settings to save the state and get it.

The flag variable, I think, should be declared outside the listener or it will be reset every time a click event occurs and you lose state. Play with him. If you are 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; } } }); 
+2
source

As you already go through View in a function call, you can directly use View instead of calling button1 . This is more accurate. 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; } } 
+2
source

Source: https://habr.com/ru/post/1492021/


All Articles