skip to Main Content

I’m making a simple login with a checkbox. My sharedpreferences xml file does not contain the boolean statement at the very first login so the checkbox goes through as unchecked. For all users’ first login, the checkbox value does not correctly reflect. I have to interact with the checkbox (click to check/uncheck) for it to work but after that, the succeeding tries work fine. What have I missed?

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText lUsername = (EditText) findViewById(R.id.lUsername);
    EditText lPassword = (EditText) findViewById(R.id.lPassword);
    Button btnSignin = (Button) findViewById(R.id.btnSignin);
    CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);

    btnSignin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick (View v) {

            String username = lUsername.getText().toString();
            String password = lPassword.getText().toString();

            SharedPreferences preferences = getSharedPreferences("MYPREFS", MODE_PRIVATE);                

            String details = preferences.getString(username + password + "data", "invalid");
            SharedPreferences.Editor editor = preferences.edit();

            if (details == "invalid") {
               Toast.makeText(MainActivity.this, "Invalid credentials", Toast.LENGTH_SHORT).show();
            } else {
                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if(checkBox.isChecked()) {
                            editor.putBoolean("checked", true);
                        }else{
                            editor.putBoolean("checked", false);
                        }
                        editor.commit();
                    }
                });
                Intent displayScreen = new Intent(MainActivity.this, activity2.class);
                startActivity(displayScreen);
            }
   }
});

DisplayScreen.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.DisplayScreen);

    SharedPreferences preferences = getSharedPreferences("MYPREFS", MODE_PRIVATE);

    String username = preferences.getString("username", "invalid");

    if(preferences.contains("checked") && preferences.getBoolean("checked",false) == true) {
        String data = preferences.getString("data", "Hello senior " + username + "!");
        TextView displayInfo = (TextView) findViewById(R.id.helloDisplay);
        displayInfo.setText(data);
    }else {
        String data = preferences.getString("data", "Hello " + username + "!");
        TextView displayInfo = (TextView) findViewById(R.id.helloDisplay);
        displayInfo.setText(data);
    }
}

2

Answers


  1. It is because you have used checkBox.setOnCheckedChangeListener() this will only be triggered when you interact with the checkBox. If you want to save some default value in sharedPreferences just store it before you do anything, also declare sharedPreferences and other things outside your button clickListener not inside.

    Button btnSignin = (Button) findViewById(R.id.btnSignin);
    CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
    
    SharedPreferences preferences = getSharedPreferences("MYPREFS", MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();   
    editor.putBoolean("checked", false);    //for default case
    
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if(checkBox.isChecked()) {
                            editor.putBoolean("checked", true);
                        }else{
                            editor.putBoolean("checked", false);
                        }
                        editor.commit();
                    }
                });
    
    btnSignin.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick (View v) {
                String username = lUsername.getText().toString();
                String password = lPassword.getText().toString();
                String details = preferences.getString(username + password + "data", "invalid");
                if (details == "invalid") {
                   Toast.makeText(MainActivity.this, "Invalid credentials", Toast.LENGTH_SHORT).show();
                } else {
                    Intent displayScreen = new Intent(MainActivity.this, activity2.class);
                startActivity(displayScreen);
                }
            }
        });
    
    Login or Signup to reply.
  2. You are calling checkBox.setOnCheckedChangeListener inside of btnSignin.setOnClickListener which you are calling startActivity(displayScreen). So you never get the checkbox changes for the first time. But if you navigate back to the MainActivity you will get the checkbox changes because checkBox.setOnChecked ChangeListener is already set.

    Here is an approach you can use. You can always call checkBox.isChecked() method to check if the checkbox is checked or not; which in your case is better to call this method instead of setting checkBox.setOnCheckedChangeListener. So your code will become like this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        ...
    
        btnSignin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View v) {
    
                ...
    
                if (details == "invalid") {
                   ...
                } else {
                    if(checkBox.isChecked()) {
                        editor.putBoolean("checked", true);
                    } else {
                        editor.putBoolean("checked", false);
                    }
                    editor.commit();
                   ...
                }
            }
        });
    }
    

    For the sake of simplicity you can even do it like this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        ...
    
        btnSignin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View v) {
    
                ...
    
                if (details == "invalid") {
                   ...
                } else {
                    editor.putBoolean("checked", checkbox.isChecked());
                    editor.commit();
                   ...
                }
            }
        });
    }
    

    Which makes your code more readable.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search