Firebase Authentication

To make a login and sign up app using firebase authentication(Email and Password). At first connect to your firebase and set the given gradles.

LogIn interface:


SignUp interface:



Here is the build.gradle you have to set-
----------------------------------------------------------------------------------------------------------
apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.rmproduct.cselibrary"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0-alpha1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-auth:11.6.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

apply plugin: 'com.google.gms.google-services'
-------------------------------------------------------------------------------------------------------------

AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET"/>
------------------------------------------------------------------------------------------------------------

Main Activity.java

package com.rmproduct.cselibrary;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      }
}
-----------------------------------------------------------------------------------------------------

LogIn.java

package com.rmproduct.cselibrary;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class LogIn extends AppCompatActivity {

    private EditText email, password;
    private Button login, signUp;
    FirebaseAuth firebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);

        email=(EditText) findViewById(R.id.email2);
        password=(EditText) findViewById(R.id.password2);
        login=(Button) findViewById(R.id.logIn);
        signUp=(Button) findViewById(R.id.signUp);

        firebaseAuth= FirebaseAuth.getInstance();
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String txt_email= email.getText().toString().trim();
                String txt_password= password.getText().toString().trim();

                if (email.length()==0) {
                    email.setError("You must set a valid email");
                    return;
                }
                if (password.length()==0) {
                    password.setError("You must set correct password");
                    return;
                }

                firebaseAuth.signInWithEmailAndPassword(txt_email, txt_password)
                        .addOnCompleteListener(LogIn.this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (task.isSuccessful()) {
                                    startActivity(new Intent(getApplicationContext(), MainActivity.class));
                                    Toast.makeText(LogIn.this, "Login Success", Toast.LENGTH_SHORT).show();
                                }
                                else {
                                    Toast.makeText(LogIn.this, "Login Failed", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
            }
        });

        signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LogIn.this, SignUp.class);
                startActivity(intent);
            }
        });
    }
}
---------------------------------------------------------------------------------------------------------

SignUp.java

package com.rmproduct.cselibrary;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class SignUp extends Activity {

    private EditText email, password, password2;
    private Button signUp, logIn;
    private FirebaseAuth firebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);

        email=(EditText) findViewById(R.id.email2);
        password=(EditText) findViewById(R.id.password2);
        password2=(EditText) findViewById(R.id.password4);
        signUp=(Button) findViewById(R.id.signUp);
        logIn=(Button) findViewById(R.id.logIn);

        firebaseAuth = FirebaseAuth.getInstance();
        signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String txt_email= email.getText().toString().trim();
                String txt_password= password.getText().toString().trim();
                String txt_password2= password2.getText().toString().trim();

                if (email.length()==0) {
                    email.setError("You must set a valid email");
                    return;
                }
                if (password.length()==0) {
                    password.setError("You must set password");
                    return;
                }
                if (password2.length() == 0) {
                    password2.setError("You have to set same password");
                    return;
                }
                if (password.length()<6) {
                    password.setError("Your password must be at least 6 character");
                }

                if (txt_password.equals(txt_password2)) {

                    firebaseAuth.createUserWithEmailAndPassword(txt_email, txt_password)
                            .addOnCompleteListener(SignUp.this, new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    if (task.isSuccessful()) {
                                        startActivity(new Intent(getApplicationContext(), LogIn.class));
                                        Toast.makeText(SignUp.this, "Registration Comoplete", Toast.LENGTH_SHORT).show();
                                    }
                                    else {
                                        Toast.makeText(SignUp.this, "Registration Failed", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                }
            }
        });

        logIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SignUp.this, LogIn.class);
                startActivity(intent);
            }
        });
    }
}
-------------------------------------------------------------------------------------------------------------

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relative"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:padding="5dp"
    android:background="@color/white"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/welcome"
        android:text="Welcome to CSE Library mobile app"
        android:textSize="15sp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
----------------------------------------------------------------------------------------------------------------

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relative"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:padding="5dp"
    android:background="@color/white"
    tools:context=".LogIn">

    <ImageView
        android:id="@+id/image_logo"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:contentDescription="CSE Library Logo"
        android:src="@drawable/library_logo" />

    <TextView
        android:id="@+id/welcome"
        android:text="Welcome to CSE Library mobile app"
        android:textSize="15sp"
        android:textColor="@color/black"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInput"
        android:layout_below="@+id/image_logo"
        android:layout_marginTop="5dp"
        app:passwordToggleEnabled="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:textColorHint="@color/green"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foregroundTint="@color/black"
        >

        <EditText
            android:id="@+id/email2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:ems="10"
            android:hint="Email"
            android:textIsSelectable="true"
            android:textEditSidePasteWindowLayout="@color/black"
            android:inputType="textEmailAddress"
            android:textColorHint="@color/green"
            android:textSize="20sp" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInput2"
        android:layout_below="@+id/image_logo"
        android:layout_marginTop="5dp"
        app:passwordToggleEnabled="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:textColorHint="@color/green"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/password2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Password"
            android:textColorHint="@color/green"
            android:inputType="textPassword"
            android:textSize="20sp"
            android:backgroundTint="@color/black"
            />


    </android.support.design.widget.TextInputLayout>

    <LinearLayout
        android:id="@+id/linear3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textInput"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp">


        <Button
            android:id="@+id/logIn"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:background="@color/green"
            android:text="LogIn"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:textStyle="bold" />

        <LinearLayout
            android:id="@+id/linearIn"
            android:gravity="center"
            android:layout_marginTop="30dp"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Don't have any account?"
                android:layout_marginRight="5dp"
                android:textColor="@color/black"
                />

            <Button
                android:id="@+id/signUp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:text="Sign Up"
                android:textColor="@color/green"
                android:textSize="20sp" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>
-------------------------------------------------------------------------------------------------------------

activity_sign_up.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relative"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:padding="5dp"
    android:background="@color/white"
    tools:context=".SignUp">

    <ImageView
        android:id="@+id/image_logo"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:contentDescription="CSE Library Logo"
        android:src="@drawable/library_logo" />

    <TextView
        android:id="@+id/welcome"
        android:text="Welcome to CSE Library mobile app"
        android:textSize="15sp"
        android:textColor="@color/black"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInput"
        android:layout_below="@+id/image_logo"
        android:layout_marginTop="5dp"
        app:passwordToggleEnabled="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:textColorHint="@color/green"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foregroundTint="@color/black"
        >

        <EditText
            android:id="@+id/email2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Email"
            android:textIsSelectable="true"
            android:textEditSidePasteWindowLayout="@color/black"
            android:inputType="textEmailAddress"
            android:textColorHint="@color/green"
            android:textSize="20sp" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInput2"
        android:layout_below="@+id/textInput"
        android:layout_marginTop="5dp"
        app:passwordToggleEnabled="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:textColorHint="@color/green"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/password2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Password"
            android:minEms="6"
            android:textColorHint="@color/green"
            android:inputType="textPassword"
            android:textSize="20sp"
            android:backgroundTint="@color/black"
            />


    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInput3"
        android:layout_below="@+id/textInput2"
        android:layout_marginTop="5dp"
        app:passwordToggleEnabled="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:textColorHint="@color/green"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/password4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Password"
            android:minEms="6"
            android:textColorHint="@color/green"
            android:inputType="textPassword"
            android:textSize="20sp"
            android:backgroundTint="@color/black"
            />


    </android.support.design.widget.TextInputLayout>

    <LinearLayout
        android:id="@+id/linear3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textInput"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp">


        <Button
            android:id="@+id/signUp"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:background="@color/green"
            android:text="Sign Up"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:textStyle="bold" />

        <LinearLayout
            android:id="@+id/linearIn"
            android:gravity="center"
            android:layout_marginTop="30dp"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Have any account?"
                android:layout_marginRight="5dp"
                android:textColor="@color/black"
                />

            <Button
                android:id="@+id/logIn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:text="LogIn"
                android:textColor="@color/green"
                android:textSize="20sp" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>
--------------------------------------------------------------------------------------------------------------------

There are some color and drawable which are included in the color.xml and drawable directory. Please ensure those type of document otherwise your compiler will give error..

You also can collect this app source code from github.com to get follow the link below-

https://github.com/rmmostak/CSELibrary

Comments

Popular posts from this blog

Mobile Note Android App