Shareprefrences Tutorial









< ?xml version="1.0" encoding="utf-8"?>
< RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
< TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/hometext"
android:layout_centerInParent="true"
android:padding="20dp"
android:inputType="textCapCharacters"
android:gravity="center"
android:textStyle="bold"/>
< Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Logout"
android:onClick="logout"
android:padding="20dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
< /RelativeLayout>





< ?xml version="1.0" encoding="utf-8"?>
< RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back"
xmlns:android="http://schemas.android.com/apk/res/android">
< TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login Details"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:textSize="20sp"
android:layout_above="@id/ll"
android:gravity="center"/>
< LinearLayout 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:orientation="vertical"
android:id="@+id/ll"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_centerInParent="true">

< EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:padding="20dp"
android:id="@+id/luname"/>
< EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:padding="20dp"
android:inputType="textPassword"
android:id="@+id/lpass"/>
< Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:onClick="login"
android:padding="20dp"
android:layout_gravity="center"/>
< TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click for new User."
android:onClick="goToRegister"
android:padding="20dp"
android:gravity="right"/>
< /LinearLayout>
< /RelativeLayout>




< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="@drawable/back"
android:orientation="vertical">

< EditText
android:id="@+id/runame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:padding="20dp" />
< EditText
android:id="@+id/rpass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:padding="20dp" />
< EditText
android:id="@+id/rfullname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Full Name"
android:inputType="textCapWords"
android:padding="20dp" />
< Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="20dp"
android:onClick="submit"
android:text="Submit" />
< TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="20dp"
android:onClick="goToLogin"
android:text="Click for Login." />
< /LinearLayout>




< ?xml version="1.0" encoding="utf-8"?>
< FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back"
xmlns:android="http://schemas.android.com/apk/res/android">

< TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="Welcome To Android."
android:layout_gravity="center_vertical"
android:gravity="center"
android:textSize="20sp"
android:typeface="serif"
android:textStyle="bold"/>
< /FrameLayout>


< ?xml version="1.0" encoding="utf-8"?>
< resources>
< color name="colorPrimary">#3F51B5

< color name="colorPrimaryDark">#303F9F
< color name="colorAccent">#FF4081
< color name="a">#63B062
< color name="b">#3DB73C
#ffffff



< resources>
< string name="app_name">ShareDataExample
< /resources>


< resources>
< !-- Base application theme. -->
< style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
< !-- Customize your theme here. -->
< item name="colorPrimary">@color/colorPrimary
< item name="colorPrimaryDark">@color/colorPrimaryDark
< item name="colorAccent">@color/colorAccent
< /style>



< ?xml version="1.0" encoding="utf-8"?>
< shape xmlns:android="http://schemas.android.com/apk/res/android">
< gradient android:startColor="@color/a"
android:endColor="@color/b"
android:centerColor="@color/white"
android:angle="45"/>
< /shape>

________________________________________________________________


package example.com.sharedataexample;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class HomePage extends AppCompatActivity {
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
sp = getSharedPreferences("deep", MODE_PRIVATE);
String name = sp.getString("name", null);
TextView tv = findViewById(R.id.hometext);
tv.setText(name);
}
public void logout(View v) {
SharedPreferences.Editor e = sp.edit();
e.remove("uname");
e.remove("pass");
e.remove("name");
e.commit();
Intent in = new Intent(this, Login.class);
startActivity(in);
finish();
}
}

         ___________________________________________________________

package example.com.sharedataexample;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends AppCompatActivity {
EditText t1, t2;
String uname, pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
t1 = findViewById(R.id.luname);
t2 = findViewById(R.id.lpass);
}

public void login(View v) {
if (valid()) {
//do the login code here
MyDataBase md = new MyDataBase(this);
User u = md.login(uname, pass);
if (u != null) {
SharedPreferences sp = getSharedPreferences("deep", MODE_PRIVATE);
SharedPreferences.Editor e = sp.edit();
e.putString("name", u.getName());
e.putString("uname", u.getUname());
e.putString("pass", u.getPass());
e.commit();
Intent in = new Intent(this, HomePage.class);
startActivity(in);
finish();
} else {
Toast.makeText(this, "Login Failed", Toast.LENGTH_SHORT).show();
}
}
}
private boolean valid() {
uname = t1.getText().toString();
pass = t2.getText().toString();
if (TextUtils.isEmpty(uname)) {
Toast.makeText(this, "please enter uname", Toast.LENGTH_SHORT).show();
t1.requestFocus();
return false;
} else if (TextUtils.isEmpty(pass)) {
Toast.makeText(this, "please enter pass", Toast.LENGTH_SHORT).show();
t2.requestFocus();
return false;
} else {
return true;
}
}
public void goToRegister(View v) {
Intent in = new Intent(this, Register.class);
startActivity(in);
finish();
}
}


______________________________________________________________

package example.com.sharedataexample;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDataBase extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "mytask";
public static final String TABLE_NAME = "user";
public static final String UNAME = "uname";
public static final String PASS = "pass";
public static final String NAME = "name";
public static final int VERSION = 1;
public static final String CREATE_TABLE = "create table " + TABLE_NAME + " (" + NAME + " varchar(20)," + UNAME + " varchar(20) primary key," + PASS + " varchar(15));";
public MyDataBase(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("drop table if exists " + TABLE_NAME + "");
onCreate(sqLiteDatabase);
}
public long insert(User u) {
ContentValues cv = new ContentValues();
cv.put(NAME, u.getName());
cv.put(UNAME, u.getUname());
cv.put(PASS, u.getPass());
return getWritableDatabase().insert(TABLE_NAME, null, cv);
}
public User login(String uname, String pass) {
User u = null;
Cursor c = getReadableDatabase().query(TABLE_NAME, null, "" + UNAME + "=? and " + PASS + "=?", new String[]{uname, pass}, null, null, null, null);
if (c.moveToNext()) {
u = new User();
u.setName(c.getString(0));
u.setUname(c.getString(1));
u.setPass(c.getString(2));
}
return u;
}
}

         _________________________________________________________________
package example.com.sharedataexample;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class Register extends AppCompatActivity {
EditText t1, t2, t3;
String uname, pass, name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = findViewById(R.id.runame);
t2 = findViewById(R.id.rpass);
t3 = findViewById(R.id.rfullname);
}

public void submit(View v) {
if (valid()) {
//do the register code here
MyDataBase md = new MyDataBase(this);
User user = new User();
user.setName(name);
user.setPass(pass);
user.setUname(uname);
long t = md.insert(user);
if (t > 0) {
Toast.makeText(this, "Successfully Registered.", Toast.LENGTH_SHORT).show();
Intent in = new Intent(this, Login.class);
startActivity(in);
finish();
} else {
Toast.makeText(this, "Registration Failed..", Toast.LENGTH_SHORT).show();
}
}
}
private boolean valid() {
uname = t1.getText().toString();
pass = t2.getText().toString();
name = t3.getText().toString();
if (TextUtils.isEmpty(uname)) {
Toast.makeText(this, "please enter uname", Toast.LENGTH_SHORT).show();
t1.requestFocus();
return false;
} else if (TextUtils.isEmpty(pass)) {
Toast.makeText(this, "please enter pass", Toast.LENGTH_SHORT).show();
t2.requestFocus();
return false;
} else if (TextUtils.isEmpty(name)) {
Toast.makeText(this, "please enter name", Toast.LENGTH_SHORT).show();
t3.requestFocus();
return false;
} else {
return true;
}
}
public void goToLogin(View v) {
Intent in = new Intent(this, Login.class);
startActivity(in);
finish();
}
}


_________________________________________________________________

package example.com.sharedataexample;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class Splash extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);
SharedPreferences sp = getSharedPreferences("deep", MODE_PRIVATE);
String uname = sp.getString("uname", null);
String pass = sp.getString("pass", null);
if (uname != null && pass != null) {
//go to home page
Intent in = new Intent(this, HomePage.class);
startActivity(in);
finish();
} else {
//go to login page
Intent in = new Intent(this, Login.class);
startActivity(in);
finish();
}
}
}


_____________________________________________________________

package example.com.sharedataexample;
public class User {
private String name;
private String uname;
private String pass;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}

       For Video Tutorial Click Here.

_______________________________________________________________




No comments: