Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- JAVA
- ======
- MainActivity.java
- ----------------
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener{
- EditText lUser,lPass;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- }
- private void setPointer() {
- lUser=(EditText)findViewById(R.id.userLogin);
- lPass=(EditText)findViewById(R.id.userPass);
- }
- @Override
- public void onClick(View view) {
- switch (view.getId()){
- case R.id.btnLogin:
- // Key Value
- if (!SharedPref.checkPass(lUser.getText().toString(),lPass.getText().toString(),this))
- {
- Toast.makeText(this, "Invalid user name or pass", Toast.LENGTH_LONG).show();
- return;
- }
- Intent intent = new Intent(this, Tasks.class);
- intent.putExtra("userName",lUser.getText().toString());
- startActivity(intent);
- break;
- case R.id.btnRegister:
- startActivity(new Intent(this,Register.class));
- break;
- default:
- Toast.makeText(this, "Error in button", Toast.LENGTH_SHORT).show();
- }
- }
- private void checkLogin() {
- }
- }
- Register.java
- ---------------
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class Register extends AppCompatActivity implements View.OnClickListener {
- EditText rUser, rPass, rCheck;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- setPointer();
- }
- private void setPointer() {
- rUser = (EditText) findViewById(R.id.rName);
- rPass = (EditText) findViewById(R.id.rpass);
- rCheck = (EditText) findViewById(R.id.rCheck);
- }
- @Override
- public void onClick(View view) {
- switch (view.getId()) {
- case R.id.rCancel:
- finish();
- break;
- case R.id.rRegister:
- registerUser();
- break;
- default:
- Toast.makeText(this, "Error in buttons", Toast.LENGTH_SHORT).show();
- }
- }
- private void registerUser() {
- //check if all fields filled
- if (rUser.getText().toString().isEmpty() ||
- rPass.getText().toString().isEmpty() ||
- rCheck.getText().toString().isEmpty())
- {
- Toast.makeText(this, "Please fill all fields", Toast.LENGTH_LONG).show();
- return;
- }
- //check if minimum password length > 2
- if (rPass.getText().toString().length()<3)
- {
- Toast.makeText(this, "Password is too short (3 minimum)", Toast.LENGTH_SHORT).show();
- return;
- }
- //check if password match
- if (!rPass.getText().toString().equals(rCheck.getText().toString()))
- {
- Toast.makeText(this, "Password not match", Toast.LENGTH_LONG).show();
- return;
- }
- //check if user exists
- if (SharedPref.checkUserExists(rUser.getText().toString(),this))
- {
- Toast.makeText(this, "User already exists", Toast.LENGTH_LONG).show();
- return;
- }
- //register user and finitu la comedia
- SharedPref.registerUser(rUser.getText().toString(),rPass.getText().toString(),this);
- finish();
- }
- }
- SharedPref.java
- -----------------
- import android.content.Context;
- import android.content.SharedPreferences;
- /**
- * Created by teacher on 9/25/2017.
- */
- public class SharedPref {
- //for login screen
- public static boolean checkPass(String userName,String userPass, Context context)
- {
- //declaration of shared preferences
- SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
- //get user pass
- String spPass=prefs.getString(userName,"na");
- //check if password matches
- if (userPass.equals(spPass))
- {
- return true;
- }
- return false;
- }
- public static boolean checkUserExists(String userName, Context context)
- {
- //declaration of shared preferences
- SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
- //get user pass
- String spPass=prefs.getString(userName,"na");
- //check if password matches
- return !spPass.equals("na");
- }
- public static void registerUser(String userName, String userPass, Context context)
- {
- //declaration of shared preferences
- SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
- //declaration of shared preferences Editor
- SharedPreferences.Editor editor = prefs.edit();
- //put data inside, we use hash map , so we need KEY,VALUE (K,V)
- editor.putString(userName,userPass);
- //sending data to my shared prefernces file, we have to commit the changes
- editor.commit();
- }
- }
- tasks.java
- ------------
- import android.content.Context;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.w3c.dom.Text;
- public class Tasks extends AppCompatActivity {
- TextView userName;
- Context context;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_tasks);
- setPointer();
- //get the data from previous activity.
- Bundle extras = getIntent().getExtras();
- if (extras==null)
- {
- Toast.makeText(context, "Error in getting info", Toast.LENGTH_SHORT).show();
- }
- else
- {
- String uName=extras.getString("userName");
- userName.setText("Hello "+uName);
- }
- }
- private void setPointer() {
- this.context=this;
- userName=(TextView)findViewById(R.id.userName);
- }
- }
- XML
- 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="match_parent"
- android:orientation="vertical"
- android:layout_weight="1">
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:src="@drawable/task"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="vertical">
- <EditText
- android:id="@+id/userLogin"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="20dp"
- android:hint="Enter user name..." />
- <EditText
- android:id="@+id/userPass"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="20dp"
- android:hint="Enter user pass..."
- android:inputType="textPassword" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="horizontal">
- <Button
- android:id="@+id/btnLogin"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="20dp"
- android:layout_weight="1"
- android:background="#009fff"
- android:onClick="onClick"
- android:text="Login"
- android:textColor="#ffffff"
- android:textSize="22sp"
- />
- <Button
- android:id="@+id/btnRegister"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="20dp"
- android:layout_weight="1"
- android:background="#009fff"
- android:onClick="onClick"
- android:text="Register"
- android:textColor="#ffffff"
- android:textSize="22sp" />
- </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="wrap_content"
- android:textSize="18dp"
- android:text="in memory of Tomer, which didn't bring chocolate to the class, shame you!!!"
- android:gravity="center"/>
- </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">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="vertical"
- android:gravity="center">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="REGISTER"
- android:layout_gravity="center"
- android:textSize="32sp"/>
- </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="wrap_content"
- android:hint="Ener user name..."
- android:id="@+id/rName"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Enter password..."
- android:id="@+id/rpass"
- android:inputType="textPassword"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Retype password..."
- android:id="@+id/rCheck"
- android:inputType="textPassword"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="horizontal">
- <Button
- android:id="@+id/rCancel"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="20dp"
- android:layout_weight="1"
- android:background="#009fff"
- android:onClick="onClick"
- android:text="Cancel"
- android:textColor="#ffffff"
- android:textSize="22sp"
- />
- <Button
- android:id="@+id/rRegister"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="20dp"
- android:layout_weight="1"
- android:background="#009fff"
- android:onClick="onClick"
- android:text="Register"
- android:textColor="#ffffff"
- android:textSize="22sp" />
- </LinearLayout>
- </LinearLayout>
- activity_task.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:text="Kill Eyal"
- android:gravity="center"
- android:layout_marginTop="10dp"
- android:textSize="30sp"
- android:id="@+id/userName"/>
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement