Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Main_Activity============================================
- package com.example.user.sharedpreferences;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- EditText txtUserName;
- EditText txtPassword;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // Getting information form user name and password fields
- txtUserName=(EditText)findViewById(R.id.txtUserName);
- txtPassword=(EditText)findViewById(R.id.txtPassword);
- // If user exist go automatically to Logged
- if(CheckUser())
- {
- SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- Intent loggedIntent=new Intent(this,Logged.class);
- // Send user name to Logged screen
- loggedIntent.putExtra("userPut", pref.getString("Logged", "Error"));
- // Go to logged screen
- this.startActivity(loggedIntent);
- }
- }
- // Checking if user exist
- private boolean CheckUser()
- {
- SharedPreferences pref=getApplicationContext().getSharedPreferences("users", MODE_PRIVATE);
- String userName = pref.getString("Logged","Error");
- // If user not exist return false
- return (!userName.equals("Error"));
- }
- public void btnNext(View v)
- {
- SharedPreferences pref = getApplicationContext().getSharedPreferences("users", MODE_PRIVATE);
- String logUserName=txtUserName.getText().toString().toLowerCase();
- String logPassword=txtPassword.getText().toString();
- String logPasswordConf=pref.getString(logUserName, "Error");
- // If any field is empty
- if(logUserName.isEmpty() || logPassword.isEmpty())
- {
- Toast.makeText(this,"One of more fields is empty",Toast.LENGTH_SHORT).show();
- return;
- }
- // if user name or password doesn't exist in "users" file
- else if(!pref.contains(logUserName) || !logPassword.equals(logPasswordConf))
- {
- Toast.makeText(this,"Invalid user name or password ",Toast.LENGTH_SHORT).show();
- return;
- }
- // if proper data
- else
- {
- SharedPreferences.Editor editor=pref.edit();
- editor.putString("Logged",logUserName).commit();
- Toast.makeText(this,"You have successfuly logged",Toast.LENGTH_SHORT).show();
- Intent loggedIntent = new Intent(this, Logged.class);
- // Send user name to Logged screen
- loggedIntent.putExtra("userPut", pref.getString("Logged", "Error"));
- // Go to logged screen
- this.startActivity(loggedIntent);
- }
- }
- // Go to registration screen
- public void btnRegister(View v)
- {
- Intent registerIntent=new Intent(this,Register.class);
- // Go to registration screen
- this.startActivity(registerIntent);
- }
- }
- Register=======================================================
- package com.example.user.sharedpreferences;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- import android.content.SharedPreferences;
- public class Register extends Activity {
- EditText txtUserName,txtPassword1,txtPassword2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.register);
- // Pointers
- txtUserName=(EditText)findViewById(R.id.regUserName);
- txtPassword1=(EditText)findViewById(R.id.regPassword1);
- txtPassword2=(EditText)findViewById(R.id.regPassword2);
- }
- public void btnCancel(View v)
- {
- Intent cancelIntent=new Intent(this,MainActivity.class);
- // Go to login screen
- this.startActivity(cancelIntent);
- }
- // Registration
- public void btnRegister(View v)
- {
- SharedPreferences pref=getApplicationContext().getSharedPreferences("users", MODE_PRIVATE);
- SharedPreferences.Editor editor=pref.edit();
- String regUserName=txtUserName.getText().toString().toLowerCase();
- String regPassword1=txtPassword1.getText().toString();
- String regPassword2=txtPassword2.getText().toString();
- // If any field is empty
- if(regUserName.isEmpty() || regPassword1.isEmpty() || regPassword2.isEmpty())
- {
- Toast.makeText(this,"One of more fields is empty",Toast.LENGTH_SHORT).show();
- return;
- }
- // if user name exist
- else if(pref.contains(regUserName))
- {
- Toast.makeText(this,"User name is exists",Toast.LENGTH_SHORT).show();
- return;
- }
- // if passwords match
- else if(!regPassword1.equals(regPassword2))
- {
- Toast.makeText(this,"Passwords don't match",Toast.LENGTH_SHORT).show();
- return;
- }
- // if proper data
- Toast.makeText(this,"Registration was successful",Toast.LENGTH_SHORT).show();
- // Adding a new user
- editor.putString(regUserName, regPassword1).commit();
- Intent registeredIntent = new Intent(this, MainActivity.class);
- // Go to login screen
- this.startActivity(registeredIntent);
- }
- }
- Logged=======================================================
- package com.example.user.sharedpreferences;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.TextView;
- public class Logged extends Activity{
- TextView txtUserName;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.logged);
- // Pointer to text view
- txtUserName=(TextView)findViewById(R.id.textView);
- // Print "Hello "+ user name from main activity
- txtUserName.setText("Hello "+getIntent().getStringExtra("userPut"));
- }
- // Go back to main activity and remove the user name and password
- public void btnLogout(View v)
- {
- SharedPreferences pref=getApplicationContext().getSharedPreferences("users",MODE_PRIVATE);
- SharedPreferences.Editor editor=pref.edit();
- // Remove last logged user
- editor.remove("Logged").commit();
- Intent logoutIntent=new Intent(this,MainActivity.class);
- // Go to login screen
- this.startActivity(logoutIntent);
- }
- }
- main_Activity xml =================================================
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:background="@color/white">
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:src="@drawable/user_login"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="User Login"
- android:textSize="25sp"
- android:gravity="center" />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="User name"
- android:textSize="20sp"
- android:id="@+id/txtUserName"
- android:inputType="text" />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Password"
- android:textSize="20sp"
- android:id="@+id/txtPassword"
- android:inputType="textPassword" />
- <Button
- android:layout_width="150sp"
- android:layout_height="wrap_content"
- android:text="Login"
- android:textSize="20sp"
- android:onClick="btnNext"
- android:layout_gravity="center" />
- <Button
- android:layout_width="150sp"
- android:layout_height="wrap_content"
- android:text="Register"
- android:textSize="20sp"
- android:onClick="btnRegister"
- android:layout_gravity="center"/>
- </LinearLayout>
- </LinearLayout>
- register xml =====================================================
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:background="@color/white">
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:src="@drawable/new_user"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Create a new user"
- android:textSize="25sp"
- android:gravity="center"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="User name"
- android:textSize="20sp"
- android:id="@+id/regUserName"
- android:inputType="text"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Password"
- android:textSize="20sp"
- android:id="@+id/regPassword1"
- android:inputType="textPassword"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Confirm password"
- android:textSize="20sp"
- android:id="@+id/regPassword2"
- android:inputType="textPassword"/>
- <Button
- android:layout_width="150sp"
- android:layout_height="wrap_content"
- android:text="Register"
- android:textSize="20sp"
- android:onClick="btnRegister"
- android:layout_gravity="center"/>
- <Button
- android:layout_width="150sp"
- android:layout_height="wrap_content"
- android:text="Cancel"
- android:textSize="20sp"
- android:onClick="btnCancel"
- android:layout_gravity="center"/>
- </LinearLayout>
- </LinearLayout>
- logged xml=====================================================
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:background="@color/white">
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:src="@drawable/logged2"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="25sp"
- android:gravity="center"
- android:id="@+id/textView"/>
- <Button
- android:layout_width="150sp"
- android:layout_height="wrap_content"
- android:text="Logout"
- android:textSize="20sp"
- android:onClick="btnLogout"
- android:layout_gravity="center"/>
- </LinearLayout>
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement