Hola tengo un login y un checkbox y quiero usarlo para que me recuerde el usuario y la contraseña como podria hacerlo?
Aqui dejo el login activity:
public class LoginActivity extends AppCompatActivity {
EditText email, password;
private Button sign_in_register;
private RequestQueue requestQueue;
private static final String URL = "";
private StringRequest request;
public CheckBox ch;
public static final String MY_PREFS_NAME = "user_pass_pref";
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
email = (EditText) findViewById(R.id.editText2);
password = (EditText) findViewById(R.id.editText);
sign_in_register = (Button) findViewById(R.id.button);
ch = (CheckBox)findViewById(R.id.checkBox);
if(ch.isChecked())
{
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("username", email.getText().toString());
editor.putString("password1", password.getText().toString());
editor.apply();
}
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("username", null);
if (restoredText != null) {
String name = prefs.getString("username", "");
String password1 = prefs.getString("password1", "");
email.setText(name);
password.setText(password1);
}
requestQueue = Volley.newRequestQueue(this);
sign_in_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.names().get(0).equals("success")) {
Toast.makeText(getApplicationContext(), "SUCCESS " + jsonObject.getString("success"), Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
} else {
Toast.makeText(getApplicationContext(), "Error" + jsonObject.getString("error"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("email", email.getText().toString());
hashMap.put("password", password.getText().toString());
return hashMap;
}
};
requestQueue.add(request);
}
});
}
}