Skip to content
Advertisement

Android Button color changes to another color

I am creating a simple quiz app. And want to change the color of button to green if answer is correct, and to red if answer is incorrect.If i change color through xml of layout it is normally changed, but when I write

   if (option1.getText().equals(correctAnswer)){
option1.setBackgroundColor(R.color.green);
Toast.makeText(this, "Answer is correct", Toast.LENGTH_SHORT).show();
                 }

color changes but totally to another color (dark blue). Why colors are not changed as supposed?

themes.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.IslomniOrganamiz" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/white</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_700</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>

XML:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SavolJavob"
    android:gravity="center_vertical"
    android:background="@color/black">
<TextView
    android:id="@+id/whichQuestion"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="1 - savol"
    android:textColor="@color/white"
    android:textSize="40sp"
    android:layout_gravity="center_horizontal"
    />
    <TextView
        android:id="@+id/questionTextView"
        android:layout_width="match_parent"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="180dp"
        android:layout_gravity="center_vertical"
        android:text="Payg'ambarimiz Muhammad(s.a.v) qaysi shaharda tavallud topganlar?"
        android:textColor="@color/white"
        android:textSize="24sp" />
    <LinearLayout
        android:layout_marginTop="200dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <Button
            style="@style/Theme.IslomniOrganamiz"
            android:tag="0"
            android:layout_width="200dp"
            android:id="@+id/option1"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textColor="@color/black"
            android:text="Makkada"
            android:paddingRight="25dp"
            android:paddingLeft="25dp"/>
        <Button
            android:tag="1"
            android:id="@+id/option2"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textColor="@color/black"

            android:text="Madinada" />
        <Button
            android:tag="2"
            android:id="@+id/option3"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textColor="@color/black"
            android:text="Rimda"/>
        <Button
            android:tag="3"
            android:id="@+id/option4"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textColor="@color/black"
            android:text="To'gri javob yo'q" />
    </LinearLayout>
</LinearLayout>

Java:

   package com.example.islomniorganamiz;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Random;

public class SavolJavob extends AppCompatActivity implements View.OnClickListener {
    Random random=new Random();
    int questionNumber=1;
    TextView questionTextView,whichQuestion;
    Button option1,option2,option3,option4;
    Questions questions=new Questions();
    ArrayList<Integer> askedQuestions = new ArrayList<Integer>();

    int random_button1;
    int random_button2;
    int random_button3;
    int random_button4;
    int random_number;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_savol_javob);

        questionTextView=findViewById(R.id.questionTextView);
        whichQuestion=findViewById(R.id.whichQuestion);
        option1=findViewById(R.id.option1);
        option2=findViewById(R.id.option2);
        option3=findViewById(R.id.option3);
        option4=findViewById(R.id.option4);

        option1.setOnClickListener(this);
        option2.setOnClickListener(this);
        option3.setOnClickListener(this);
        option4.setOnClickListener(this);


nextQuestion();
    }

    
    @Override
    public void onClick(View v) {
        String correctAnswer=questions.correctAnswersArray[random_number];
        switch (v.getId()){
                case R.id.option1:
                    if (option1.getText().equals(correctAnswer)){
option1.setBackgroundColor(R.color.green);
                   Toast.makeText(this, "Answer is correct", Toast.LENGTH_SHORT).show();
                 }
                    else{
                     Toast.makeText(this, "Incorrect Button1.Correct answer was: "+questions.correctAnswersArray[random_number], Toast.LENGTH_SHORT).show();
                      }
                    nextQuestion();
                   break;

                case R.id.option2:
                if (option2.getText().equals(correctAnswer)){
                    Toast.makeText(this, "Answer is correct", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(this, "Incorrect Button2.Correct answer was: "+questions.correctAnswersArray[random_number], Toast.LENGTH_SHORT).show();
                }
                nextQuestion();
                break;

            case R.id.option3:
                if (option3.getText().equals(correctAnswer)){
                    Toast.makeText(this, "Answer is correct", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(this, "Incorrect Button3.Correct answer was: "+questions.correctAnswersArray[random_number], Toast.LENGTH_SHORT).show();
                }
                nextQuestion();
                break;

            case R.id.option4:
                if (option4.getText().equals(correctAnswer)){
                    Toast.makeText(this, "Answer is correct", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(this, "Incorrect Button4.Correct answer was: "+questions.correctAnswersArray[random_number], Toast.LENGTH_SHORT).show();
                }
                nextQuestion();
                break;

        }

    }



    @SuppressLint("ResourceAsColor")
    public void nextQuestion(){
       random_number=random.nextInt(questions.questionsArray.length);
        while ((askedQuestions.contains(random_number))){
           random_number=random.nextInt(questions.questionsArray.length);
       }
       askedQuestions.add(random_number);
        random_button1=random.nextInt(4);
        while (random_button1==random_button2) {
            random_button2 = random.nextInt(4);
        }
        while (random_button3==random_button1||random_button3==random_button2) {
            random_button3 = random.nextInt(4);
        }
        while (random_button4==random_button1||random_button4==random_button2||random_button4==random_button3) {
            random_button4 = random.nextInt(4);
       }
        whichQuestion.setText(questionNumber+" - SAVOL");
     questionTextView.setText(questions.questionsArray[random_number]);
     option1.setText(questions.optionsArray[random_number][random_button1]);
     option2.setText(questions.optionsArray[random_number][random_button2]);
     option3.setText(questions.optionsArray[random_number][random_button3]);
     option4.setText(questions.optionsArray[random_number][random_button4]);

questionNumber++;
    }


}

Advertisement

Answer

It happens because the method setBackgroundColor(@ColorInt int color) works with a @ColorInt, not a resource (@ColorRes)

Use:

button.setBackgroundColor(ContextCompat.getColor(this,R.color.xxxx));

or something like:

button.setBackgroundColor(Color.GREEN)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement