2

just started working in Android Studio, for most of the things I can find just fine everything with "Google it" but apparently for this thing I don't. I have a basic switch button that I want to change the app color background from white to black and back unchecking the button. The interrogation is really simple and works just fine changing the text of the switch from "dark mode" to "White mode". The interrogation is done in MainActivity.java.

final Switch BackgroundColorButton = (Switch) findViewById(R.id.BackgroundColorButton);
            BackgroundColorButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(BackgroundColorButton.isChecked())
                    {
                        BackgroundColorButton.setText("White Mode");
                        //here I need to change the app background color to Black or DarkGray
                    }
                    else
                    {
                        BackgroundColorButton.setText("Dark Mode");
                         //here I need to change the app background color to White
                    }
                }
            });
Akatori
  • 37
  • 6

1 Answers1

1

Ok, did find on an Indian video. the short solution, add activity main XML the line of code with //JUST THIS

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rView" // JUST THIS
    tools:context=".MainActivity">

and in the mainactivity.java

final Switch BackgroundColorButton = (Switch) findViewById(R.id.BackgroundColorButton);
            BackgroundColorButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(BackgroundColorButton.isChecked())
                    {
                        BackgroundColorButton.setText("White Mode");
                        screenView.setBackgroundColor(Color.BLACK);//JUST THIS

                    }
                    else
                    {
                        BackgroundColorButton.setText("Dark Mode");
                        screenView.setBackgroundColor(Color.WHITE);//JUST THIS
                    }
                }
            });
Akatori
  • 37
  • 6