How to change the background of a button when clicked?

Asked

Viewed 1,215 times

4

I need to change the background of a button when clicked. Here’s 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 image, but it is not happening. Besides main, I should 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" />

1 answer

5


You are not changing the background button. To do 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 method getDrawable() was considered obsolete in API 22.

If minSdk is 21 or higher use

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

If lower 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 plays the behavior of a button with two states: on/off.

For it 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 as background this selector:

<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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.