1

I'm building an app using Java in Android Studio. I'm at the stage of writing an if-statement:

"If the background color of the button is white, turn the background color of the button to black."

Does anyone know the code to check if the background color button is white?

"if(button == white){}"

I only know the part on how to turn the background color button to black.

btnChangeColor.setBackgroundColor(getResources().getColor(R.color.black)

Thanks

M123
  • 1,203
  • 4
  • 14
  • 31
  • Does this answer your question? [Get the background color of a button in android](https://stackoverflow.com/questions/8089054/get-the-background-color-of-a-button-in-android) – M123 May 27 '22 at 04:58
  • Thanks. I think I experienced a crash after i run it. public void btnOnClick (View view) { Button btnChangeColor = findViewById(R.id.btnChangeColor); ColorDrawable btnColor = (ColorDrawable) btnChangeColor.getBackground(); int colorId = btnColor.getColor(); if (colorId == R.color.black) { btnChangeColor.setBackgroundColor(getResources().getColor(R.color.white)); } if (colorId == R.color.white) { btnChangeColor.setBackgroundColor(getResources().getColor(R.color.black)); } } – user19211453 May 27 '22 at 06:16

1 Answers1

0

A button view doesn't hold background color as a solid value. when you call setBackgroundColor, the given color value applies over the background drawable object(A Drawable is a graphic that can be drawn to the screen) of the button. So you have to read the color value from this drawable.

    final int black = 0xFF000000;
    final int white = 0xFFffffff;

    button.setBackgroundColor(black);
    button.setTextColor(white);
    button.setText("Black");

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (((ColorDrawable) v.getBackground()).getColor() ==black) {
                button.setBackgroundColor(white);
                button.setTextColor(black);
                button.setText("White");
            } else {
                button.setBackgroundColor(black);
                button.setTextColor(white);
                button.setText("Black");
            }
        }
    });
ucMedia
  • 4,105
  • 4
  • 38
  • 46
  • Thanks @ucMedia, I tried your method but I got a crash again when I run the app and click the button. I’m starting to think whether if it’s a bug from android studio instead of a syntax error. Because the compiler usually provides an error message instead of crashing the app. – user19211453 May 29 '22 at 05:28
  • Please attach error log – ucMedia May 29 '22 at 12:52
  • https://drive.google.com/file/d/1yWFL5OwKlY9dAhhog4DDl_MCMscJxz0x/view?usp=sharing – user19211453 May 31 '22 at 03:14
  • This is not related to your code, the easiest way to fix it, uninstall/install both android studio and SDK. – ucMedia May 31 '22 at 08:59
  • I put the working example here https://www.ucmedia.ir/samples/android/change-view-background.html , make a new project , just overwrite the code in your mainActivity. And don't forget to give me +1 – ucMedia May 31 '22 at 09:43
  • I press +1, says I need more reputation – user19211453 Jun 02 '22 at 11:23