0

I am having trouble with making 'Remember Me' checkbox. The code i created only saves password, not saving username. I am saving user information on database. Please help me where am doing mistake.Here is my code;

public class LoginActivity extends AppCompatActivity {

    private SharedPreferences loginPreferences;
    private SharedPreferences.Editor loginPrefsEditor;
    private Boolean saveLogin;
    private String username;
    private String password;



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

        final EditText userText = (EditText) findViewById(R.id.userText);
        final EditText passText = (EditText) findViewById(R.id.passText);

        final Button engButton = (Button) findViewById(R.id.engButton);
        final Button loginButton = (Button) findViewById(R.id.loginButton);
        final Button regButton = (Button) findViewById(R.id.regButton);
        final Button exitButton = (Button) findViewById(R.id.exitButton);

        final TextView loginText = (TextView) findViewById(R.id.loginText);
        final TextView yaziText = (TextView) findViewById(R.id.yaziText);

        final CheckBox beniBox = (CheckBox) findViewById(R.id.beniBox);

        loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
        loginPrefsEditor = loginPreferences.edit();

        saveLogin = loginPreferences.getBoolean("saveLogin", false);
        if (saveLogin == true) {
            userText.setText(loginPreferences.getString("username", ""));
            passText.setText(loginPreferences.getString("password", ""));
            beniBox.setChecked(true);
        }





        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String user_name = userText.getText().toString();
                final String password = passText.getText().toString();

                if(view==loginButton){
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(userText.getWindowToken(), 0);

                    if (beniBox.isChecked()) {
                        loginPrefsEditor.putBoolean("saveLogin", true);
                        loginPrefsEditor.putString("username", username);
                        loginPrefsEditor.putString("password", password);
                        loginPrefsEditor.commit();
                    } else {
                        loginPrefsEditor.clear();
                        loginPrefsEditor.commit();
                    }



                }

                Response.Listener<String> responseListener = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");

                            if(success){

                                String user_name = jsonResponse.getString("user_name");

                                Intent intent = new Intent(LoginActivity.this , ProfileActivity.class);
                                intent.putExtra("user_name", user_name);

                                LoginActivity.this.startActivity(intent);

                            }else{
                                AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                                builder.setMessage("Login Failed")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                };

                LoginRequest loginRequest = new LoginRequest(user_name, password, responseListener);
                RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
                queue.add(loginRequest);
            }
        });

        regButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent regIntent = new Intent(LoginActivity.this , RegisterActivity.class);
                LoginActivity.this.startActivity(regIntent);
            }
        });

    }
}
Sunisha Guptan
  • 1,555
  • 17
  • 44
Alp Tahta
  • 65
  • 7
  • Question is similar but here the problem is not same.Problem is belonging to logic. This should not have been closed – Abdul Waheed Jul 11 '17 at 11:31
  • I can't post an answer so I will write here your problem. change the line loginPrefsEditor.putString("username", username); with loginPrefsEditor.putString("username", user_name); the variable username is always empty, insinde onclick method you are setting the username and password to fields usern_name and password – gmetax Jul 11 '17 at 11:46
  • @gmetax Thank you so much.That solved my problem. – Alp Tahta Jul 11 '17 at 12:10
  • You can delete that post now because it has been wrongly marked as duplicate – gmetax Jul 11 '17 at 13:45

1 Answers1

-1

Please refer this one: Add a "Remember me" checkbox , This is good enough to understand the basics.

Rohith Krishnan
  • 648
  • 8
  • 29