Created
May 21, 2020 18:07
-
-
Save KhadijaHameed/242321e548d394541ff60fcb738fb109 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.takessharperfence; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.os.Bundle; | |
import android.text.TextUtils; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.CheckBox; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
public class MainActivity extends AppCompatActivity { | |
private static final String KEY_EMAIL = "Email"; | |
private static final String KEY_PASSWORD = "Password"; | |
private static final String MyPREFERENCES = "MyPREFERENCES"; | |
TextView etEmail, etPassword; | |
Button btnLogin; | |
CheckBox cbRememberMe; | |
SharedPreferences sharedpreferences; | |
SharedPreferences.Editor editor; | |
@Override | |
protected void onCreate(final Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); | |
editor = sharedpreferences.edit(); | |
etEmail = findViewById(R.id.et_email); | |
etPassword = findViewById(R.id.et_password); | |
btnLogin = findViewById(R.id.btn_login); | |
cbRememberMe = findViewById(R.id.cb_remember_me); | |
getCredentials(); | |
btnLogin.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
String email = etEmail.getText().toString(); | |
String password = etPassword.getText().toString(); | |
if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) { | |
Toast.makeText(MainActivity.this, "Please enter email address and password!" , Toast.LENGTH_LONG).show(); | |
} else { | |
Toast.makeText(MainActivity.this, "You are successfully login!", Toast.LENGTH_LONG).show(); | |
saveDataIfNeeded(email, password); | |
} | |
} | |
}); | |
} | |
private void getCredentials() { | |
String email = sharedpreferences.getString(KEY_EMAIL, "") ; | |
String password = sharedpreferences.getString(KEY_PASSWORD, ""); | |
etEmail.setText(email); | |
etPassword.setText(password); | |
if (!email.isEmpty() || !password.isEmpty()){ | |
cbRememberMe.setChecked(true); | |
} | |
} | |
public void saveDataIfNeeded(String email, String password) { | |
if (cbRememberMe.isChecked()) { | |
editor.putString(KEY_EMAIL, email); | |
editor.putString(KEY_PASSWORD, password); | |
editor.commit(); | |
} | |
if (!cbRememberMe.isChecked()){ | |
editor.clear(); | |
editor.apply(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment