Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //TaskAdapter
- --------------------
- package com.example.esra.myloginapp.Adapters;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.provider.ContactsContract;
- import android.support.v7.app.AlertDialog;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.CheckBox;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.TextView;
- import com.example.esra.myloginapp.Cls.ClsTask;
- import com.example.esra.myloginapp.R;
- import com.example.esra.myloginapp.UtlTask;
- import java.util.List;
- /**
- * Created by esra on 1/17/2017.
- */
- public class TaskAdapter extends BaseAdapter {
- Context context;
- String spUser;
- List<ClsTask> myTaskList;
- public TaskAdapter(Context context, List<ClsTask> myTaskList, String spUser) {
- this.context = context;
- this.myTaskList = myTaskList;
- this.spUser = spUser;
- }
- @Override
- public int getCount() {
- return myTaskList.size();
- }
- @Override
- public Object getItem(int position) {
- return null;
- }
- @Override
- public long getItemId(int position) {
- return 0;
- }
- @Override
- public View getView(final int position, View convertView, ViewGroup parent) {
- final ClsTask singleItem = myTaskList.get(position);
- View singleTask = LayoutInflater.from(context).inflate(R.layout.task_layout, null, false);
- TextView txtTask = (TextView) singleTask.findViewById(R.id.txtTask);
- txtTask.setText(singleItem.taskName);
- CheckBox cbIsDone = (CheckBox) singleTask.findViewById(R.id.cbIsDone);
- cbIsDone.setChecked(singleItem.taskIsDone);
- /*cbIsDone.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- AlertDialog.Builder adConfirmStatus=new AlertDialog.Builder(context);
- adConfirmStatus.setTitle("Warning!");
- adConfirmStatus.setMessage("Are you sure you want to toggle this task?");
- adConfirmStatus.setPositiveButton("Change Status", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- UtlTask.taskStatusToggle(context,spUser,singleItem.taskName);
- notifyDataSetChanged();
- }
- });
- adConfirmStatus.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- adConfirmStatus.show();
- }
- });*/
- ImageView btnTrash = (ImageView) singleTask.findViewById(R.id.btnTrash);
- btnTrash.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- AlertDialog.Builder adConfirmDelete=new AlertDialog.Builder(context);
- adConfirmDelete.setTitle("Warning!");
- adConfirmDelete.setMessage("are you sure you want to delete this task?");
- adConfirmDelete.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- UtlTask.removeTask(context, spUser, singleItem.taskName);
- myTaskList.remove(position);
- notifyDataSetChanged();
- }
- });
- adConfirmDelete.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- adConfirmDelete.show();
- }
- });
- return singleTask;
- }
- }
- ---------------------------
- //ClsTask
- -------------------------
- package com.example.esra.myloginapp.Cls;
- import java.util.Date;
- /**
- * Created by esra on 1/17/2017.
- */
- public class ClsTask {
- public Date taskDate;
- public Date taskDue;
- public String taskName;
- public String taskDetail;
- public Boolean taskIsDone;
- public ClsTask(String userTask,String myData)
- {
- String[]myTaskInfo=myData.split(",");
- this.taskDate=new Date();
- this.taskDue=new Date();
- this.taskName=userTask;
- this.taskDetail=myTaskInfo[3];
- this.taskIsDone=myTaskInfo[4].equals("1");
- }
- }
- --------------------------
- //LoginActivity
- -------------------------
- package com.example.esra.myloginapp;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- 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 {
- EditText txtUserName,txtPass;
- TextView logo;
- Button btnLogin,btnReg;
- Context context;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- setPointers();
- }
- private void setPointers()
- {
- this.context=this;
- txtUserName=(EditText)findViewById(R.id.txtUserName);
- txtPass=(EditText)findViewById(R.id.txtPass);
- logo=(TextView)findViewById(R.id.txtLogo);
- btnLogin=(Button)findViewById(R.id.btnLogin);
- btnReg=(Button)findViewById(R.id.btnReg);
- logo.setOnLongClickListener(new View.OnLongClickListener()
- {
- public boolean onLongClick(View v)
- {
- Toast.makeText(context,context.getResources().getString(R.string.msgLogo),Toast.LENGTH_SHORT).show();
- return true;
- }
- });
- final int ORG_COLOR=txtUserName.getCurrentTextColor();
- btnLogin.setOnClickListener(new View.OnClickListener() {
- UtlShared utl=new UtlShared(context);
- public void onClick(View v) {
- txtPass.setHintTextColor(ORG_COLOR);
- txtUserName.setHintTextColor(ORG_COLOR);
- if(txtUserName.getText().toString().length()<2||txtPass.getText().toString().length()<2)
- {
- Toast.makeText(context,"you had not fill the userName or the password",Toast.LENGTH_SHORT).show();
- txtUserName.setText("");
- txtPass.setText("");
- txtUserName.setHintTextColor(Color.RED);
- txtPass.setHintTextColor(Color.RED);
- return;
- }
- if(!(utl.checkPass((txtUserName.getText().toString()),(txtPass.getText().toString()))))
- {
- Toast.makeText(context,"the user name or the password are not correct",Toast.LENGTH_SHORT).show();
- return;
- }
- Intent myIntent=new Intent(context,TaskActivity.class);
- myIntent.putExtra("userName",utl.getNickName(txtUserName.getText().toString()));
- myIntent.putExtra("user",txtUserName.getText().toString());
- startActivity(myIntent);
- finish();
- }
- });
- btnReg.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent myIntent=new Intent(context,RegisterActivity.class);
- startActivity(myIntent);
- }
- });
- }
- }
- -------------------------
- //RegisterActivity
- -------------------------
- package com.example.esra.myloginapp;
- 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 {
- Context context;
- EditText txtUser,txtPass,checkPass,nickNmae;
- Button btnReg,btnCancel;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- setPointers();
- }
- private void setPointers()
- {
- this.context=this;
- txtUser=(EditText)findViewById(R.id.regTxtUser);
- txtPass=(EditText)findViewById(R.id.regTxtPass);
- checkPass=(EditText)findViewById(R.id.checkPass);
- nickNmae=(EditText)findViewById(R.id.nickName);
- btnReg=(Button)findViewById(R.id.regBtnReg);
- btnCancel=(Button)findViewById(R.id.regBtnCancel);
- btnReg.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- btnReg();
- }
- });
- btnCancel.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- finish();
- }
- });
- }
- private void btnReg()
- {
- UtlShared utl=new UtlShared(context);
- if(txtUser.getText().toString().length()<2||nickNmae.getText().toString().length()<2)
- {
- Toast.makeText(context,"your name or nickName is less than 2 chars",Toast.LENGTH_SHORT).show();
- return;
- }
- if(utl.checkUser(txtUser.getText().toString()))
- {
- Toast.makeText(context, "user is exists", Toast.LENGTH_SHORT).show();
- return;
- }
- if(txtPass.getText().toString().length()<3)
- {
- Toast.makeText(context,"the password is less than 3 chars",Toast.LENGTH_SHORT).show();
- return;
- }
- if(!txtPass.getText().toString().equals(checkPass.getText().toString()))
- {
- Toast.makeText(context, "the passwords don't match", Toast.LENGTH_SHORT).show();
- return;
- }
- utl.addUser(txtUser.getText().toString(),txtPass.getText().toString(),nickNmae.getText().toString());
- finish();
- }
- }
- --------------------------------
- //TaskActivity
- ------------------------------
- package com.example.esra.myloginapp;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.support.v7.app.AlertDialog;
- 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.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.example.esra.myloginapp.Adapters.TaskAdapter;
- public class TaskActivity extends AppCompatActivity {
- Context context;
- TextView taskLogo;
- Button btnAddTask;
- String spUser="";
- ListView lvTask;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_task);
- setPointers();
- getExtra();
- showData();
- }
- private void showData()
- {
- TaskAdapter myAdapter=new TaskAdapter(context,UtlTask.getAllTask(context,spUser),spUser);
- lvTask.setAdapter(myAdapter);
- }
- private void getExtra()
- {
- String myData=getIntent().getStringExtra("userName");
- taskLogo.setText("Hello "+myData);
- Toast.makeText(context,"user"+myData+"is logged in",Toast.LENGTH_SHORT).show();
- spUser=getIntent().getStringExtra("user");
- }
- private void setPointers()
- {
- this.context=this;
- taskLogo=(TextView)findViewById(R.id.taskLogo);
- btnAddTask=(Button)findViewById(R.id.btnAddTask);
- btnAddTask.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showAdAddTask();
- }
- });
- lvTask=(ListView)findViewById(R.id.lvTask);
- }
- private void showAdAddTask()
- {
- AlertDialog.Builder adTask=new AlertDialog.Builder(context);
- View inflatedTask= LayoutInflater.from(context).inflate(R.layout.alert_dialog_new_task,null,false);
- final EditText myNewTask=(EditText)inflatedTask.findViewById(R.id.txtNewTask);
- adTask.setView(inflatedTask);
- adTask.setPositiveButton(getResources().getString(R.string.adPosBtn), new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- String myTask=myNewTask.getText().toString();
- UtlTask.addTask(context,spUser,myTask);
- showData();
- }
- });
- adTask.setNegativeButton(getResources().getString(R.string.adNegBtn), new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- adTask.show();
- }
- }
- -----------------------------
- //UtlShared
- ---------------------------
- package com.example.esra.myloginapp;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.os.StrictMode;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- /**
- * Created by esra on 1/14/2017.
- */
- public class UtlShared {
- Context context;
- SharedPreferences userPref;
- SharedPreferences.Editor editor;
- public UtlShared(Context context)
- {
- this.context=context;
- userPref=context.getSharedPreferences("users",Context.MODE_PRIVATE);
- editor=userPref.edit();
- }
- //return true if user excists
- //else return false
- public boolean checkUser(String userName)
- {
- String checkString=userPref.getString(userName,"n/a");
- return !checkString.equals("n/a");
- }
- //returns true if password matches the user name
- //else returns false
- public boolean checkPass(String userName,String password)
- {
- if(!checkUser(userName))return false;
- String[] values=userPref.getString(userName,"n/a").split(",");
- String pass=values[0];
- return pass.equals(password);
- }
- public void addUser(String userName,String pass,String nickName)
- {
- editor.putString(userName,(pass+","+nickName));
- editor.commit();
- }
- public String getNickName(String userName)
- {
- String[] values=userPref.getString(userName,"n/a").split(",");
- return values[1];
- }
- }
- -------------------------
- //UtlTask
- -------------------------
- package com.example.esra.myloginapp;
- import android.content.Context;
- import android.content.SharedPreferences;
- import com.example.esra.myloginapp.Cls.ClsTask;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- /**
- * Created by esra on 1/17/2017.
- */
- public class UtlTask {
- public static void removeTask(Context context,String spUser,String userTask)
- {
- SharedPreferences myPref=context.getSharedPreferences(spUser,Context.MODE_PRIVATE);
- SharedPreferences.Editor editor=myPref.edit();
- editor.remove(userTask);
- editor.commit();
- }
- public static void taskStatusToggle(Context context,String spUser,String userTask)
- {
- SharedPreferences myPref=context.getSharedPreferences(spUser,Context.MODE_PRIVATE);
- SharedPreferences.Editor editor=myPref.edit();
- boolean taskStatus=!(myPref.getString(userTask,"0").equals("1"));
- editor.putString(userTask,taskStatus?"1":"0");
- editor.commit();
- }
- public static List<ClsTask> getAllTask(Context context, String spUser)
- {
- SharedPreferences myPref=context.getSharedPreferences(spUser,Context.MODE_PRIVATE);
- List<ClsTask> myTasks=new ArrayList<>();
- Map<String,?> tasks=myPref.getAll();
- for(Map.Entry<String,?> singleTask:tasks.entrySet())
- {
- myTasks.add(new ClsTask(singleTask.getKey(),singleTask.getValue().toString()));
- }
- return myTasks;
- }
- public static void addTask(Context context,String spUser,String userTask)
- {
- SharedPreferences myPref=context.getSharedPreferences(spUser,Context.MODE_PRIVATE);
- SharedPreferences.Editor editor=myPref.edit();
- editor.putString(userTask,"date"+","+"date"+","+userTask+","+"userDetail"+","+"0");
- editor.commit();
- }
- }
- ------------------------------------
- //activity_login
- -------------------------------
- <LinearLayout android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- xmlns:android="http://schemas.android.com/apk/res/android" >
- <!--my logo linearLayout-->
- <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/logo_login"
- android:gravity="center"
- android:textSize="@dimen/logo"
- android:id="@+id/txtLogo"/>
- </LinearLayout>
- <!--my user and pass 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:hint="@string/username_hint"
- android:layout_weight="1"
- android:id="@+id/txtUserName"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:hint="@string/pass_hint"
- android:id="@+id/txtPass"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="horizontal">
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/btn_login"
- android:layout_weight="1"
- android:layout_margin="@dimen/btn_margin"
- android:id="@+id/btnLogin"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/btn_reg"
- android:layout_weight="1"
- android:layout_margin="@dimen/btn_margin"
- android:id="@+id/btnReg"/>
- </LinearLayout>
- </LinearLayout>
- --------------------------
- //activity_register
- --------------------------
- <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:layout_weight="3">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="@string/reg_logo"
- android:textSize="@dimen/regLogo"
- android:gravity="center"
- />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="vertical">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:hint="@string/regHintUserName"
- android:id="@+id/regTxtUser"
- />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:hint="@string/regHintPass"
- android:id="@+id/regTxtPass"
- />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:hint="@string/checkPass"
- android:id="@+id/checkPass"
- />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:hint="@string/nickName"
- android:id="@+id/nickName"
- />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:layout_weight="3">
- <Button
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_margin="@dimen/btn_margin"
- android:text="@string/btn_reg"
- android:layout_weight="1"
- android:id="@+id/regBtnReg"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="@string/btn_cancel"
- android:layout_weight="1"
- android:layout_margin="@dimen/btn_margin"
- android:id="@+id/regBtnCancel"/>
- </LinearLayout>
- </LinearLayout>
- -----------------------
- //activity_task
- ------------------------
- <LinearLayout android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/task_logo"
- android:gravity="center"
- android:textSize="@dimen/taskLogo"
- android:id="@+id/taskLogo" />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/add_task"
- android:id="@+id/btnAddTask" />
- <ListView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/lvTask">
- </ListView>
- </LinearLayout>
- ---------------------------
- //alert_dialog_new_task
- ----------------------------
- <?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="wrap_content">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="@string/adLogo"
- android:gravity="center"
- android:textSize="@dimen/newTaskLogo"
- android:layout_weight="1.3"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:hint="@string/adTxtHint"
- android:id="@+id/txtNewTask" />
- </LinearLayout>
- ----------------------------
- //task_layout
- --------------------------
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <CheckBox
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/cbIsDone"
- android:layout_weight="5"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/txtTask"
- android:layout_weight="1"
- />
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnTrash"
- android:layout_weight="5"
- android:src="@drawable/delete_task"/>
- </LinearLayout>
- ----------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement