Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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"
- android:layout_margin="16dp">
- <!-- place for logo -->
- <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="MY TASK"
- android:textSize="64sp"/>
- </LinearLayout>
- <!-- place for user input -->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="vertical"
- android:gravity="center">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="@string/userName"
- android:id="@+id/txtUser"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="@string/userPass"
- android:inputType="textPassword"
- android:layout_marginTop="20dp"
- android:id="@+id/txtPass"/>
- </LinearLayout>
- <!-- place for buttons-->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="horizontal"
- android:gravity="top">
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/register"
- android:textColor="#ffffff"
- android:background="#009fff"
- android:textSize="22sp"
- android:layout_weight="1"
- android:layout_margin="20dp"
- android:id="@+id/btnRegister"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/login"
- android:textColor="#ffffff"
- android:background="#009fff"
- android:textSize="22sp"
- android:layout_weight="1"
- android:layout_margin="20dp"
- android:id="@+id/btnLogin"/>
- </LinearLayout>
- </LinearLayout>
- activity_register.xml
- =======================
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_margin="16dp">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="2"
- android:orientation="vertical"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:text="LOGO"
- android:textSize="62sp"
- android:layout_gravity="center"
- />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="vertical"
- android:layout_margin="30dp">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/regUserName"
- android:hint="Enter a user name..."
- android:layout_weight="1"
- android:gravity="bottom"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/regPass1"
- android:hint="Enter your password...."
- android:layout_weight="1"
- android:gravity="bottom"
- android:inputType="textPassword"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/regPass2"
- android:hint="Re-Enter your password..."
- android:layout_weight="1"
- android:gravity="bottom"
- android:inputType="textPassword"
- />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="2"
- android:orientation="horizontal">
- <Button
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="@string/register"
- android:textColor="#ffffff"
- android:background="#009fff"
- android:textSize="32sp"
- android:layout_weight="1"
- android:layout_margin="10dp"
- android:id="@+id/btnRegRegister"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="cancel"
- android:textColor="#ffffff"
- android:background="#009fff"
- android:textSize="32sp"
- android:layout_weight="1"
- android:layout_margin="10dp"
- android:id="@+id/btnRegCancel"/>
- </LinearLayout>
- </LinearLayout>
- android manifest (example only)
- ======================================
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.teacher.mytasks">
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".RegisterActivity"/>
- </application>
- </manifest>
- RegisterActivity.java
- =========================
- import android.content.Context;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- /**
- * Created by teacher on 12/21/2017.
- */
- public class RegisterActivity extends AppCompatActivity {
- Context context;
- EditText regUser,regPass1,regPass2;
- Button btnRegister,btnCancel;
- UtlUserSP userUtil=new UtlUserSP();
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- setPointer();
- }
- private void setPointer() {
- this.context=this;
- userUtil.setContext(context);
- regUser=findViewById(R.id.regUserName);
- regPass1=findViewById(R.id.regPass1);
- regPass2=findViewById(R.id.regPass2);
- btnCancel=findViewById(R.id.btnRegCancel);
- btnCancel.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- finish();
- }
- });
- btnRegister =findViewById(R.id.btnRegRegister);
- btnRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- //check if password o.k.
- if (!regPass1.getText().toString().equals(regPass2.getText().toString()))
- {
- Toast.makeText(context, "Password not match", Toast.LENGTH_SHORT).show();
- return;
- }
- if (regPass1.getText().toString().length()<3)
- {
- Toast.makeText(context, "Password must be 3 letter minimum", Toast.LENGTH_SHORT).show();
- return;
- }
- if(!userUtil.registerUser(regUser.getText().toString(),regPass1.getText().toString()))
- {
- Toast.makeText(context, "Error in user registration....", Toast.LENGTH_SHORT).show();
- return;
- }
- Toast.makeText(context, "new user is add...", Toast.LENGTH_SHORT).show();
- finish();
- }
- });
- }
- }
- MainActivity.java
- ======================
- import android.content.Context;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- public class MainActivity extends AppCompatActivity {
- EditText txtUser, txtPass;
- Button btnLogin, btnRegister;
- Context context;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- }
- private void setPointer() {
- this.context = this;
- txtUser = findViewById(R.id.txtUser);
- txtPass = findViewById(R.id.txtPass);
- btnLogin = findViewById(R.id.btnLogin);
- btnRegister = findViewById(R.id.btnRegister);
- btnRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent myIntent=new Intent(context,RegisterActivity.class);
- startActivity(myIntent);
- }
- });
- }
- }
- UtlUserSp.java
- =====================
- package com.example.teacher.mytasks;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.widget.Toast;
- /**
- * Created by teacher on 12/21/2017.
- */
- public class UtlUserSP implements UserAble {
- Context context;
- public UtlUserSP() { }
- @Override
- public void setContext(Context context) {
- this.context=context;
- }
- @Override
- public boolean registerUser(String userName, String userPass) {
- //User Exists
- if (userExists(userName)) {
- Toast.makeText(context, "user already exists...", Toast.LENGTH_SHORT).show();
- return false;
- }
- //save user and password
- //declaration of shared preference
- SharedPreferences prefs = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
- //declaration of shared preference 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 preferences file.
- editor.commit();
- return true;
- }
- @Override
- public boolean userExists( String userName) {
- //declaration of shared prefernces
- SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
- //get user pass
- String spPass=prefs.getString(userName,"na");
- //return if users exists, if not (na) return false
- return !spPass.equals("na");
- }
- @Override
- public boolean checkUser( String userName, String userPass) {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement