Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainActivity.java
- ========================================================================
- package com.example.adm.myapplication;
- import android.app.Activity;
- import android.app.Fragment;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.net.Uri;
- 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.TextView;
- import android.widget.Toast;
- import android.content.Context;
- import com.google.android.gms.appindexing.Action;
- import com.google.android.gms.appindexing.AppIndex;
- import com.google.android.gms.common.api.GoogleApiClient;
- import java.util.Objects;
- public class MainActivity extends AppCompatActivity {
- EditText txtUserName;
- EditText txtUserPassword;
- TextView lblMsg;
- Context context;
- private int totalTry = 5;
- Button btnLogin;
- Button btnRegister;
- EditText txtUser,txtPass,txtPass2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- this.context = this;
- }
- private void setPointer() {
- this.context = this;
- lblMsg = (TextView) findViewById(R.id.lblMsg);
- btnLogin = (Button) findViewById(R.id.btnLogin);
- btnRegister = (Button) findViewById(R.id.btnRegister);
- txtUser = (EditText) findViewById(R.id.txtUserName);
- txtPass = (EditText) findViewById(R.id.txtPassword);
- txtPass2 = (EditText) findViewById(R.id.txtPassword2);
- txtUserName = (EditText) findViewById(R.id.txtUserName);
- txtUserPassword = (EditText) findViewById(R.id.txtUserPassword);
- //listerners
- btnLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- login();
- }
- });
- btnRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- register();
- }
- });
- }
- private void login() {
- String getUser=txtUserName.getText().toString();
- String getPass=txtUserPassword.getText().toString();
- if (totalTry == 0) {
- Toast.makeText(MainActivity.this, R.string.blocked, Toast.LENGTH_SHORT).show();
- return;
- }
- utlShared shared = new utlShared(this);
- if (shared.checkUserPassword(getUser, getPass)) {
- lblMsg.setText(R.string.userLoged);
- Intent intent = new Intent(this, Welcom.class);
- intent.putExtra("userName", getUser);
- startActivity(intent);
- finish();
- } else {
- if (shared.checkUser(getUser)) {
- Toast.makeText(MainActivity.this, R.string.wrongpass, Toast.LENGTH_LONG).show();
- } else {
- showLoginErrorDialog();
- }
- }
- }
- public void register() {
- //create builder
- android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
- //set title
- builder.setTitle(R.string.register);
- //inflate view from layout ->custom layout,null,false as defualt values
- View viewInflated = LayoutInflater.from(context).inflate(R.layout.dlg_new_task, null, true);
- final EditText txtUser = (EditText)viewInflated.findViewById(R.id.txtUserName);
- final EditText txtPass = (EditText)viewInflated.findViewById(R.id.txtPassword);
- final EditText txtPass2 = (EditText)viewInflated.findViewById(R.id.txtPassword2);
- builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- }
- });
- final utlShared myShared = new utlShared(context);
- builder.setPositiveButton(R.string.register, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- String pass1=txtPass.getText().toString();
- String pass2=txtPass2.getText().toString();
- if (!pass1.equals(pass2)) {
- Toast.makeText(context, R.string.errorSamepass, Toast.LENGTH_LONG).show();
- register();
- return;
- }
- utlShared myShared = new utlShared(context);
- String userName=txtUser.getText().toString();
- //check if user exists
- if (myShared.checkUser(userName)) {
- Toast.makeText(context, R.string.userExixst, Toast.LENGTH_SHORT).show();
- register();
- return;
- }
- //running the method of add user
- myShared.addUser(userName,pass1);
- }
- });
- //display our inflated view in screen
- builder.setView(viewInflated);
- //show the dialog
- builder.show();
- }
- private void showLoginErrorDialog() {
- final String getUser = txtUser.getText().toString();
- final utlShared shared = new utlShared(context);
- if (! shared.checkUser(getUser)) {
- new AlertDialog.Builder(context).setTitle(R.string.user_not_found)
- .setMessage(R.string.wantToRegist)
- .setPositiveButton(R.string.register, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- register();
- }
- })
- .setNegativeButton(R.string.tryAgain, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- Toast.makeText(context, R.string.tryAgain, Toast.LENGTH_SHORT).show();
- dialogInterface.dismiss();
- }
- })
- .show();
- }
- }
- }
- ======================================================================
- TaskAdabter.java
- ======================================================================
- package com.example.adm.myapplication;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.Switch;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- /**
- * Created by ADM on 9/3/2016.
- */
- public class TaskAdapter extends BaseAdapter {
- //transfer context
- Context context;
- //transfer user to use for shared preferences
- String userName;
- //create a list of tasks.....
- List<taskItem> myTasks;
- //constructor, for creating the adapter we need from the user context and userName
- public TaskAdapter(Context context,String userName) {
- this.context = context;
- this.userName=userName;
- //go to user shared preferences and fill the list
- getData();
- }
- //how many item to display
- @Override
- public int getCount() {
- //return the myTasks size....
- return myTasks.size();
- }
- //return a specific item by index
- @Override
- public Object getItem(int i) {
- return myTasks.get(i);
- }
- //return index number
- @Override
- public long getItemId(int i) {
- return 0;
- }
- //create our view
- @Override
- public View getView(int index, View view, ViewGroup viewGroup) {
- //inflate the view inside view object -> viewInflated
- View viewInflated = LayoutInflater.from(context).inflate(R.layout.task_item,null,false);
- //set our inflated view behiver
- //set pointer for our inflated view
- //set pointer for task name....
- final TextView txtTaskName=(TextView)viewInflated.findViewById(R.id.taskName);
- //set pointer for task status....
- final Switch swTask=(Switch)viewInflated.findViewById(R.id.taskDone);
- //set task name, by the index of my myTasks collection
- txtTaskName.setText(myTasks.get(index).taskName);
- //set task status , switch is getting true/false
- swTask.setChecked(myTasks.get(index).taskStatus);
- //create listener event, when switch is pressed
- swTask.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- //we using utlShared to update task status
- //create instance of utlShared
- utlShared myShared = new utlShared(context);
- //calling method of task, and giving userName(shared preferences, taskName, taskStatus)
- myShared.task(userName,txtTaskName.getText().toString(),swTask.isChecked());
- //we sending a message to the user, and inform him/her about the change
- Toast.makeText(context, swTask.isChecked()?"task done" :"Task undone...", Toast.LENGTH_SHORT).show();
- }
- });
- //return the view with the behiver.....
- return viewInflated;
- }
- private void getData()
- {
- //go to specific shared preferences by user name.....
- SharedPreferences taskPref=context.getSharedPreferences(userName,context.MODE_PRIVATE);
- //create instance of our myTasks list
- myTasks = new ArrayList<>();
- //get all tasks from shared preferances
- //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 what type of value we will get. <K,?>
- Map<String,?> tasks=taskPref.getAll();
- // transfer the data from map collection to list collection , single item is like the defination of the tasks <String,?>
- //Entry -> record , enterSet -> 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, becuase 1=true 0=false
- myTasks.add(new taskItem(singleTask.getKey(),singleTask.getValue().equals("1")));
- }
- }
- =======================================================================
- taskItem.java
- ==================================================================
- package com.example.adm.myapplication;
- /**
- * Created by ADM on 9/3/2016.
- */
- public class taskItem {
- public String taskName;
- public boolean taskStatus;
- public taskItem(String taskName, boolean taskStatus) {
- this.taskName = taskName;
- this.taskStatus = taskStatus;
- }
- }
- ===============================================================================
- utlShared.java
- ====================================================================================
- package com.example.adm.myapplication;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.renderscript.Sampler;
- import android.text.Editable;
- import android.util.Log;
- import java.util.jar.Attributes;
- /**
- * Created by ADM on 8/26/2016.
- */
- public class utlShared {
- //context to use later
- Context context;
- //declatrtion of shared preferences object
- private SharedPreferences userPref;
- //declaration of shared preferences editor
- private SharedPreferences.Editor editor;
- public utlShared() {}
- public utlShared(Context context)
- {
- //get context to use it
- this.context=context;
- //declaretion of shared preferences with file name and file mode (private,public)
- userPref=context.getSharedPreferences("users",Context.MODE_PRIVATE);
- //declaration of editor
- editor=userPref.edit();
- }
- //get user and password
- public void addUser(String userName, String password)
- {
- //stores in the phone device under data\data\package name
- //put in shared preferences user name and password
- editor.putString(userName,password);
- //commit (save/apply) the changes.
- editor.commit();
- }
- public boolean checkUser(String userName)
- {
- //get name by key->userName
- String checkString = userPref.getString(userName,"na");
- //print to logcat a custom message.....
- Log.e("checkUser", "checkUser: "+checkString );
- //check if userName equals to responded data, if it's na, we don't havce the user...
- return !checkString.equals("na");
- }
- public boolean checkUserPassword(String userName, String userPassword)
- {
- String checkString = userPref.getString(userName,"na");
- return checkString.equals(userPassword);
- }
- public void task(String userName,String taskName,boolean taskDone)
- {
- //pointer to user task shared preferances
- SharedPreferences taskPref=context.getSharedPreferences(userName,Context.MODE_PRIVATE);
- //create editor to change the specific shared preferences
- SharedPreferences.Editor taskEditor=taskPref.edit();
- //add new task -> if true write 1 else write 0
- taskEditor.putString(taskName,taskDone?"1":"0");
- //apply the changes
- taskEditor.commit();
- }
- public void res(String userName) {
- SharedPreferences taskPref = context.getSharedPreferences(userName, context.MODE_PRIVATE);
- SharedPreferences.Editor taskEditor = taskPref.edit();
- taskEditor.clear();
- taskEditor.commit();
- }
- }
- ========================================================================================================
- Welcom.java
- ===================================================================================================================================
- package com.example.adm.myapplication;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.media.Image;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.text.InputType;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.Window;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- public class Welcom extends AppCompatActivity {
- String userName;
- Context context;
- utlShared myUtl;
- ListView taskList;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_welcom);
- setPointer();
- }
- private void setPointer()
- {
- this.context=this;
- userName=getIntent().getStringExtra("userName");
- myUtl = new utlShared(context);
- taskList=(ListView)findViewById(R.id.taskList);
- setListData();
- Toast.makeText(Welcom.this, "Welcome " +userName, Toast.LENGTH_SHORT).show();
- }
- private void setListData()
- {
- TaskAdapter adapter = new TaskAdapter(context,userName);
- taskList.setAdapter(adapter);
- }
- public void addCustomTask(View view)
- {
- //create builder
- AlertDialog.Builder builder = new AlertDialog.Builder(context);
- //set title
- builder.setTitle(R.string.addnewtask);
- //inflate view from layout ->custom layout,null,false as defualt values
- View viewInflated= LayoutInflater.from(context).inflate(R.layout.weed,null,false);
- final EditText txtCustomTask = (EditText)viewInflated.findViewById(R.id.txtTask);
- builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- }
- });
- builder.setPositiveButton(R.string.addtask, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- String myTask = txtCustomTask.getText().toString();
- //Toast.makeText(context, "task is:"+myText, Toast.LENGTH_SHORT).show();
- myUtl.task(userName,myTask,false);
- /////
- setListData();
- }
- });
- //display our inflated view in screen
- builder.setView(viewInflated);
- //show the dialog
- builder.show();
- }
- public boolean onCreateOptionsMenu(Menu menu) {
- //create MenuInflater object
- MenuInflater inflater=getMenuInflater();
- //inflate the menu
- inflater.inflate(R.menu.menu,menu);
- return super.onCreateOptionsMenu(menu);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId())
- {
- case R.id.itemLogOut:
- Intent intent = new Intent(this,MainActivity .class);
- startActivity(intent);
- finish();
- break;
- case R.id.itemReset:
- new AlertDialog.Builder(context).setTitle(R.string.reset)
- .setMessage(R.string.suretorest)
- .setPositiveButton(R.string.reset, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- taskList.setAdapter(null);
- myUtl.res(userName);
- dialogInterface.dismiss();
- }
- })
- .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- Toast.makeText(context, R.string.cancel, Toast.LENGTH_SHORT).show();
- dialogInterface.dismiss();
- }
- })
- .show();
- break;
- case R.id.itemAbout:
- android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
- //set title
- builder.setTitle(R.string.about);
- //inflate view from layout ->custom layout,null,false as defualt values
- View viewInflated = LayoutInflater.from(context).inflate(R.layout.about_msg, null, true);
- TextView txtView = new TextView(context);
- //positive button to dissmiss the dialog by pressing OK
- builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- }
- });
- //show the builder...
- builder.setView(viewInflated);
- builder.show();
- }
- return super.onOptionsItemSelected(item);
- }
- }
- =================================================================================================
- about_msg.xml
- ===================================================================================================================
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <ImageView
- android:id="@+id/goProDialogImage"
- android:layout_width="wrap_content"
- android:layout_height="300dp"
- android:src="@drawable/sexypolice"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/adamBuild"
- android:textSize="30sp"
- android:gravity="center"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/date"
- android:textSize="30sp"
- android:gravity="center"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/zeev"
- android:textSize="30sp"
- android:gravity="center"/>
- </LinearLayout>
- ==========================================================================================================
- activity_main.xml
- =====================================================================
- <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="150dp"
- android:orientation="vertical">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:text="@string/myLogo"
- android:textSize="50sp">
- </TextView>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="100dp"
- android:orientation="vertical">
- <EditText
- android:id="@+id/txtUserName"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="@string/user_name"
- android:gravity="center"
- android:inputType="text"
- android:textSize="22sp" />
- <EditText
- android:id="@+id/txtUserPassword"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="@string/password1"
- android:gravity="center"
- android:inputType="textPassword"
- android:textSize="22sp" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="150dp"
- android:orientation="vertical">
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/login"
- android:background="#00BFFF"
- android:textColor="#ffffff"
- android:textSize="32sp"
- android:id="@+id/btnLogin"
- android:layout_marginBottom="20dp"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/register"
- android:background="#00BFFF"
- android:textColor="#ffffff"
- android:textSize="32sp"
- android:id="@+id/btnRegister"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:textSize="32sp"
- android:text="@string/my_login_system"
- android:gravity="center"
- android:id="@+id/lblMsg"/>
- </LinearLayout>
- </LinearLayout>
- ===================================================================
- activity_welcom.xml
- ================================================================================
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- >
- <Button android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/addtask"
- android:onClick="addCustomTask"
- />
- <ListView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/taskList"
- />
- </LinearLayout>
- ==================================================================================
- dlg_new_task.xml
- =======================================================================================
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:gravity="center">
- <ImageView
- android:layout_width="100dp"
- android:layout_height="100dp"
- android:src="@drawable/task"/>
- <EditText
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:hint="@string/user_name"
- android:id="@+id/txtUserName"/>
- <EditText
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:hint="@string/password1"
- android:id="@+id/txtPassword"/>
- <EditText
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:hint="@string/password2"
- android:id="@+id/txtPassword2"/>
- </LinearLayout>
- ===============================================================================================
- task_item.xml
- ==========================================================================================================================
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:orientation="horizontal"
- android:layoutDirection="rtl"
- android:layout_margin="20dp"
- >
- <Switch
- android:layout_width="40dp"
- android:layout_height="wrap_content"
- android:id="@+id/taskDone"
- />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="22sp"
- android:id="@+id/taskName"
- android:text="EXAMPLE FOR TASK"
- />
- </LinearLayout>
- ==============================================================================================================================
- weed.xml
- ========================================================================================================================
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:gravity="center">
- <ImageView
- android:layout_width="100dp"
- android:layout_height="100dp"
- android:src="@drawable/task"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="@string/addtask"
- android:id="@+id/txtTask"/>
- </LinearLayout>
- ===================================================================
- menu.xml
- =========================================================================
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android">
- <item
- android:id="@+id/itemLogOut"
- android:title="@string/logout"/>
- <item
- android:id="@+id/itemReset"
- android:title="@string/reset"/>
- <item
- android:id="@+id/itemAbout"
- android:title="@string/about"/>
- </menu>
- =============================================================================
- strings.xml
- =============================================================================
- <resources>
- <string name="app_name"> האפלקציה שלי </string>
- <string name="user_name"> שם משתמש </string>
- <string name="password1"> סיסמה </string>
- <string name="password2"> סיסמה עוד פעם</string>
- <string name="register"> הרשמה </string>
- <string name="login"> כניסה </string>
- <string name="cancel"> חזרה </string>
- <string name="regist_accept"> נרשמת בהצלחה </string>
- <string name="welcome"> ברוך הבא </string>
- <string name="myLogo"> הלוגו שלי </string>
- <string name="my_login_system"> מערכת הרשמה שלי </string>
- <string name="user_not_found"> משתמש לא נמצא </string>
- <string name="wantToRegist"> רוצה להירשם </string>
- <string name="tryAgain"> נסה שוב </string>
- <string name="errorSamepass"> סיסמאות לא זהות </string>
- <string name="userExixst"> משתמש קיים </string>
- <string name="userLoged"> משתמש נכנס </string>
- <string name="blocked"> נחסמת </string>
- <string name="wrongpass"> סיסמה שגויה </string>
- <string name="addnewtask"> הוספ משימה חדשה </string>
- <string name="addtask"> הוסף משימה </string>
- <string name="taskis"> משימה היא </string>
- <string name="taskdone"> משימה בוצעה </string>
- <string name="taskUndone"> משיצה לא בומעה </string>
- <string name="reset"> איפוס כל המשימות </string>
- <string name="logout"> יציאה </string>
- <string name="about"> עלינו </string>
- <string name="ok"> סבבה </string>
- <string name="adamBuild"> פטריות הזיה </string>
- <string name="date"> 9/6/2016 </string>
- <string name="zeev">זאאב זה מה שרציתה</string>
- <string name="suretorest"> בטוח שרוצה לאפס </string>
- </resources>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement