Today Most of the apps need to know the identity of the user. but it’s difficult without having your own server and save data securely.Firebase authentication provides the backend service to authenticate and save the user data. Firebase authentication support OAuth2.0 and OpenID connect to easily integrate with your app.

Output:

Download APK – [media-downloader media_id=”1296″ texts=”here”]

Download project – here

Wants to Learn Android Application development using Firebase from scratch

let’s follow these steps to complete the setup.

  1. Create a project in android studio name it “FirebaseAuth”. choose the empty activity then click next. Activity name will be “Signin” and Layout name will be “signin”. click finish.
  2. Now open settings in the android studio(file>Settings). A popup will open then click on “plugin”. In search bar type “firebase”.  Enable”Firebase App Indexing”,”Firebase Services”,”Firebase Testing”.

3. Click on Tools from the menu bar then click “firebase” as shown in the below image. Now a drawer will open on the right side click on “Authentication”.

4. Now click on “email and password authentication”. now click on “connect to firebase” button. it will ask you to authenticate using your Gmail account.  Now click on “Add Firebase Authentication to your app”. By this, all required dependencies will automatically be created in your app. you can do it manually if you are facing any problem in configuration.

5. download [media-downloader media_id=”1285″ texts=”drawable”] folder and add it in your application.

6. Open signin.xml and add the following line of code. which will be the launcher screen.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradiant"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:paddingLeft="60dp"
        android:paddingRight="60dp">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="16dp">


                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:src="@drawable/layeru"
                    android:layout_gravity="center"/>

                <EditText
                    android:id="@+id/email"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="email"
                    android:textColorHint="#d9e5f3"
                    android:layout_marginLeft="15dp"
                    android:background="#00000000"
                    android:letterSpacing="0.1"
                    android:textSize="16dp"
                    android:inputType="text"
                    android:textColor="#fff"
                    />

            </LinearLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#4a5a71"></LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="16dp">


                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:src="@drawable/layer0"
                    android:layout_gravity="center"/>


                <EditText
                    android:id="@+id/password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Password"
                    android:textColorHint="#d9e5f3"
                    android:layout_marginLeft="15dp"
                    android:background="#00000000"
                    android:inputType="textPassword"
                    android:letterSpacing="0.1"
                    android:textSize="16dp"
                    android:textColor="#fff"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#4a5a71"></LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="60dp"
        android:paddingRight="60dp"
        android:orientation="vertical"
        android:gravity="bottom"
        android:layout_marginBottom="20dp"
        >
        <TextView
            android:id="@+id/login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#009a9a"
            android:text="Login"
            android:textColor="#d9e5f3"
            android:textStyle="bold"
            android:gravity="center"
            android:layout_gravity="bottom"
            android:padding="16dp"
            android:layout_marginBottom="80dp"
            android:letterSpacing="0.2"/>

        <TextView
            android:id="@+id/signup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Create Account ? Signup"
            android:textColor="#d9e5f3"
            android:letterSpacing="0.1"/>


    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_marginBottom="400dp">

    </LinearLayout>

</RelativeLayout>

7. Create another Layout file name it “signup.xml”. add the following lines of code. this will be the Signup screen.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradiant"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:paddingLeft="60dp"
        android:paddingRight="60dp">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="16dp">


                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:src="@drawable/layerem"
                    android:layout_gravity="center"/>

                <EditText
                    android:id="@+id/email"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Email Address"
                    android:textColorHint="#d9e5f3"
                    android:layout_marginLeft="15dp"
                    android:background="#00000000"
                    android:letterSpacing="0.1"
                    android:textSize="16dp"
                    android:inputType="text"
                    android:textColor="#fff"
                    />

            </LinearLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#4a5a71"></LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="16dp">


                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:src="@drawable/layeru"
                    android:layout_gravity="center"/>

                <EditText
                    android:id="@+id/user"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Username"
                    android:textColorHint="#d9e5f3"
                    android:layout_marginLeft="15dp"
                    android:background="#00000000"
                    android:letterSpacing="0.1"
                    android:textSize="16dp"
                    android:inputType="text"
                    android:textColor="#fff"
                    />

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#4a5a71"></LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="16dp">


                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:src="@drawable/layer0"
                    android:layout_gravity="center"/>


                <EditText
                    android:id="@+id/password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Password"
                    android:textColorHint="#d9e5f3"
                    android:layout_marginLeft="15dp"
                    android:background="#00000000"
                    android:inputType="textPassword"
                    android:letterSpacing="0.1"
                    android:textSize="16dp"
                    android:textColor="#fff"
                    />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#4a5a71"></LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="16dp">


                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:src="@drawable/mob"
                    android:layout_gravity="center"/>

                <EditText
                    android:id="@+id/mob"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Mobile Number"
                    android:textColorHint="#d9e5f3"
                    android:layout_marginLeft="15dp"
                    android:background="#00000000"
                    android:letterSpacing="0.1"
                    android:textSize="16dp"
                    android:inputType="number"
                    android:textColor="#fff"
                    />

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#4a5a71"></LinearLayout>

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="60dp"
        android:paddingRight="60dp"
        android:orientation="vertical"
        android:gravity="bottom"
        android:layout_marginBottom="20dp"
        >
        <TextView
            android:id="@+id/signup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#009a9a"
            android:text="Signup"
            android:textColor="#d9e5f3"
            android:textStyle="bold"
            android:gravity="center"
            android:layout_gravity="bottom"
            android:padding="16dp"
            android:layout_marginBottom="50dp"
            android:letterSpacing="0.2"/>

        <TextView
            android:id="@+id/signinsv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Already have an Account ?"
            android:textColor="#d9e5f3"
            android:letterSpacing="0.1"/>


    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_marginBottom="400dp">

    </LinearLayout>



</RelativeLayout>

Related:

Creating Authentication App | Java Database Connectivity using Netbeans

Firebase Authentication using facebook login in android

Login using twitter in android application- tutorial

Firebase Authentication using google login in android

Signin/Signup session management in Android using PHP/MySQL/Shared preferences – Volley Library

Signin/Signup using android Skygear SDK

8. Now Create one more Layout file name it “show.xml”. which will show the user details after Signed in or Sign up.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradiant"
    android:orientation="vertical">

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="123dp"
        android:textColor="#ffffff"
        android:id="@+id/email"
        android:textSize="25dp" />

    <TextView
        android:text="Welcome"
        android:textSize="35dp"
        android:textColor="#ffffff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/email"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="63dp"
        android:id="@+id/welcome" />
</RelativeLayout>

9. In Signin.java we have created a method named signed in which takes two parameters i.e email and password. then signs a user in with the method signInWithEmailAndPassword.

package com.mytrendin.firebaseauth;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
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;

/**
 * Created by csa on 3/27/2017.
 */

public class Signin extends Activity {

    private FirebaseAuth mAuth;

    TextView signin,signup;
    EditText email ,password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signin);
        signin = (TextView) findViewById(R.id.login);
        signup = (TextView) findViewById(R.id.signup);
        email = (EditText) findViewById(R.id.email);
        password = (EditText) findViewById(R.id.password);
        mAuth = FirebaseAuth.getInstance();

        signup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Signin.this,Signup.class);
                startActivity(intent);

            }
        });


        signin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String em = email.getText().toString();
                String pas= password.getText().toString();
               // signin(em,pas);

                if(em.isEmpty() || pas.isEmpty()){

                    Toast.makeText(Signin.this, "fill up details", Toast.LENGTH_SHORT).show();

                }else {

                    signedin(em,pas);
                }


            }
        });
    }

    public void signedin(final String email, String password){


        mAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d("hi", "signInWithEmail:onComplete:" + task.isSuccessful());


                        if (task.isSuccessful()) {
                            Toast.makeText(Signin.this, "Signed in."+email,
                                    Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(Signin.this,Show.class);
                            intent.putExtra("email",email);
                            intent.putExtra("message","welcome");
                            startActivity(intent);


                        }else {

                            Toast.makeText(Signin.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();

                        }

                    }
                });

    }

}

10. Create another java file name it “Signup.java”. In Signup.java we have created a method named signup which takes two parameters i.e email and password. then signup a user with the method createUserWithEmailAndPassword .

package com.mytrendin.firebaseauth;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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;
import com.google.firebase.auth.FirebaseUser;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by csa on 3/27/2017.
 */

public class Signup extends Activity {

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;
    TextView singup,signin;
    EditText email ,password;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);
        singup = (TextView) findViewById(R.id.signup);
        signin = (TextView) findViewById(R.id.signinsv);
        email = (EditText) findViewById(R.id.email);
        password = (EditText) findViewById(R.id.password);

        signin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Signup.this,Signin.class);
                startActivity(intent);
            }
        });


        mAuth = FirebaseAuth.getInstance();

        mAuthListener = new FirebaseAuth.AuthStateListener(){


            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null){

                    Toast.makeText(Signup.this, ""+user.getDisplayName(), Toast.LENGTH_SHORT).show();

                }else{

                    Toast.makeText(Signup.this,"Sorry",Toast.LENGTH_LONG).show();

                }

            }
        };

        singup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mail = email.getText().toString();
                String pass = password.getText().toString();

                if(mail.isEmpty() || pass.isEmpty()){

                    Toast.makeText(Signup.this, "fill up details", Toast.LENGTH_SHORT).show();

                }else {

                    signupuser(mail,pass);
                }


            }
        });




    }

    @Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(mAuthListener !=null){
            mAuth.removeAuthStateListener(mAuthListener);
        }
    }


    public void signupuser(String email, String password){

        mAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d("hitesh", "createUserWithEmail:onComplete:" + task.isSuccessful());
                        if (task.isSuccessful()){

                            String nam = getdata();
                            Intent intent = new Intent(Signup.this,Show.class);
                            intent.putExtra("email",nam);
                            intent.putExtra("message","Account Created");
                            startActivity(intent);
                            finish();

                          //  Toast.makeText(Signup.this, ""+nam, Toast.LENGTH_SHORT).show();
                        }


                        else{
                            Toast.makeText(Signup.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }


                    }
                });




    }

    public String getdata(){
        String name = null;

        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        if (user != null) {
            // Name, email address, and profile photo Url
             name = user.getEmail();
          //  String email = user.getEmail();
            Uri photoUrl = user.getPhotoUrl();

            // The user's ID, unique to the Firebase project. Do NOT use this value to
            // authenticate with your backend server, if you have one. Use
            // FirebaseUser.getToken() instead.
            String uid = user.getUid();
        }

        return name;

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

    }
}

11. Create another java file name it “Show.java”. which is responsible for showing the user details after Signed in or Sign up.

package com.mytrendin.firebaseauth;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

/**
 * Created by csa on 3/27/2017.
 */

public class Show extends Activity {

    TextView email,welcome;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show);
        welcome = (TextView) findViewById(R.id.welcome);
        email= (TextView) findViewById(R.id.email);
        Intent intent= getIntent();
        String mail = intent.getStringExtra("email");
        String message = intent.getStringExtra("message");

        email.setText(mail);
        welcome.setText(message);

    }
}

12. Don’t forget to enable Email/password sign in provider as shown in the below in the image. AndroidManifest.xml file will remain same.

13. You can check the added user in Firebase Authentication Dashboard.

 

If You are Beginner Learn Android application development from the scratch


If you really liked the article, please subscribe to our YouTube Channel for videos related to this article.Please find us on Twitter and Facebook.

If you really liked the article, please subscribe to our YouTube Channel for videos related to this article.Please find us on Twitter and Facebook.

Related Posts

Categories: