Storing data to Shared Preferences
1. Create a new Android Project called SharedPrefExample with package mdad.localdata and Activity name: SharedPrefExample.
1. Create a new Android Project called SharedPrefExample with package mdad.localdata and Activity name: SharedPrefExample.
2. Copy/replace activity_main.xml file with the code below:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tvPw" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Password" android:layout_marginLeft="100dp" /> <EditText android:id="@+id/etPw" android:inputType="textPassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="100dp" android:ems="10" /> <Button android:id="@+id/btnLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="100dp" android:text="Login" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
3. Create 2 more activities called setPassword and DetailsActivity
Use the codes below to create the Layout for activity_set_password.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".setPassword"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="0dp" android:layout_height="0dp" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="100dp" android:layout_marginTop="150dp" android:text="Your Name" /> <EditText android:id="@+id/etName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="100dp" android:ems="10" /> <TextView android:id="@+id/tvPwd1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="100dp" android:text="Password" /> <EditText android:id="@+id/etPwd1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="100dp" android:ems="10" /> <TextView android:id="@+id/tvPwd2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="100dp" android:text="Confirm Password" /> <EditText android:id="@+id/etPwd2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="100dp" android:ems="10" android:inputType="textPassword" /> <Button android:id="@+id/btnSet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="100dp" android:text="Set Password" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
Use the codes below to create the Layout for activity_details.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DetailsActivity">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tvWelcome"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/btnLogOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Log Out" />
<Button
android:id="@+id/btnClearPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Clear Password" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
3. Edit MainActivity.java
Declare class variables
EditText pwd;
Button loginBtn;
SharedPreferences prf;
String sPassword;
inside the onCreate method,
Do the binding
pwd = (EditText)findViewById(R.id.etPw);loginBtn = (Button)findViewById(R.id.btnLogin);
we read the data from sharedPreference loginDetails.
check if the pw is null, if it is null we launch the activity setPassword
prf = getSharedPreferences("loginDetails",MODE_PRIVATE);// open from sharedPref
//if pw (password) is null means it has not been set or
//it is the 1st time the user use the app and has not set the password yet
if (prf.getString("pw",null) ==null)
{
Toast.makeText(getApplicationContext(),"Empty",Toast.LENGTH_LONG).show();
Intent nextPage = new Intent(MainActivity.this, setPassword.class);
startActivity(nextPage);
finish();
}
else{
sPassword=prf.getString("pw",null); //if not null , we read and store the pw in sPassword
}
Create a Login Button listener
if the user enter the password (password) is equal to the password from SharedPref (sPassword)
then we launch the DetailsActivity
else
we just simply Toast a message (Credentials are not valid)
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String password = pwd.getText().toString(); //get text from user
if( password.equals(sPassword) ){
Toast.makeText(getApplicationContext(), "Login Successful",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,DetailsActivity.class);
startActivity(intent);
finish();
}
else
{
Toast.makeText(getApplicationContext(),"Credentials are not valid",Toast.LENGTH_SHORT).show();
}
}
});
Test it out
4. Edit setPassword.java
Declare class variables
SharedPreferences pref;
EditText pwd1, pwd2, etName;
Button btnSetPW;
inside the onCreate method,
Do the binding
etName = (EditText)findViewById(R.id.etName);
pwd1 = (EditText)findViewById(R.id.etPwd1);
pwd2 = (EditText)findViewById(R.id.etPwd2);
btnSetPW= (Button)findViewById(R.id.btnSet);
btnSetPW.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
String name = etName.getText().toString(); //getText from user
String pw1 = pwd1.getText().toString();//getText from user
String pw2 = pwd2.getText().toString();//getText from user
if (pw1.equals(pw2)) { //check if both pw1 and pw2 are the same
pref = getSharedPreferences("loginDetails", MODE_PRIVATE); //Open sharedPreference
SharedPreferences.Editor editor = pref.edit(); //set Editor mode
editor.putString("sname", name);
editor.putString("pw", pw1);
editor.commit();
Intent nextPage = new Intent(setPassword.this, MainActivity.class);//goto next Activity
startActivity(nextPage);
finish(); //finish the current activity
}
else
{
Toast.makeText(getApplicationContext(), "Your password & confirmation password do not match", Toast.LENGTH_LONG).show();
}
Test it out
5. Edit DetailsActivity.java
SharedPreferences prf;
inside the onCreate method,
Do the binding
TextView result = (TextView)findViewById(R.id.tvWelcome);
Button btnLogOut = (Button)findViewById(R.id.btnLogOut);
Button btnClearPw = (Button)findViewById(R.id.btnClearPassword);
prf = getSharedPreferences("loginDetails",MODE_PRIVATE);
result.setText("Hello, "+prf.getString("sname",null));
btnClearPw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = prf.edit(); //set edit mode
editor.clear();
editor.commit();
Intent intent = new Intent(DetailsActivity.this,MainActivity.class);
startActivity(intent);
}
});
Create a Button listener for btnLogOut button
btnLogOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish(); //finish the current activity
Intent intent = new Intent(DetailsActivity.this,MainActivity.class);
startActivity(intent);
}
});
Test it out
No comments:
Post a Comment
Note: only a member of this blog may post a comment.