'Android Studio set Background Color on Button isn't doing anything

I'm devlopping an android application in wich there is some button. The idea is to create a test with question above button and possible answer on the buttons.

I tried to color these buttons from a thread which is not the UI thread and the coloration is not effective but the program dont give any error. And that is the same by running on UI thread.

public void discoverAnswer(final String rep){
        this.sleeping = true;
        Log.d(TAG, "discoverReponse: step1");
        btn1.setBackgroundColor(btn1.getText().toString().equals(rep) ? Color.GREEN:Color.RED);
        btn2.setBackgroundColor(btn2.getText().toString().equals(rep) ? Color.GREEN:Color.RED);
        btn3.setBackgroundColor(btn3.getText().toString().equals(rep) ? Color.GREEN:Color.RED);
        btn4.setBackgroundColor(btn4.getText().toString().equals(rep) ? Color.GREEN:Color.RED);
        Log.d(TAG, "discoverReponse step2");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "discoverReponse: step3");
                btn1.setBackgroundColor(btn1.getText().toString().equals(rep) ? Color.GREEN:Color.RED);
                btn2.setBackgroundColor(btn2.getText().toString().equals(rep) ? Color.GREEN:Color.RED);
                btn3.setBackgroundColor(btn3.getText().toString().equals(rep) ? Color.GREEN:Color.RED);
                btn4.setBackgroundColor(btn4.getText().toString().equals(rep) ? Color.GREEN:Color.RED);
                Log.d(TAG, "discoverReponse step4");     
            }
        });
    }

For the moment I got all exepted output (step1-4) but the button didn't become neither red or green.



Solution 1:[1]

OK, so step by step. Having this this layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="aaaa" />

    <Button
        android:id="@+id/button_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="bbbb" />

    <Button
        android:id="@+id/button_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="cccc" />

    <Button
        android:id="@+id/button_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="dddd" />

</LinearLayout>

which look like there:

before click

You can create simple logic:

package training.com.myapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    // All of your buttons
    Button btn1;
    Button btn2;
    Button btn3;
    Button btn4;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // "Find" buttons in layout
        btn1 = findViewById(R.id.button_1);
        btn2 = findViewById(R.id.button_2);
        btn3 = findViewById(R.id.button_3);
        btn4 = findViewById(R.id.button_4);

        // your correct response
        String response = "aaaa";

        // Adding listeners to all of buttons
        btn1.setOnClickListener(v -> {
            discoverAnswer(response);
        });

        btn2.setOnClickListener(v -> {
            discoverAnswer(response);
        });

        btn3.setOnClickListener(v -> {
            discoverAnswer(response);
        });

        btn4.setOnClickListener(v -> {
            discoverAnswer(response);
        });
    }

    public void discoverAnswer(final String rep) {
        // Change color all of the buttons
        runOnUiThread(() -> {
            btn1.setBackgroundColor(btn1.getText().toString().equals(rep) ? Color.GREEN : Color.RED);
            btn2.setBackgroundColor(btn2.getText().toString().equals(rep) ? Color.GREEN : Color.RED);
            btn3.setBackgroundColor(btn3.getText().toString().equals(rep) ? Color.GREEN : Color.RED);
            btn4.setBackgroundColor(btn4.getText().toString().equals(rep) ? Color.GREEN : Color.RED);
        });
    }
}

Or using list of Buttons:

package training.com.myapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    // List of buttons
    List<Button> listOfButtons = new ArrayList<>();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listOfButtons.addAll(Arrays.asList(
                findViewById(R.id.button_1),
                findViewById(R.id.button_2),
                findViewById(R.id.button_3),
                findViewById(R.id.button_4)
        ));

        // your correct response
        String response = "aaaa";

        // Adding listeners to all of buttons - for API < 24
        for (Button button : listOfButtons) {
            button.setOnClickListener(v -> discoverAnswer(response));
        }
    }

    public void discoverAnswer(final String rep) {
        // Change color all of the buttons
        runOnUiThread(() -> {
            for (Button button : listOfButtons) {
                button.setBackgroundColor(button.getText().toString().equals(rep) ? Color.GREEN : Color.RED);
            }
        });
    }
}

And after click on of the button you can expect that:

  • correct one - will be green

  • rest of them - red

after click

Solution 2:[2]

I suggest using .setBackgroundTint() instead of .setBackground()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Binyamin Robbins