Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainActivity.java
- ========================
- package com.example.zeevm.mysharedpref;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.support.v7.app.AlertDialog;
- 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.TextView;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- EditText txtUserName;
- EditText txtUserPass;
- TextView lblMsg;
- Context context;
- private int totalTry=5;
- Button btnLogin;
- Button btnRegister;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- }
- private void setPointer()
- {
- this.context=this;
- txtUserName=(EditText)findViewById(R.id.txtUserName);
- txtUserPass=(EditText)findViewById(R.id.txtUserPassword);
- lblMsg=(TextView)findViewById(R.id.lblMsg);
- btnLogin=(Button)findViewById(R.id.btnLogin);
- btnRegister=(Button)findViewById(R.id.btnRegister);
- //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=txtUserPass.getText().toString();
- if (totalTry==0) {
- Toast.makeText(MainActivity.this, "You are an idiot!!", Toast.LENGTH_SHORT).show();
- return;
- }
- utlShared shared = new utlShared(this);
- if (shared.checkUserPassword(getUser,getPass))
- {
- Intent intent = new Intent(this,Welcome.class);
- intent.putExtra("userName",getUser);
- startActivity(intent);
- finish();
- }
- else {
- if (shared.checkUser(getUser))
- {
- Toast.makeText(context, "Wrong password", Toast.LENGTH_SHORT).show();
- }
- else {
- showLoginErrorDialog();
- }
- //Toast.makeText(MainActivity.this, "Wrong user name or password", Toast.LENGTH_SHORT).show();
- }
- }
- private void register()
- {
- startActivity(new Intent(this,Register.class));
- }
- private void showLoginErrorDialog()
- {
- new AlertDialog.Builder(this)
- //set title to dialog
- .setTitle("User not found")
- //set message to dialog
- .setMessage("Do you want to register new user")
- //set ok(yes) button
- .setPositiveButton("register", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- //close the dialog
- dialogInterface.dismiss();
- //start register activity
- startActivity(new Intent(context,Register.class));
- }
- })
- //set cancel(no) button
- .setNegativeButton("let me try again", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- }
- })
- //show the dialog
- .show();
- }
- }
- Register.java
- ==============
- package com.example.zeevm.mysharedpref;
- 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 Register extends AppCompatActivity {
- Button btnCancel;
- Button btnRegister;
- Context context;
- EditText txtUser,txtPass,txtPass2;
- @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.txtUserName);
- txtPass=(EditText)findViewById(R.id.txtPassword);
- txtPass2=(EditText)findViewById(R.id.txtPassword2);
- btnCancel=(Button)findViewById(R.id.btnCancel);
- btnCancel.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- finish();
- }
- });
- btnRegister=(Button)findViewById(R.id.btnRegisterUser);
- btnRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- //check if password equal
- String pass1=txtPass.getText().toString();
- String pass2=txtPass2.getText().toString();
- if (!pass1.equals(pass2))
- {
- Toast.makeText(context, "Password not match!!!", Toast.LENGTH_SHORT).show();
- return;
- }
- //calling the utl shared class
- utlShared myShared = new utlShared(context);
- //check if user exists
- String userName=txtUser.getText().toString();
- if (myShared.checkUser(userName))
- {
- Toast.makeText(context, "User Exists....", Toast.LENGTH_SHORT).show();
- return;
- }
- //running the method of add user
- myShared.addUser(userName,pass1);
- finish();
- }
- });
- }
- }
- TaskAdapter.java
- =================
- package com.example.zeevm.mysharedpref;
- 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 zeevm on 9/1/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.zeevm.mysharedpref;
- /**
- * Created by zeevm on 9/1/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.zeevm.mysharedpref;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.util.Log;
- /**
- * Created by zeevm on 8/25/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();
- }
- }
- Welcome.java
- ===================
- package com.example.zeevm.mysharedpref;
- 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.text.InputType;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.Toast;
- public class Welcome extends AppCompatActivity {
- String userName;
- Context context;
- utlShared myUtl;
- ListView taskList;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_welcome);
- setPointer();
- }
- private void setPointer()
- {
- this.context=this;
- userName=getIntent().getStringExtra("userName");
- myUtl = new utlShared(context);
- taskList=(ListView)findViewById(R.id.taskList);
- setListData();
- Toast.makeText(Welcome.this, "welcome user:"+userName, Toast.LENGTH_SHORT).show();
- }
- private void setListData()
- {
- TaskAdapter adapter = new TaskAdapter(context,userName);
- taskList.setAdapter(adapter);
- }
- public void addTask(View view)
- {
- //create builder
- AlertDialog.Builder builder = new AlertDialog.Builder(context);
- //set title
- builder.setTitle("Add task");
- //set up the input
- final EditText newTask = new EditText(context);
- newTask.setHint("Enter new task....");
- newTask.setInputType(InputType.TYPE_CLASS_TEXT);
- builder.setView(newTask);
- builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- }
- });
- builder.setPositiveButton("Add Task", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- String myTaskTest = newTask.getText().toString();
- Toast.makeText(context, "task name:"+myTaskTest, Toast.LENGTH_SHORT).show();
- }
- });
- builder.show();
- }
- public void addCustomTask(View view)
- {
- //create builder
- AlertDialog.Builder builder = new AlertDialog.Builder(context);
- //set title
- builder.setTitle("Add new task");
- //inflate view from layout ->custom layout,null,false as defualt values
- View viewInflated= LayoutInflater.from(context).inflate(R.layout.dlg_new_task,null,false);
- final EditText txtCustomTask = (EditText)viewInflated.findViewById(R.id.txtTask);
- builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- }
- });
- builder.setPositiveButton("Add task", 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 void addYesNo(View view)
- {
- AlertDialog.Builder builder = new AlertDialog.Builder(context);
- builder.setTitle("YES OR NO");
- builder.setMessage("Wanta a fanta???");
- builder.setPositiveButton("YES I WANT", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- Toast.makeText(context, "ENJOY", Toast.LENGTH_LONG).show();
- dialogInterface.dismiss();
- }
- });
- builder.setNegativeButton("ON DIET", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- Toast.makeText(context, "GoodBye Fatty!!", Toast.LENGTH_SHORT).show();
- dialogInterface.dismiss();
- }
- });
- builder.show();
- }
- }
- 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="LOGO"
- android:textSize="100sp">
- </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="Enter your name...."
- android:inputType="text"
- android:textSize="22sp" />
- <EditText
- android:id="@+id/txtUserPassword"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Enter Password..."
- 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="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="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="MY LOGIN SYSTEM V1"
- android:gravity="center"
- android:id="@+id/lblMsg"/>
- </LinearLayout>
- </LinearLayout>
- activity_register.xml
- =====================
- <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:layout_marginTop="20dp"
- android:gravity="center"
- android:text="USER REGISTERTION"
- android:textSize="32sp" />
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Enter User Name...."
- android:id="@+id/txtUserName"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Enter password...."
- android:id="@+id/txtPassword"
- android:layout_below="@id/txtUserName"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Enter password again..."
- android:id="@+id/txtPassword2"
- android:layout_below="@id/txtPassword"/>
- <Button
- android:layout_width="150dp"
- android:layout_height="wrap_content"
- android:id="@+id/btnRegisterUser"
- android:layout_below="@id/txtPassword2"
- android:text="Register"/>
- <Button
- android:layout_width="150dp"
- android:layout_height="wrap_content"
- android:text="Cancel"
- android:id="@+id/btnCancel"
- android:layout_toEndOf="@id/btnRegisterUser"
- android:layout_below="@id/txtPassword2"/>
- </RelativeLayout>
- </LinearLayout>
- activity_welcome.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="ADD TASK"
- 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="Enter task"
- android:id="@+id/txtTask"/>
- </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>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement