How do I change the background of a button when clicked?

4

I need to change the background of a button when it is clicked. Here is the code I tried:

private Button btn;
private int colorFlag = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tela2);

    btn = (Button) findViewById(R.id.button7);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (colorFlag == 0) {
                btn.getResources().getDrawable(R.drawable.curisoidade_desligado);
                colorFlag = 1;
            } else {
                btn.getResources().getDrawable(R.drawable.curisoidade);
                colorFlag = 0;
            }
        }

When the id button (button7) is clicked, it should be switched to the off-hook image, however, it is not occurring. In addition to main, should I add something in xml?

Xml:

 <Button
    android:id="@+id/button7"
    android:layout_width="32dp"
    android:layout_height="37dp"
    android:layout_marginEnd="47dp"
    android:background="@drawable/curisoidade"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    app:layout_constraintHorizontal_bias="0.97"
    app:layout_constraintVertical_bias="0.087" />
    
asked by anonymous 30.05.2017 / 16:39

1 answer

4

You are not changing the background of the button. For this you must use the method setBackground()

Instead of

btn.getResources().getDrawable(R.drawable.curisoidade_desligado);

use

btn.setBackground(getResources().getDrawable(R.drawable.curisoidade_desligado));

Note:

The getDrawable() method was considered obsolete in API 22.

If minSdk is 21 or greater use

btn.setBackground(getResources().getDrawable(R.drawable.curisoidade_desligado, getTheme()));

If you are down and using the compatibility library use

btn.setBackground(ContextCompat.getDrawable(this, R.drawable.curisoidade_desligado))

However the "correct" would be to use a ToggleButton , since it reproduces the behavior of a button with two states: on / off.

To use your images, create a selector in the drawable folder:

toggle_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/R.drawable.curisoidade" android:state_checked="true"/>

    <item android:drawable="@drawable/R.drawable.curisoidade_desligado" android:state_checked="false"/>
</selector>

In the layout, replace the Button with a ToggleButton and use this selector as the background:

<ToggleButton
    android:id="@+id/button7"
    android:layout_width="32dp"
    android:layout_height="37dp"
    android:layout_marginEnd="47dp"
    android:background="@drawable/toggle_selector"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    app:layout_constraintHorizontal_bias="0.97"
    app:layout_constraintVertical_bias="0.087" 
    android:textOff=""
    android:textOn=""

/>

In Java, you no longer need to use that if , the image change will be automatic.

    
30.05.2017 / 16:55