mardi 11 juillet 2017

Android : How to make remember me checkbox

I am having trouble with making 'Remember Me' checkbox. The code i created only saves password, not saving username. I am saving user informations on database. 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);
        }
    });

}

}




Aucun commentaire:

Enregistrer un commentaire