am I using boolean wrong?

Viewed 151

I'm trying to get a button to load a class and set a boolean then when the button is clicked again it loads another class..etc. I don't think it's setting MODE to false after it's done, is something wrong with my code?

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            boolean darkMode = false;
            public void onClick(View v) {
                if(!darkMode){
                    injectCSSdark();
                    darkMode = true;
                } else {
                    injectCSS();
                    darkMode = false;
                }

            }
        });

Debug code:

    Button button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        boolean darkMode = false;
        public void onClick(View v) {
                Log.d("mode", "onClick: 1 " + darkMode);
            if(!darkMode){
                injectCSSdark();
                darkMode = true;
                Log.d("mode", "onClick: 2 " + darkMode);
            } else {
                injectCSS();
                darkMode = false;
                Log.d("mode", "onClick: 3 " + darkMode);
            }

        }
    });
private void injectCSS() {
        try {
            Log.d("mode", "injectCSS!");
            InputStream inputStream = getAssets().open("style.css");
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            inputStream.close();
            String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
            wv.loadUrl("javascript:(function() {" +
                    "var parent = document.getElementsByTagName('head').item(0);" +
                    "var style = document.createElement('style');" +
                    "style.type = 'text/css';" +
                    // Tell the browser to BASE64-decode the string into your script !!!
                    "style.innerHTML = window.atob('" + encoded + "');" +
                    "parent.appendChild(style)" +
                    "})()");
            Log.d("mode", "injectCSS! end");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

injectCSSDark is the same just different name.

1 Answers

You are suppose to put the boolean outside from the method. The initialization of the variable "darkMode" conditions the algorithm in such a way that it is always true, removes the initialization of this variable to a higher scope such that it is outside the main algorithm.

public class MainActivity extends AppCompatActivity {
    private boolean darkMode = false; //put here

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (!darkMode) {
                injectCSSdark();
                darkMode = true;
            } else {
                injectCSS();
                darkMode = false;
            }

        }
    });
    }
}
Related