Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //TaskActivity
- //-----------------------
- package com.example.zeevm.mylogin;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class TaskActivity extends AppCompatActivity {
- Button btnAddTask,btnTest;
- Context context;
- TextView txtLogo;
- boolean isBackPressed=false;
- String spUser="";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_task);
- setPointer();
- getExtra();
- }
- private void setPointer()
- {
- this.context=this;
- txtLogo=(TextView)findViewById(R.id.txtHello);
- btnAddTask=(Button)findViewById(R.id.btnAddTask);
- btnAddTask.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showAdAddTask();
- }
- });
- btnTest=(Button)findViewById(R.id.btnTest);
- btnTest.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //show all the tasks.....
- String taskTest = UtlShared.getAllTask(context,spUser);
- Toast.makeText(context, taskTest, Toast.LENGTH_LONG).show();
- }
- });
- }
- private void showAdAddTask()
- {
- //create alert dialog builder
- AlertDialog.Builder adTask = new AlertDialog.Builder(context);
- //set the title
- //adTask.setTitle(getResources().getString(R.string.adTtlNewTask));
- /*
- //create new EditText instance
- EditText txtTask = new EditText(context);
- //set hint to the Edit Text
- txtTask.setHint(getResources().getString(R.string.adTaskHint));
- //add the EditText(View) to the Alert Dialog
- adTask.setView(txtTask);
- */
- //we are going to inflate our view (insert the entire layout to View object)
- View inflatedTask = LayoutInflater.from(context).inflate(R.layout.alert_dialog_new_task,null,false);
- //we setting pointer to our inflated view , and not the activity layout
- final EditText myNewTask = (EditText)inflatedTask.findViewById(R.id.txtNewTask);
- adTask.setView(inflatedTask);
- //set positive button
- adTask.setPositiveButton(getResources().getString(R.string.btnAddTask),
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- //handle add task.... (utl shared)
- String myTask = myNewTask.getText().toString();
- UtlShared.addTask(context,spUser,myTask);
- }
- });
- //set negative button
- adTask.setNegativeButton(getResources().getString(R.string.regBtnCancel), new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- //show message
- adTask.show();
- }
- private void getExtra()
- {
- String myData = getIntent().getStringExtra("userName");
- txtLogo.setText("Hello "+myData);
- //Toast.makeText(context, "user "+myData+" is loged in...", Toast.LENGTH_SHORT).show();
- spUser=getIntent().getStringExtra("user");
- }
- @Override
- public void onBackPressed() {
- if (!isBackPressed)
- {
- isBackPressed=true;
- Toast.makeText(context, "Press again to exit", Toast.LENGTH_SHORT).show();
- }
- else
- {
- finish();
- }
- }
- }
- ////////////////////////
- //UtlShared
- //-------------------------
- package com.example.zeevm.mylogin;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.icu.text.DateFormat;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- /**
- * Created by zeevm on 09/01/2017.
- */
- public class UtlShared {
- //getting context
- Context context;
- //declaration of shared preferences object
- SharedPreferences userPref;
- //declaration of shared preferences editor
- SharedPreferences.Editor editor;
- public UtlShared(Context context) {
- //get context to use it
- this.context = context;
- //declartion of shared preferences with file name and file mode(private)
- userPref = context.getSharedPreferences("users", Context.MODE_PRIVATE);
- //declartion of editor, for updating,changing,etc...
- editor = userPref.edit();
- }
- //add new user
- public void addUser(String userName, String userPass, String userNickName) {
- //stores in the phone device under data\data\package name
- //put in shared preferences user name and password
- editor.putString(userName, (userPass+","+userNickName));
- //commit (save/apply) the changes.
- editor.commit();
- }
- //check if user exists
- //return true if user exists, false if user not exists
- public boolean checkUser(String userName) {
- //get name by key->user name
- String checkString = userPref.getString(userName, "n/a");
- //check if username equals to responded data, if it's n/a , we dont have the user
- return !checkString.equals("n/a");
- }
- //check user name and password
- public String checkUserPassword(String userName, String userPass)
- {
- String checkString = userPref.getString(userName,"n/a"); // 1234|zeevik
- String[] myData = checkString.split(","); //myData[0]=1234 ; myData[1]=zeevik
- return userPass.equals(myData[0])?myData[1]:""; //1234 (password)
- }
- public static void addTask(Context context,String spUser, String userTask )
- {
- //declaration of shared prefences
- SharedPreferences myPref = context.getSharedPreferences(spUser,Context.MODE_PRIVATE);
- //declaration of shared prefences editor
- SharedPreferences.Editor editor=myPref.edit();
- //add new task (editor is working with key and value)
- editor.putString(userTask,"0");
- //apply/save changes
- editor.commit();
- }
- public static void taskStatusToggle(Context context, String spUser, String userTask)
- {
- //declaration of shared prefences
- SharedPreferences myPref = context.getSharedPreferences(spUser,Context.MODE_PRIVATE);
- //declaration of shared prefences editor
- SharedPreferences.Editor editor=myPref.edit();
- //get the current value
- boolean taskStatus=!(myPref.getString(userTask,"0").equals("1"));
- editor.putString(userTask,taskStatus?"1":"0");
- editor.commit();
- }
- public static String getAllTask(Context context,String spUser)
- {
- //go to specific preferences by user name....
- SharedPreferences taskPref = context.getSharedPreferences(spUser,context.MODE_PRIVATE);
- List<String> myTasks = new ArrayList<>();
- //get all tasks from shared preferences
- //the shared preferences is by key and value
- //therefor we will use Map Collection
- //we know that the key is string, but we don't know or sure
- //if the value is string, so we will be using <k,?>
- Map<String,?> tasks=taskPref.getAll();
- //transfer the data from map collection to list colliection
- //single item is like the defination of the tasks<String,?>
- //entry->record, entrySet -> set of records
- for (Map.Entry<String,?> singleTask:tasks.entrySet())
- {
- //insert task to list by key and value
- //we check if value is equal to 1, because 1=true/0=false
- myTasks.add(singleTask.getKey());
- }
- return myTasks.toString();
- }
- }
- ----------------------------
- //RegisterActivity
- ------------------------
- package com.example.zeevm.mylogin;
- import android.content.Context;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class RegisterActivity extends AppCompatActivity {
- EditText txtUser,txtPass,txtPassCheck, txtNick;
- Button btnRegister,btnCancel;
- Context context;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- setPointer();
- }
- private void setPointer()
- {
- this.context=this;
- txtUser=(EditText)findViewById(R.id.regUser);
- txtPass=(EditText)findViewById(R.id.regPass);
- txtPassCheck=(EditText)findViewById(R.id.regCheck);
- btnRegister=(Button)findViewById(R.id.regBtnRegister);
- btnRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- btnReg();
- }
- });
- btnCancel=(Button)findViewById(R.id.regBtnCancel);
- btnCancel.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- finish();
- }
- });
- txtNick=(EditText)findViewById(R.id.regNick);
- }
- private void btnReg()
- {
- //validation
- UtlShared utl = new UtlShared(context);
- //check if we have minimum length of user name
- if (txtUser.getText().toString().length()<2 ||
- txtNick.getText().toString().length()<2)
- {
- Toast.makeText(context, "Minimum of 2 chars for user or nick name", Toast.LENGTH_SHORT).show();
- return;
- }
- //check if user exists
- if (utl.checkUser(txtUser.getText().toString()))
- {
- Toast.makeText(context, "User exists", Toast.LENGTH_SHORT).show();
- return;
- }
- //check password
- if ((!txtPass.getText().toString().equals(txtPassCheck.getText().toString()) ||
- (txtPass.getText().toString().length()<3))) {
- Toast.makeText(context, "Password not match or less then 3 char", Toast.LENGTH_SHORT).show();
- return;
- }
- //register new user and finish
- utl.addUser(txtUser.getText().toString(),txtPass.getText().toString(),txtNick.getText().toString());
- finish();
- }
- }
- -----------------------
- //LoginActivity
- -----------------------
- package com.example.zeevm.mylogin;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.graphics.Color;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class LoginActivity extends AppCompatActivity {
- final String TAG="LoginScreen";
- EditText txtUser,txtPass;
- Button btnLogin,btnRegister;
- TextView txtLogo;
- //final String USER_NAME="Firas",USER_PASS="12345";
- Context context;
- UtlShared utl;
- //we need a flag to indicate registrion mode.
- boolean needReg=false;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- setPointer();
- }
- private void setPointer()
- {
- this.context=this;
- utl = new UtlShared(context);
- txtUser=(EditText) findViewById(R.id.txtUser);
- txtPass=(EditText)findViewById(R.id.txtPass);
- txtLogo=(TextView)findViewById(R.id.txtLogo);
- txtLogo.setOnLongClickListener(new View.OnLongClickListener() {
- @Override
- public boolean onLongClick(View v) {
- Toast.makeText(context, context.getResources().getString(R.string.msgLogo), Toast.LENGTH_SHORT).show();
- return true;
- }
- });
- final int orgHintColor=txtLogo.getCurrentHintTextColor();
- btnLogin=(Button)findViewById(R.id.btnLogin);
- btnLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View roni) {
- String userName=txtUser.getText().toString();
- String userPass=txtPass.getText().toString();
- //validation for user name
- if (userName.length()<1)
- {
- txtUser.setText("");
- txtUser.setHintTextColor(Color.RED);
- return;
- }
- if (userPass.length()<1)
- {
- txtPass.setText("");
- txtPass.setHintTextColor(Color.RED);
- return;
- }
- //check user
- //if (Utl.checkUser(userName,userPass,USER_NAME,USER_PASS))
- String userNick =utl.checkUserPassword(userName,userPass);
- if (userNick.length()>1)
- {
- //move now to second screen - which is our main screen
- Intent myIntent = new Intent(context,TaskActivity.class);
- myIntent.putExtra("userName",userNick);
- myIntent.putExtra("user",txtUser.getText().toString());
- startActivity(myIntent);
- finish();
- Log.i(TAG, "onClick: User OK");
- }
- else
- {
- userNotFound();
- }
- }
- });
- btnRegister=(Button)findViewById(R.id.btnRegister);
- btnRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent myIntent = new Intent(context,RegisterActivity.class);
- startActivity(myIntent);
- }
- });
- }
- //create alert dialog builder using instance of AlertDialog
- private void userNotFound()
- {
- //create two strings to hold Alert Dialog Title and Message
- String adMsg,adTitle,adPos;
- //getting user not found in our UtlShared
- UtlShared mySP = new UtlShared(context);
- //check if user exists or we have wrong password
- if (mySP.checkUser(txtUser.getText().toString()))
- {
- //user exists....
- adTitle = getResources().getString(R.string.adMsgTtlPassword);
- adMsg = getResources().getString(R.string.adMsgErrorPassword);
- adPos = getResources().getString(R.string.adBtnOK);
- }
- else
- {
- //user not exists...
- adTitle = getResources().getString(R.string.adTtlUserNotFound);
- adMsg = getResources().getString(R.string.adMsgZeStamMebalbel);
- adPos = getResources().getString(R.string.adBtnRegister);
- needReg=true;
- }
- //create builder
- AlertDialog.Builder adUser = new AlertDialog.Builder(context);
- //create title
- adUser.setTitle(adTitle);
- //create message
- adUser.setMessage(adMsg);
- //create positive button
- adUser.setPositiveButton(adPos
- , new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- if (needReg)
- {
- // go to registration activity
- Intent myIntent = new Intent(context,RegisterActivity.class);
- startActivity(myIntent);
- }
- else
- {
- dialog.dismiss();
- }
- }
- });
- //show the negative button only if user not exists
- if (needReg) {
- adUser.setNegativeButton(getResources().getString(R.string.adBtnCancel), new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- }
- //show alert dialog.
- adUser.show();
- }
- /*
- // user not found, using alert dialog in anonymous method
- private void userNotFound()
- {
- //user is incorrect -false
- txtPass.setText("");
- //Toast.makeText(context, "You are not allowed!!", Toast.LENGTH_LONG).show();
- //create alert dialog
- new AlertDialog.Builder(context)
- //create title for our alert dialog
- .setTitle(getResources().getString(R.string.adTtlUserError))
- //create message for our alert dialog
- .setMessage(getResources().getString(R.string.adMsgError))
- //create our OK button
- .setPositiveButton(getResources().getString(R.string.adBtnOK), new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- })
- //show Dialog
- .show();
- Log.e(TAG, "onClick: Incorrect User" );
- }
- */
- }
- ------------------------------------
- //alert_dialog_new_task
- -----------------------------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="@dimen/activity_vertical_margin">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/adTtlNewTask"
- android:textSize="26sp"
- android:gravity="center"
- android:id="@+id/textView" />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="@string/adTaskHint"
- android:layout_marginTop="20dp"
- android:id="@+id/txtNewTask"
- />
- </LinearLayout>
- ---------------------------
- //activity_task
- -----------------------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="32sp"
- android:text="place holder"
- android:gravity="center"
- android:layout_marginTop="15dp"
- android:id="@+id/txtHello"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@color/btnBackground"
- android:textColor="@color/btnText"
- android:text="@string/btnAddTask"
- android:textSize="@dimen/regBtn"
- android:layout_margin="@dimen/regBtnMargin"
- android:id="@+id/btnAddTask"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="show all task"
- android:id="@+id/btnTest"/>
- </LinearLayout>
- ----------------------
- //activity_register
- -------------------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:layout_weight="1">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:text="@string/ttlRegister"
- android:textSize="@dimen/ttl"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:layout_weight="1">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:hint="@string/regUserName"
- android:textSize="@dimen/regEditText"
- android:inputType="text"
- android:id="@+id/regUser"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:hint="@string/regNick"
- android:textSize="@dimen/regEditText"
- android:inputType="text"
- android:id="@+id/regNick"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:hint="@string/regUserPass"
- android:textSize="@dimen/regEditText"
- android:inputType="textPassword"
- android:id="@+id/regPass"
- />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:hint="@string/regUserCheck"
- android:textSize="@dimen/regEditText"
- android:inputType="textPassword"
- android:id="@+id/regCheck"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:layout_weight="1"
- android:gravity="center">
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@color/btnBackground"
- android:textColor="@color/btnText"
- android:text="@string/regBtnRegister"
- android:layout_margin="@dimen/regBtnMargin"
- android:textSize="@dimen/regBtn"
- android:id="@+id/regBtnRegister"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@color/btnBackground"
- android:textColor="@color/btnText"
- android:text="@string/regBtnCancel"
- android:layout_margin="@dimen/regBtnMargin"
- android:textSize="@dimen/regBtn"
- android:id="@+id/regBtnCancel"/>
- </LinearLayout>
- </LinearLayout>
- ------------------------------
- //activity_login
- --------------------------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <!-- my logo layout -->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:layout_weight="1"
- >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="@string/myLogo"
- android:textSize="@dimen/ttl"
- android:gravity="center"
- android:id="@+id/txtLogo"
- />
- </LinearLayout>
- <!-- my Edit text box -->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:layout_weight="1">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:hint="@string/hintUserName"
- android:textSize="@dimen/regEditText"
- android:layout_weight="1"
- android:id="@+id/txtUser"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="5px"
- android:orientation="vertical"
- android:background="#000"
- android:visibility="gone"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:hint="@string/hintPassword"
- android:textSize="@dimen/regEditText"
- android:inputType="numberPassword"
- android:layout_weight="1"
- android:id="@+id/txtPass"
- />
- </LinearLayout>
- <!-- my Login Button -->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:layout_weight="1">
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="#009fff"
- android:textColor="#ffffff"
- android:text="@string/btnLogin"
- android:textSize="@dimen/regBtn"
- android:id="@+id/btnLogin"
- android:layout_weight="1"
- android:layout_margin="@dimen/regBtnMargin"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@color/btnBackground"
- android:textColor="@color/btnText"
- android:text="@string/regBtnRegister"
- android:layout_weight="1"
- android:textSize="@dimen/regBtn"
- android:layout_margin="@dimen/regBtnMargin"
- android:id="@+id/btnRegister"/>
- </LinearLayout>
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement