Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- activity_main.xml
- =======================
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_margin="16dp"
- android:orientation="vertical">
- <!-- logo -->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:gravity="center"
- android:orientation="vertical">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="LOGO"
- android:textSize="64dp"
- android:layout_gravity="center"/>
- </LinearLayout>
- <!-- user input -->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="vertical"
- android:gravity="center">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="@string/userName"
- android:inputType="textVisiblePassword|textNoSuggestions"
- android:id="@+id/txtUser"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="@string/userPass"
- android:inputType="textWebPassword"
- android:layout_marginTop="20dp"
- android:id="@+id/txtPass"/>
- </LinearLayout>
- <!-- buttons -->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="horizontal"
- android:gravity="top">
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:layout_margin="20dp"
- android:text="@string/register"
- android:textColor="#ffffff"
- android:textSize="22sp"
- android:background="#009fff"
- android:id="@+id/btnRegister"
- android:onClick="onClick" />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:layout_margin="20dp"
- android:text="@string/login"
- android:textColor="#ffffff"
- android:textSize="22sp"
- android:background="#009fff"
- android:id="@+id/btnLogin"
- android:onClick="onClick" />
- </LinearLayout>
- </LinearLayout>
- activity_Register.xml
- ========================
- package com.example.sale7.myapplication3;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.widget.Toast;
- /**
- * Created by teacher on 12/21/2017.
- */
- public class UtlUserSP implements UserAble {
- Context context;
- public UtlUserSP() { }
- @Override
- public void setContext(Context context) {
- this.context=context;
- }
- @Override
- public boolean registerUser(String userName, String userPass) {
- //User Exists
- if (userExists(userName)) {
- Toast.makeText(context, "user already exists...", Toast.LENGTH_SHORT).show();
- return false;
- }
- //save user and password
- //declaration of shared preference
- SharedPreferences prefs = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
- //declaration of shared preference editor
- SharedPreferences.Editor editor = prefs.edit();
- //put data inside, we use hash map, so we need KEY,VALUE (k,v)
- editor.putString(userName, userPass);
- //sending data to my shared preferences file.
- editor.commit();
- return true;
- }
- @Override
- public boolean userExists( String userName) {
- //declaration of shared prefernces
- SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
- //get user pass
- String spPass=prefs.getString(userName,"na");
- //return if users exists, if not (na) return false
- return !spPass.equals("na");
- }
- @Override
- public boolean checkUser( String userName, String userPass) {
- //declaration of shared prefernces
- SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
- //get user password
- String spPass=prefs.getString(userName,"na");
- //return the result if the passwords are match
- return spPass.equals(userPass);
- }
- }
- =====================
- MainActivity.java
- =================
- package com.example.sale7.myapplication3;
- import android.support.v7.app.AppCompatActivity; // why is it not working well ??
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity { // My TASK PART 1
- final int DefualtName_DB = 11111;
- final String DefualtPassword_DB = "Same";
- EditText txtUser, txtPass;
- @Override
- protected void onCreate(Bundle savedInstanceState) { // "on Create" is the start of the LIFE OF "ACTIVITY"
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- }
- private void setPointer() {
- txtUser = findViewById(R.id.txtUser); //
- txtPass = findViewById(R.id.txtPass);
- }
- public void OnClick(View view) {
- String userName = txtUser.getText().toString(); // WE RELEATED THE XML PART TO THE JAVA PART
- String userPass = txtPass.getText().toString(); // SAME
- switch (view.getId()) {
- case (R.id.btnLogin):
- if (userName.equals(DefualtName_DB)) {
- if (userPass.equals(DefualtPassword_DB))
- Toast.makeText(MainActivity.this, "Login Successfully", Toast.LENGTH_SHORT).show();
- } else if ((!userName.equals(DefualtName_DB)) || (!userPass.equals(DefualtPassword_DB))){
- Toast.makeText(MainActivity.this, "Inccorect UserName Or Password", Toast.LENGTH_SHORT).show();
- }
- break;
- case (R.id.btnRegister):
- if (userName.length() == 0) {
- Toast.makeText(MainActivity.this, "InEfficient Input(Name)", Toast.LENGTH_SHORT).show();
- break;
- }
- if (userPass.length() == 0) {
- Toast.makeText(MainActivity.this, "InEfficient Input(Pass)", Toast.LENGTH_SHORT).show();
- break;
- }
- //TODO create a new username <--> md5 password LinkedHashMap
- break;
- }
- }
- }
- ==========
- shared prefrences
- ========
- package com.example.sale7.myapplication3;
- import android.content.Context;
- import android.content.SharedPreferences;
- /**
- * Created by teacher on 9/25/2017.
- */
- public class SharedPref {
- //for login screen
- public static boolean checkPass(String userName,String userPass, Context context) // CHECKIF PASSWORD EXISTS IS THE SYSTEM.
- {
- //declaration of shared preferences
- SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE); // ??
- //get user pass
- String spPass=prefs.getString(userName,"na"); // NA = NOT AVALIABLE
- //check if password matches
- if (userPass.equals(spPass))
- {
- return true;
- }
- return false;
- }
- public static boolean checkUserExists(String userName, Context context) // CHECK IF USER EXISTS IN THE SYSTEM
- {
- //declaration of shared preferences
- SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
- //get user pass
- String spPass=prefs.getString(userName,"na");
- //check if password matches
- return !spPass.equals("na"); // na = not available
- }
- public static void registerUser(String userName, String userPass, Context context) // WE SET A NEW USER NAME TO THIS CLASS..ALSO WE NEED AN EDITOR TO WRITE IN IT..
- {
- //declaration of shared preferences
- SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);// PUT DATA ISNIDE..SAVE THE CHANGES(comit) AT END.
- //declaration of shared preferences Editor
- SharedPreferences.Editor editor = prefs.edit();
- //put data inside, we use hash map , so we need KEY,VALUE (K,V)
- editor.putString(userName,userPass);
- //sending data to my shared prefernces file, we have to commit the changes
- editor.commit();
- }
- }
- ==========
- UtilUserSP
- =============
- package com.example.sale7.myapplication3;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.widget.Toast;
- /**
- * Created by teacher on 12/21/2017.
- */
- public class UtlUserSP implements UserAble {
- Context context;
- public UtlUserSP() { }
- @Override
- public void setContext(Context context) {
- this.context=context;
- }
- @Override
- public boolean registerUser(String userName, String userPass) {
- //User Exists
- if (userExists(userName)) {
- Toast.makeText(context, "user already exists...", Toast.LENGTH_SHORT).show();
- return false;
- }
- //save user and password
- //declaration of shared preference
- SharedPreferences prefs = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
- //declaration of shared preference editor
- SharedPreferences.Editor editor = prefs.edit();
- //put data inside, we use hash map, so we need KEY,VALUE (k,v)
- editor.putString(userName, userPass);
- //sending data to my shared preferences file.
- editor.commit();
- return true;
- }
- @Override
- public boolean userExists( String userName) {
- //declaration of shared prefernces
- SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
- //get user pass
- String spPass=prefs.getString(userName,"na");
- //return if users exists, if not (na) return false
- return !spPass.equals("na");
- }
- @Override
- public boolean checkUser( String userName, String userPass) {
- //declaration of shared prefernces
- SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
- //get user password
- String spPass=prefs.getString(userName,"na");
- //return the result if the passwords are match
- return spPass.equals(userPass);
- }
- }
- ========
- UserAble InterFace
- ====
- package com.example.sale7.myapplication3;
- import android.content.Context;
- /**
- * Created by User on 07/01/2018.
- */
- public interface UserAble {
- boolean registerUser(String userName, String userPass);
- boolean userExists(String userName);
- boolean checkUser(String userName,String userPass);
- void setContext(Context context);
- }
- ==========
- package com.example.sale7.myapplication3;
- import android.content.Context;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.example.sale7.myapplication3.UtlUserSP;
- /**
- * Created by teacher on 12/21/2017.
- */
- public class RegisterActivity extends AppCompatActivity {
- Context context;
- EditText regUser,regPass1,regPass2;
- EditText rName,rpass,rCheck;
- Button btnRegister,btnCancel,rCancel,rRegister;
- UtlUserSP userUtil=new UtlUserSP();
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register2);
- setPointer();
- }
- private void setPointer() {
- this.context=this;
- userUtil.setContext(context);
- // rName = findViewById(R.id.rUser);
- // rpass = findViewById(R.id.)
- regUser=findViewById(R.id.regUserName);
- regPass1=findViewById(R.id.regPass1);
- regPass2=findViewById(R.id.regPass2);
- btnCancel=findViewById(R.id.btnRegCancel);
- btnCancel.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- finish();
- }
- });
- btnRegister =findViewById(R.id.btnRegRegister);
- btnRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- //CHECK IF PASSWORD O.K
- if (!regPass1.getText().toString().equals(regPass2.getText().toString()))
- {
- Toast.makeText (context, "Password not match", Toast.LENGTH_SHORT).show();
- return;
- }
- if (regPass1.getText().toString().length()<3)
- {
- Toast.makeText(context, "Password must be 3 letter minimum", Toast.LENGTH_SHORT).show();
- return;
- }
- if(!userUtil.registerUser(regUser.getText().toString(),regPass1.getText().toString()))
- {
- Toast.makeText(context, "Error in user registration....", Toast.LENGTH_SHORT).show();
- return;
- }
- Toast.makeText(context, "new user is add...", Toast.LENGTH_SHORT).show();
- finish();
- }
- });
- }
- }
- =========
- Register.java
- ======
- package com.example.sale7.myapplication3;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class Register extends AppCompatActivity implements View.OnClickListener {
- EditText rUser, rPass, rCheck;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register2);
- setPointer();
- }
- private void setPointer() {
- rUser = (EditText) findViewById(R.id.regUserName);
- rPass = (EditText) findViewById(R.id.regPass1);
- rCheck = (EditText) findViewById(R.id.regPass2);
- }
- @Override
- public void onClick(View view) {
- /*switch (view.getId()) {
- case R.id.rCancel:
- finish();
- break;
- case R.id.rRegister:
- registerUser();
- break;
- default:
- Toast.makeText(this, "Error in buttons", Toast.LENGTH_SHORT).show();
- }*/
- }
- private void registerUser() {
- //check if all fields filled
- if (rUser.getText().toString().isEmpty() ||
- rPass.getText().toString().isEmpty() ||
- rCheck.getText().toString().isEmpty())
- {
- Toast.makeText(this, "Please fill all fields", Toast.LENGTH_LONG).show();
- return;
- }
- //check if minimum password length > 2
- if (rPass.getText().toString().length()<3)
- {
- Toast.makeText(this, "Password is too short (3 minimum)", Toast.LENGTH_SHORT).show();
- return;
- }
- //check if password match
- if (!rPass.getText().toString().equals(rCheck.getText().toString()))
- {
- Toast.makeText(this, "Password not match", Toast.LENGTH_LONG).show();
- return;
- }
- //check if user exists
- if (SharedPref.checkUserExists(rUser.getText().toString(),this))
- {
- Toast.makeText(this, "User already exists", Toast.LENGTH_LONG).show();
- return;
- }
- // AFTER REGISTERATION CHECKOUT THE DATA WILL BE RESTORED IN RTHE DATA BASE
- SharedPref.registerUser(rUser.getText().toString(),rPass.getText().toString(),this);
- finish();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement