Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HomeWork MyToDoList by Igor
- xml activity_main======================================================================================================================
- <RelativeLayout
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:id="@+id/mainTag"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="30sp"
- android:hint="Username"
- android:gravity="center"
- android:id="@+id/inUser"
- android:layout_marginTop="15sp"
- />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="30sp"
- android:inputType="textPassword"
- android:hint="Password"
- android:gravity="center"
- android:id="@+id/inPass"
- android:layout_below="@+id/inUser"
- android:layout_marginTop="5sp"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="30sp"
- android:text="Login"
- android:id="@+id/btnLog"
- android:layout_below="@+id/inPass"
- android:layout_marginTop="40sp"
- android:padding="20sp"
- android:onClick="btnLog"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Register"
- android:textSize="25sp"
- android:id="@+id/btnReg"
- android:layout_below="@+id/btnLog"
- android:layout_marginTop="10sp"
- android:padding="10sp"
- android:onClick="btnReg"
- />
- </RelativeLayout>
- ====================================================================================================================================
- Java MainActivity===================================================================================================================
- package com.example.igor.mytodolist;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends Activity
- {
- //creating local EditText variables to connect with XML tag ids
- // username password
- private EditText userTxt, userPass;
- //Creating a SharedPRefrences
- private SharedPreferences myPref;
- private SharedPreferences.Editor editor;
- //will be used to check if the username is online
- private String userConnected;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPoints();
- //checks if user has allready logged in the past
- if(chkOnline())
- {
- //if he did and the method is true go direclty to Logged page
- Intent logged = new Intent(this,Logged.class);
- logged.putExtra("userOn",userConnected); // if logged transfer this data onward
- this.startActivity(logged);
- }
- }
- private void setPoints()
- {
- //connects the local vars with the id'd tags
- userTxt = (EditText)findViewById(R.id.inUser);
- userPass = (EditText)findViewById(R.id.inPass);
- //Setting up the SharedPrefrences //working with the clients(users) file
- myPref = getApplicationContext().getSharedPreferences(Register.myClients,MODE_PRIVATE);
- editor = myPref.edit();
- }
- //method that is caleld by the button to check if all info is valid
- private boolean chkUser()
- {
- String userInput = userTxt.getText().toString();
- String passInput = userPass.getText().toString();
- //checks if all fields are correct
- if(userInput.trim().equals("") || passInput.trim().equals(""))
- {
- Toast.makeText(this,"Error, Please fill all the fields", Toast.LENGTH_SHORT).show();
- return false;
- }
- //checks if username and password are correct!
- if(!myPref.contains(userInput) || !passInput.equals(myPref.getString(userInput,passInput)))
- {
- Toast.makeText(this,"Error, Wrong username or password!", Toast.LENGTH_SHORT).show();
- return false;
- }
- // if none of the if statements accure save the Data in userOn and return true
- editor.putString("userOn",userInput).commit();
- return true;
- }
- //method that checks if the username has logged in the past
- private boolean chkOnline()
- {
- userConnected = myPref.getString("userOn","offline");
- return !userConnected.equals("offline");
- }
- //button that calls upon method that checks users name and password and decides if to continue
- public void btnLog(View v)
- {
- if(chkUser())
- {
- Toast.makeText(this,"Logged in Succesfully",Toast.LENGTH_SHORT).show();
- Intent logIn = new Intent(this,Logged.class);
- this.startActivity(logIn);
- }
- }
- //button that sends user to the registration
- public void btnReg(View v)
- {
- Intent goReg = new Intent(this, Register.class);
- this.startActivity(goReg);
- }
- }
- ====================================================================================================================================
- XML activity_register===============================================================================================================
- <RelativeLayout
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Registration"
- android:gravity="center"
- android:textSize="20sp"
- android:id="@+id/regLine"
- android:layout_marginTop="20sp"
- />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="UserName"
- android:gravity="center"
- android:textSize="30sp"
- android:id="@+id/setUser"
- android:layout_below="@+id/regLine"
- android:layout_marginTop="15sp"
- />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Password"
- android:inputType="textPassword"
- android:gravity="center"
- android:textSize="30sp"
- android:id="@+id/setPass"
- android:layout_below="@+id/setUser"
- android:layout_marginTop="30sp" />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Re-Password"
- android:inputType="textPassword"
- android:gravity="center"
- android:textSize="30sp"
- android:id="@+id/confPass"
- android:layout_below="@+id/setPass"
- android:layout_marginTop="30sp" />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Register"
- android:textSize="30sp"
- android:id="@+id/tryReg"
- android:layout_below="@+id/confPass"
- android:layout_marginTop="10sp"
- android:onClick="btnReg"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Cancle"
- android:textSize="20sp"
- android:id="@+id/regCanc"
- android:layout_below="@+id/tryReg"
- android:layout_marginTop="20sp"
- android:onClick="btnCncl"
- />
- </RelativeLayout>
- ====================================================================================================================================
- Java Register=======================================================================================================================
- package com.example.igor.mytodolist;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- /**
- * Created by Igor on 3/19/2016.
- */
- public class Register extends Activity
- {
- //Creating final variable to set SharedPRefrences file name (XML filename)
- public static final String myClients = "account";
- private SharedPreferences myPref;
- private SharedPreferences.Editor editor;
- //creating 3 EditText variables to connect them with the XML file
- // /*username password confrim password*/
- private EditText inptUser, inpPass, confPass;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- setPoints();
- }
- //setting up all the variables in a method that will be excuted in /*onCreate*/ method
- private void setPoints()
- {
- //connecting local variables with XML id'd tags
- inptUser =(EditText)findViewById(R.id.setUser);
- inpPass =(EditText)findViewById(R.id.setPass);
- confPass =(EditText)findViewById(R.id.confPass);
- //setting up our SharedPrefrences file
- myPref = getApplicationContext().getSharedPreferences(myClients,MODE_PRIVATE);
- editor = myPref.edit();
- }
- private boolean chkReg()
- {
- String userName = inptUser.getText().toString();
- String passWord = inpPass.getText().toString();
- String confirm = confPass.getText().toString();
- //checks if all fields are filled
- if( userName.trim().equals("") || passWord.trim().equals("")
- || confirm.toLowerCase().equals("") )
- {
- Toast.makeText(this,"Error, Please make sure all fields are full!",Toast.LENGTH_SHORT).show();
- return false;
- }
- //checks if passwords match
- if(!confirm.equals(passWord))
- {
- Toast.makeText(this,"Error, Please make sure passwords match!",Toast.LENGTH_SHORT).show();
- return false;
- }
- //checks if the username is availble
- if(myPref.contains(userName))
- {
- Toast.makeText(this,"Error, this username has allready been taken!",Toast.LENGTH_SHORT).show();
- return false;
- }
- //if all these statements do not accure just input the data given and return true
- editor.putString(userName, passWord).commit();
- Toast.makeText(this,"Success, Account has been registered!",Toast.LENGTH_SHORT).show();
- return true;
- }
- //Register button that chekcs if account is valid
- public void btnReg(View v)
- {
- if(chkReg())
- {
- Intent bckLog = new Intent(this,MainActivity.class);
- this.startActivity(bckLog);
- finish();
- }
- }
- //Cancle Registration and return to login
- public void btnCncl(View v)
- {
- Intent rtnLog = new Intent(this,MainActivity.class);
- this.startActivity(rtnLog);
- finish();
- }
- }
- ====================================================================================================================================
- XML activity_logged=================================================================================================================
- <RelativeLayout
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:id="@+id/logPage"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/logMsg"
- android:layout_marginTop="25sp"
- android:textSize="50sp"
- />
- <ListView
- android:id="@+id/idList"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/logMsg"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnTask"
- android:layout_below="@+id/idList"
- android:text="Add Task"
- android:textSize="30sp"
- android:gravity="center"
- android:layout_marginTop="20sp"
- android:onClick="btnTask"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnOut"
- android:layout_below="@+id/btnTask"
- android:text="log out"
- android:textSize="30sp"
- android:gravity="center"
- android:layout_marginTop="20sp"
- android:onClick="btnLogOut"
- />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceSmall"
- android:text="Created by Igor"
- android:id="@+id/whoLog"/>
- </RelativeLayout>
- ====================================================================================================================================
- JAVA Logged=========================================================================================================================
- package com.example.igor.mytodolist;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ListView;
- import android.widget.TextView;
- import java.util.ArrayList;
- import java.util.Map;
- /**
- * Created by Igor on 3/19/2016.
- */
- public class Logged extends Activity
- {
- TextView usrName;//will be used to display user that has logged
- ListView myList; //will be used to connect all the tasks on a listview
- //SharedPrefrences
- SharedPreferences myPref;
- SharedPreferences myData; // will be used to select each users database of tasks!
- SharedPreferences.Editor editor;
- //setting up an arraylist to hold all of the tasks
- ArrayList<String> myTasks = new ArrayList<String>();
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_logged);
- setPoints();
- //say hello user
- usrName.setText("Hello "+ getUsername());
- }
- //returns the name of the user that is currently online
- private String getUsername()
- {
- String userName = myPref.getString("userOn","offline");
- return userName;//returns current username online
- }
- //settingup basic points in the onCreate
- private void setPoints()
- {
- //connects the id with variable
- usrName = (TextView)findViewById(R.id.logMsg);
- myList = (ListView)findViewById(R.id.idList);
- //setting up the prefrences
- myPref = getApplicationContext().getSharedPreferences(Register.myClients,MODE_PRIVATE);
- editor = myPref.edit();
- //setting up specific user database of tasks
- myData = getApplicationContext().getSharedPreferences(getUsername(),MODE_PRIVATE);
- }
- protected void onResume()
- {
- super.onResume();
- Map<String,?> usersTasks= myData.getAll();
- for(Map.Entry<String,?> tasks:usersTasks.entrySet())
- {
- myTasks.add(tasks.getKey().toString());
- }
- MyAdapter adpt = new MyAdapter(this,myTasks);
- myList.setAdapter(adpt);
- }
- //add new task button
- public void btnTask(View v)
- {
- Intent newTask = new Intent(this,NewTask.class);
- this.startActivity(newTask);
- finish();
- }
- //logout button
- public void btnLogOut(View v)
- {
- Intent logOut = new Intent(this,MainActivity.class);
- editor.remove("userOn").commit();
- this.startActivity(logOut);
- finish();
- }
- }
- ====================================================================================================================================
- XML activity_addtask================================================================================================================
- <RelativeLayout
- android:id="@+id/conter"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <TextView
- android:id="@+id/wlcTask"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Add Your Task below"
- android:textSize="30sp"
- android:gravity="center"
- />
- <EditText
- android:id="@+id/txtTask"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="New task"
- android:gravity="center"
- android:textSize="25sp"
- android:layout_below="@+id/wlcTask"
- android:layout_marginTop="40sp"
- />
- <Button
- android:id="@+id/btnAdd"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/txtTask"
- android:layout_marginTop="20sp"
- android:text="Add Task"
- android:textSize="50sp"
- android:onClick="btnTask"
- />
- <Button
- android:id="@+id/btnRtn"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/btnAdd"
- android:layout_marginTop="20sp"
- android:text="Canlce"
- android:textSize="30sp"
- android:onClick="btnCancle"
- />
- </RelativeLayout>
- ====================================================================================================================================
- JAVA NewTask========================================================================================================================
- package com.example.igor.mytodolist;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- /**
- * Created by Igor on 3/19/2016.
- */
- public class NewTask extends Activity
- {
- //user task input
- private EditText usrTask;
- private SharedPreferences myPref;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_addtask);
- usrTask = (EditText)findViewById(R.id.txtTask);
- myPref = getApplicationContext().getSharedPreferences(Register.myClients, MODE_PRIVATE);
- }
- public void btnTask(View v)
- {
- //create a String variable to hold userOn info
- String dataBase = myPref.getString("userOn","offline");
- //create new XML file for for current user
- SharedPreferences userData = getApplicationContext().getSharedPreferences(dataBase, MODE_PRIVATE);
- SharedPreferences.Editor editor = userData.edit();
- //add task as key with value of 0 /*by defualt not done!*/
- editor.putInt(usrTask.getText().toString(),0).commit();
- //go back to the Logged page
- Intent added = new Intent(this,Logged.class);
- this.startActivity(added);//go to logged
- finish();
- }
- public void btnCancle(View v)
- {
- Intent cncl = new Intent(this,Logged.class);
- this.startActivity(cncl);//cancle adding task
- finish();
- }
- }
- ====================================================================================================================================
- JAVA MyAdapter======================================================================================================================
- package com.example.igor.mytodolist;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.graphics.Color;
- import android.view.Gravity;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.CheckBox;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.ArrayList;
- /**
- * Created by Igor on 3/19/2016.
- */
- public class MyAdapter extends BaseAdapter
- {
- Context context;
- SharedPreferences currUser;
- SharedPreferences.Editor editor;
- //setting up arraylist to /*filter*/ the data threw adapter
- ArrayList<String> tasks = new ArrayList<String>();
- public MyAdapter(Context context, ArrayList<String> tasks)
- {
- this.context=context;
- this.tasks = tasks;
- setPrefs();
- }
- //Method that Returns the current user that is using
- private void setPrefs()
- {
- SharedPreferences myPref= context.getApplicationContext().getSharedPreferences(Register.myClients, Context.MODE_PRIVATE);
- String user = myPref.getString("userOn", "offline");
- currUser = context.getApplicationContext().getSharedPreferences(user,Context.MODE_PRIVATE);
- editor = currUser.edit();
- }
- @Override
- public int getCount() {
- return tasks.size();
- }
- @Override
- public Object getItem(int position) {
- return tasks.get(position);
- }
- @Override
- public long getItemId(int position)
- {
- return position;
- }
- @Override
- public View getView(final int position, View convertView, ViewGroup parent)
- {
- //my linearlayout that will hold all the list in!
- LinearLayout myLay = new LinearLayout(context);
- myLay.setOrientation(LinearLayout.VERTICAL);
- //my text view that will appear
- final CheckBox checkBox=new CheckBox(context);
- final TextView textView = new TextView(context);
- textView.setText(tasks.get(position));//show the task
- textView.setTextSize(30);
- textView.setGravity(Gravity.CENTER_HORIZONTAL);
- textView.setWidth(myLay.getWidth() / 2);
- //setting up colors of the lists
- if(currUser.getInt(tasks.get(position),0) == 0)
- {
- checkBox.setChecked(false);
- textView.setTextColor(Color.RED);
- }
- else
- {
- checkBox.setChecked(true);
- textView.setTextColor(Color.BLUE);
- }
- //onclick on the task
- textView.setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View v)
- {
- if(currUser.getInt(tasks.get(position),0)==0)
- {
- editor.putInt(tasks.get(position), 1);
- editor.commit();
- textView.setTextColor(Color.BLUE);
- Toast.makeText(context,"Done",Toast.LENGTH_SHORT).show();
- }
- else
- {
- editor.putInt(tasks.get(position), 0);
- editor.commit();
- textView.setTextColor(Color.RED);
- Toast.makeText(context,"Undone",Toast.LENGTH_SHORT).show();
- }
- }
- });
- myLay.addView(textView);
- return myLay;
- }
- }
- =====================================================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement