Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //==========================================MainActivity
- package com.example.com.myshared;
- 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
- {
- EditText txtUser;
- EditText txtPass;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- txtUser=(EditText)findViewById(R.id.user);
- txtPass=(EditText)findViewById(R.id.pass);
- if (checkUser())
- {
- //if key found , start the second activity.
- SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- Intent iEntry = new Intent(this,Entry.class);
- iEntry.putExtra("putUser", pref.getString("userLogged", "error"));
- this.startActivity(iEntry);
- finish();
- }
- }
- private boolean checkUser()
- {
- //declaration of shared preferences
- SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- //getting value of key "user"
- String userName = pref.getString("userLogged","error");
- //return false if key "user" not exists
- return (!userName.equals("error"));
- }
- public void btnLogin(View v)
- {
- SharedPreferences pref = getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- String userName=txtUser.getText().toString().toLowerCase();
- String password= txtPass.getText().toString();
- String passDB=pref.getString(userName,"ErrorP");
- if(userName.equals("") || password.equals("")) {
- Toast.makeText(this, "Please type username and password", Toast.LENGTH_LONG).show();
- return;
- }
- else if(!pref.contains(userName)) {
- Toast.makeText(this, "Please register", Toast.LENGTH_LONG).show();
- return;
- }
- else if(password.equals(passDB)) {
- Toast.makeText(this, "Good to see you", Toast.LENGTH_SHORT).show();
- }
- else
- {
- Toast.makeText(this, "Wrong user name or password", Toast.LENGTH_LONG).show();
- return;
- }
- //declaration of editor to edit shared preferences
- SharedPreferences.Editor editor=pref.edit();
- //create key "user" with value of userName, if key exists, it will be overwritten
- editor.putString("userLogged",userName);
- //commit the changes
- editor.commit();
- Intent iEntry = new Intent(this,Entry.class);
- iEntry.putExtra("putUser", pref.getString("userLogged","error"));
- this.startActivity(iEntry);
- finish();
- }
- public void btnReg(View v)
- {
- Intent regIntent = new Intent(this, Register.class);
- this.startActivity(regIntent);
- }
- }
- //======================================REGISTER CLASS
- package com.example.com.myshared;
- 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 Register extends Activity
- {
- EditText txtUser, txtPass1, txtPass2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.register);
- setPointer();
- }
- public void setPointer()
- {
- txtUser=(EditText)findViewById(R.id.user);
- txtPass1=(EditText)findViewById(R.id.pass1);
- txtPass2=(EditText)findViewById(R.id.pass2);
- }
- public void btnRegister(View v)
- {
- SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref", MODE_PRIVATE);
- //declaration of editor to edit shared preferences
- SharedPreferences.Editor editor=pref.edit();
- //insert into string value from EditText
- String userName=txtUser.getText().toString().toLowerCase();
- String password1=txtPass1.getText().toString();
- String password2=txtPass2.getText().toString();
- if(userName.equals("")||password1.equals("")){
- Toast.makeText(this, "Please type username and password", Toast.LENGTH_LONG).show();
- return;
- }
- else if(pref.contains(userName)) {
- Toast.makeText(this, "The username exist, please type another username", Toast.LENGTH_LONG).show();
- return;
- }
- else if(!password1.equals(password2)){
- Toast.makeText(this, "The password must be equals", Toast.LENGTH_LONG).show();
- return;
- }
- //create key "user name" with value of your password
- editor.putString(userName, password1);
- //commit the changes
- editor.commit();
- Intent iLogin = new Intent(this, MainActivity.class);
- this.startActivity(iLogin);
- finish();
- }
- public void btnCancel(View v)
- {
- finish();
- }
- }
- //===========================================ENTRY CLASS
- package com.example.com.myshared;
- import android.app.Activity;
- import android.content.Context;
- 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.Map;
- public class Entry extends Activity {
- TextView txt;
- ListView myList;
- Context context = this;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.entry);
- txt=(TextView)findViewById(R.id.text);
- myList=(ListView)findViewById(R.id.myListV);
- String myUser = getIntent().getStringExtra("putUser");
- txt.setText("Hello " + myUser);
- SharedPreferences pref=getApplicationContext().getSharedPreferences(myUser, MODE_PRIVATE);
- String[] taskArr = new String[pref.getAll().size()];
- int cnt=0;
- Map<String, ?> allTask = pref.getAll();
- for (Map.Entry<String, ?> singleTask : allTask.entrySet())
- {
- taskArr[cnt] = singleTask.getKey();
- cnt++;
- }
- TaskAdapter myTask = new TaskAdapter(context,taskArr,myUser);
- myList.setAdapter(myTask);
- }
- public void btnLogout(View v)
- {
- //declaration of shared preferences
- SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- //declaration of editor
- SharedPreferences.Editor editor=pref.edit();
- //remove key user and pass
- editor.remove("userLogged");
- //commit changes
- editor.commit();
- //start another window - main window
- Intent iMain = new Intent(this,MainActivity.class);
- this.startActivity(iMain);
- finish();
- }
- public void btnAddTask(View view)
- {
- Intent iAddTask = new Intent(this, AddTask.class);
- iAddTask.putExtra("putUserTask", getIntent().getStringExtra("putUser"));
- this.startActivity(iAddTask);
- finish();
- }
- }
- //=========================================ADD TASK CLASS
- package com.example.com.myshared;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- public class AddTask extends Activity {
- EditText txtTask;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.add_task);
- txtTask = (EditText)findViewById(R.id.task_type);
- }
- public void btnConfirm(View view)
- {
- String myUser = getIntent().getStringExtra("putUserTask");
- SharedPreferences pref=getApplicationContext().getSharedPreferences(myUser, MODE_PRIVATE);
- SharedPreferences.Editor editor = pref.edit();
- String task = txtTask.getText().toString();
- editor.putString(task, "0");
- editor.commit();
- Intent iEntry = new Intent(this, Entry.class);
- iEntry.putExtra("putUser", myUser);
- this.startActivity(iEntry);
- finish();
- }
- public void btnCancelTask(View view)
- {
- SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- Intent iEntry = new Intent(this, Entry.class);
- iEntry.putExtra("putUser", pref.getString("userLogged", "error"));
- this.startActivity(iEntry);
- finish();
- }
- }
- //============================================TASK ADAPTER CLASS
- package com.example.com.myshared;
- 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.LinearLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- public class TaskAdapter extends BaseAdapter
- {
- Context context;
- String myTask[];
- String myUser;
- public TaskAdapter(Context context,String myTask[], String myUser)
- {
- this.context=context;
- this.myTask=myTask;
- this.myUser=myUser;
- }
- @Override
- public int getCount() {
- return myTask.length;
- }
- @Override
- public Object getItem(int position) {
- return myTask[position];
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(final int position, View convertView, ViewGroup parent)
- {
- final SharedPreferences pref=context.getApplicationContext().getSharedPreferences(myUser, context.MODE_PRIVATE);
- final SharedPreferences.Editor editor = pref.edit();
- LinearLayout myLayout = new LinearLayout(context);
- myLayout.setOrientation(LinearLayout.VERTICAL);
- final TextView task = new TextView(context);
- task.setText(myTask[position]);
- task.setTextSize(32);
- task.setGravity(Gravity.CENTER_HORIZONTAL);
- task.setWidth(myLayout.getWidth() / 2);
- if(pref.getString(myTask[position], "error").equals("1"))
- task.setBackgroundColor(Color.GREEN);
- else
- task.setBackgroundColor(Color.RED);
- task.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if(pref.getString(myTask[position], "error").equals("0")) {
- editor.putString(myTask[position], "1");
- editor.commit();
- task.setBackgroundColor(Color.GREEN);
- Toast.makeText(context, "The mission was successful ", Toast.LENGTH_SHORT).show();
- }
- else
- {
- editor.putString(myTask[position], "0");
- editor.commit();
- task.setBackgroundColor(Color.RED);
- Toast.makeText(context, "The mission not successful ", Toast.LENGTH_SHORT).show();
- }
- }
- });
- myLayout.addView(task);
- return myLayout;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement