Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.ayob.loginsystem;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.view.View;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- EditText userTxt;
- EditText passTxt ;
- SharedPreferences Prefs;
- SharedPreferences.Editor editor;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointers();
- Toast.makeText(this,Prefs.getString("logged", "Error"),Toast.LENGTH_SHORT).show();
- if(CheckUser())
- {
- Intent intent=new Intent(this,Display_Activity.class);
- // Send user name to Logged screen
- intent.putExtra("userinput", Prefs.getString("logged", "Error"));
- // Go to Disply logged screen
- this.startActivity(intent);
- }
- }
- // Checking if user exist in the SharedPreferences XML file
- private boolean CheckUser()
- {
- String userName = Prefs.getString("logged","Error");
- // If user not exist return false
- return (!userName.equals("Error"));
- }
- //pointer initioialazation
- private void setPointers()
- {
- Prefs = getApplicationContext().getSharedPreferences("users",MODE_PRIVATE);
- editor = Prefs.edit();
- userTxt = (EditText)findViewById(R.id.txt_userName);
- passTxt = (EditText)findViewById(R.id.txt_Password);
- }
- // show Registeration Activity
- public void btn_show_register(View v)
- {
- Intent reg=new Intent(this,RegisterActivity.class);
- startActivity(reg);
- }
- // Login To the System
- public void btnLogin(View v)
- {
- SharedPreferences pref = getApplicationContext().getSharedPreferences("users", MODE_PRIVATE);
- String logUserName=userTxt.getText().toString().toLowerCase();
- String logPassword=passTxt.getText().toString();
- String loggeedUser=pref.getString(logUserName, "Error");
- // validate user input not 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" sharedpreferences xml file
- else if(!pref.contains(logUserName) || !logPassword.equals(loggeedUser))
- {
- 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, Display_Activity.class);
- // Send user name to Logged screen
- loggedIntent.putExtra("userPut", pref.getString("Logged", "Error"));
- // Go to logged screen
- this.startActivity(loggedIntent);
- }
- }
- _________________________________________________________________________________--
- public class RegisterActivity extends Activity
- {
- EditText UserName;//(EditText)findViewById(R.id.et_user);
- EditText Password1;//=(EditText)findViewById(R.id.et_password);
- EditText Password2;//=(EditText)findViewById(R.id.et_confirm_pass);
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.register_activity);
- UserName=(EditText)findViewById(R.id.et_user);
- Password1=(EditText)findViewById(R.id.et_password);
- Password2=(EditText)findViewById(R.id.et_confirm_pass);
- }
- public void btnCancel(View v)
- {
- Intent MainIntent=new Intent(this,MainActivity.class);
- // Go to login screen
- this.startActivity(MainIntent);
- }
- public void btnUserRegister(View v)
- {
- SharedPreferences pref=getApplicationContext().getSharedPreferences("users", MODE_PRIVATE);
- SharedPreferences.Editor editor=pref.edit();
- String regUserName=UserName.getText().toString().toLowerCase();
- String regPassword1=Password1.getText().toString();
- String regPassword2=Password2.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);
- }
- ____________________________________________________________________________________
- package com.example.ayob.loginsystem;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.View;
- import android.widget.TextView;
- /**
- * Created by Ayob on 11/03/2016.
- */
- public class Display_Activity extends Activity
- {
- TextView txtUserName;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.user_display);
- txtUserName=(TextView)findViewById(R.id.txt_hello);
- // Print "Hello "+ user name from main activity
- txtUserName.setText("Hello " + getIntent().getStringExtra("userPut"));
- }
- 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 MainIntent=new Intent(this,MainActivity.class);
- // Go to login screen
- this.startActivity(MainIntent);
- }
- }
- _____________________________________________________________________________________________________________
- <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="horizontal"
- android:layout_weight="1">
- <EditText
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:hint="User"
- android:textSize="30sp"
- android:layout_weight="1"
- android:gravity="center"
- android:id="@+id/txt_userName"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:layout_weight="1"
- android:layout_gravity="center">
- <EditText
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:hint="Password"
- android:textSize="30sp"
- android:layout_weight="1"
- android:gravity="center"
- android:id="@+id/txt_Password"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:layout_weight="1"
- android:layout_gravity="center">
- <Button android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:text="Login"
- android:textSize="30dp"
- android:layout_weight="1"
- android:onClick="btnLogin"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:layout_weight="1">
- <Button android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:text="Register"
- android:textSize="30dp"
- android:layout_weight="1"
- android:onClick="btn_show_register"/>
- </LinearLayout>
- </LinearLayout>
- _______________________________________________________________________________________________________________
- <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="horizontal"
- android:layout_weight="1">
- <EditText
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:hint="User"
- android:textSize="30sp"
- android:layout_weight="1"
- android:gravity="center"
- android:id="@+id/et_user"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:layout_weight="1"
- android:layout_gravity="center">
- <EditText
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:hint="Password"
- android:textSize="30sp"
- android:layout_weight="1"
- android:gravity="center"
- android:id="@+id/et_password"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:layout_weight="1"
- android:layout_gravity="center">
- <EditText
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:hint="confirm password"
- android:textSize="30sp"
- android:layout_weight="1"
- android:gravity="center"
- android:id="@+id/et_confirm_pass"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:layout_weight="1"
- android:layout_gravity="center">
- <Button android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:text="Register"
- android:textSize="30dp"
- android:layout_weight="1"
- android:id="@+id/btn_register"
- android:onClick="btnUserRegister"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:layout_weight="1">
- <Button android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:text="Cancel"
- android:textSize="30dp"
- android:layout_weight="1"
- android:id="@+id/btn_cancel"
- android:onClick="btnCancel"/>
- </LinearLayout>
- </LinearLayout>
- __________________________________________________________________________________________________________________
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- >
- <TextView android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint=""
- android:gravity="center"
- android:textSize="35dp" />
- <TextView android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="hello user"
- android:gravity="center"
- android:textSize="35dp"
- android:id="@+id/txt_hello"
- />
- <Button
- android:layout_gravity="center"
- android:padding="20dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="LogOut"
- android:textSize="25dp"
- android:onClick="btnLogOut"/>
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement