Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaImplement Splash Screen and Authentication in Social Media Android App

Implement Splash Screen and Authentication in Social Media Android App

This is the Part 1 of “Build a Social Media App on Android Studio” tutorial, and we are going to cover the following functionalities in this article:

  • Creating a Splash Screen
  • Authentication Part:
    • Registration, and
    • Login

Step By Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Connect your app to firebase 

Go to the Tools > Firebase > Authentication and connect your app to firebase and also add the firebase authentication SDK to your app like the following image.

Then go to the Firebase Console > Authentication > Sign-in method and Enable the Email/Password.

Step 3: Create 4 new empty activities

Go to the package name > right-click > New > Activity > Empty Activity and name the activity as SplashScreen, RegistrationActivity, LoginActivity, and DashboardActivity. You may also refer to this article How to create new empty activity in android studio.

Step 4: Working with the SplashScreen Activity

Navigate to the app > res > layout > activity_splash_screen.xml and add the below code to that file. Below is the code for the activity_splash_screen.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_color"
 
    tools:context=".SplashScreen">
 
</androidx.constraintlayout.widget.ConstraintLayout>


Working with the SplashScreen.java file. Here we are checking that if the user is null then go to LoginActivity. Else move to DashboardActivity. 

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                FirebaseUser user=mAuth.getCurrentUser();
                if(user==null){
                    Intent intent = new Intent(SplashScreen.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
                else {
                    Intent mainIntent= new Intent(SplashScreen.this, DashboardActivity.class);
                    mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(mainIntent);
                    finish();
                }
            }
      },1000);

Below is the code for the SplashScreen.java file.

Java




package com.example.socialmediaapp;
 
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
 
import androidx.appcompat.app.AppCompatActivity;
 
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
 
public class SplashScreen extends AppCompatActivity {
 
    FirebaseUser currentUser;
    private FirebaseAuth mAuth;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
        mAuth = FirebaseAuth.getInstance();
        if (mAuth != null) {
            currentUser = mAuth.getCurrentUser();
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                FirebaseUser user = mAuth.getCurrentUser();
                if (user == null) {
                    Intent intent = new Intent(SplashScreen.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Intent mainIntent = new Intent(SplashScreen.this, DashboardActivity.class);
                    mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(mainIntent);
                    finish();
                }
            }
        }, 1000);
    }
}


Step 5: Working with the RegistrationActivity Activity

Navigate to the app > res > layout > activity_registration.xml and add the below code to that file. Below is the code for the activity_registration.xml file.  Here we have created three EditText for the name, email and password and one Button to Register user.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RegistrationActivity">
 
    <TextView
        android:id="@+id/teacher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:text="Sign Up Here"
        android:textColor="@color/colorBlack"
        android:textSize="28sp"
        android:textStyle="bold|italic" />
 
    <TextView
        android:id="@+id/names"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/teacher"
        android:layout_marginStart="35dp"
        android:layout_marginTop="120dp"
        android:text="Name"
        android:textColor="@color/colorBlack" />
 
    <EditText
        android:id="@+id/register_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/names"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="30dp"
        android:background="@drawable/edit"
        android:drawableStart="@drawable/name"
        android:hint="Your Name..."
        android:inputType="text"
        android:padding="8dp" />
 
    <TextView
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/register_name"
        android:layout_marginStart="35dp"
        android:layout_marginTop="10dp"
        android:text="Email"
        android:textColor="@color/colorBlack" />
 
    <EditText
        android:id="@+id/register_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/email"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="30dp"
        android:background="@drawable/edit"
        android:drawableStart="@drawable/ic_email"
        android:hint="Email..."
        android:inputType="textEmailAddress"
        android:padding="8dp" />
 
    <TextView
        android:id="@+id/pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/register_email"
        android:layout_marginStart="35dp"
        android:layout_marginTop="10dp"
        android:text="Password"
        android:textColor="@color/colorBlack" />
 
    <EditText
        android:id="@+id/register_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pass"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="30dp"
        android:background="@drawable/edit"
        android:drawableStart="@drawable/password"
        android:hint="Password..."
        android:inputType="textPassword"
        android:padding="8dp" />
 
    <Button
        android:id="@+id/register_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/register_password"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="30dp"
        android:layout_marginRight="30dp"
        android:background="@drawable/buttonss"
        android:padding="4dp"
        android:text="Create Account"
        android:textAllCaps="false"
        android:textColor="@android:color/background_light"
        android:textSize="24sp" />
 
    <TextView
        android:id="@+id/homepage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/register_button"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="15dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="23dp"
        android:text=" Already Have a Account?Sign In"
        android:textAlignment="center"
        android:textColor="@color/colorBlack"
        android:textSize="17sp"
        android:textStyle="bold" />
 
</RelativeLayout>


Working with the RegistrationActivity.java file. Creating a user with the email and password written by the user. If it fails then we will be showing the error.

mAuth.createUserWithEmailAndPassword(emaill, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()){
                    progressDialog.dismiss();
                }
                else {
                    progressDialog.dismiss();
                    Toast.makeText(RegistrationActivity.this,"Error",Toast.LENGTH_LONG).show();
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                progressDialog.dismiss();
                Toast.makeText(RegistrationActivity.this,"Error Occurred",Toast.LENGTH_LONG).show();
            }
     });

Below is the code for the RegistrationActivity.java file.

Java




package com.example.socialmediaapp;
 
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
 
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
 
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
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 com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
 
import java.util.HashMap;
 
public class RegistrationActivity extends AppCompatActivity {
 
    private EditText email, password, name;
    private Button mRegister;
    private TextView existaccount;
    private ProgressDialog progressDialog;
    private FirebaseAuth mAuth;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("Create Account");
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        email = findViewById(R.id.register_email);
        name = findViewById(R.id.register_name);
        password = findViewById(R.id.register_password);
        mRegister = findViewById(R.id.register_button);
        existaccount = findViewById(R.id.homepage);
        mAuth = FirebaseAuth.getInstance();
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Register");
         
        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String emaill = email.getText().toString().trim();
                String uname = name.getText().toString().trim();
                String pass = password.getText().toString().trim();
                if (!Patterns.EMAIL_ADDRESS.matcher(emaill).matches()) {
                    email.setError("Invalid Email");
                    email.setFocusable(true);
                } else if (pass.length() < 6) {
                    password.setError("Length Must be greater than 6 character");
                    password.setFocusable(true);
                } else {
                    registerUser(emaill, pass, uname);
                }
            }
        });
        existaccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(RegistrationActivity.this, LoginActivity.class));
            }
        });
    }
 
    private void registerUser(String emaill, final String pass, final String uname) {
        progressDialog.show();
        mAuth.createUserWithEmailAndPassword(emaill, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    progressDialog.dismiss();
                    FirebaseUser user = mAuth.getCurrentUser();
                    String email = user.getEmail();
                    String uid = user.getUid();
                    HashMap<Object, String> hashMap = new HashMap<>();
                    hashMap.put("email", email);
                    hashMap.put("uid", uid);
                    hashMap.put("name", uname);
                    hashMap.put("onlineStatus", "online");
                    hashMap.put("typingTo", "noOne");
                    hashMap.put("image", "");
                    FirebaseDatabase database = FirebaseDatabase.getInstance();
                    DatabaseReference reference = database.getReference("Users");
                    reference.child(uid).setValue(hashMap);
                    Toast.makeText(RegistrationActivity.this, "Registered User " + user.getEmail(), Toast.LENGTH_LONG).show();
                    Intent mainIntent = new Intent(RegistrationActivity.this, DashboardActivity.class);
                    mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(mainIntent);
                    finish();
                } else {
                    progressDialog.dismiss();
                    Toast.makeText(RegistrationActivity.this, "Error", Toast.LENGTH_LONG).show();
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                progressDialog.dismiss();
                Toast.makeText(RegistrationActivity.this, "Error Occurred", Toast.LENGTH_LONG).show();
            }
        });
    }
 
    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return super.onSupportNavigateUp();
    }
}


Step 6: Working with the LoginActivity Activity

Navigate to the app > res > layout > activity_login.xml and add the below code to that file. Below is the code for the activity_login.xml file. Here We are creating two EditText for email and password and one Button to Login.

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity">
 
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">
 
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
 
            <TextView
                android:id="@+id/welcom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="120dp"
                android:layout_marginTop="50dp"
                android:text="Welcome!"
                android:textColor="@color/colorBlack"
                android:textSize="30sp"
                android:textStyle="italic|bold" />
 
            <TextView
                android:id="@+id/email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/welcom"
                android:layout_marginStart="35dp"
                android:layout_marginTop="120dp"
                android:text="Email"
                android:textColor="@color/colorBlack" />
 
            <EditText
                android:id="@+id/login_email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/email"
                android:layout_alignParentStart="true"
                android:layout_marginLeft="30dp"
                android:layout_marginTop="5dp"
                android:layout_marginRight="30dp"
                android:background="@drawable/edit"
                android:drawableStart="@drawable/ic_email"
                android:hint="Email..."
                android:inputType="textEmailAddress"
                android:padding="8dp" />
 
            <TextView
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/login_email"
                android:layout_marginStart="32dp"
                android:layout_marginTop="20dp"
                android:text="Password"
                android:textColor="@color/colorBlack" />
 
            <EditText
                android:id="@+id/login_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/password"
                android:layout_alignParentStart="true"
                android:layout_marginLeft="30dp"
                android:layout_marginTop="5dp"
                android:layout_marginRight="30dp"
                android:background="@drawable/edit"
                android:drawableStart="@drawable/password"
                android:hint="Password..."
                android:inputType="textPassword"
                android:padding="8dp" />
 
            <TextView
                android:id="@+id/forgetp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/login_password"
                android:layout_marginStart="210dp"
                android:layout_marginTop="15dp"
                android:layout_marginEnd="23dp"
                android:text="Forget Password?"
                android:textAlignment="center"
                android:textColor="@color/colorPrimaryDark"
                android:textSize="17sp"
                android:textStyle="bold" />
 
            <Button
                android:id="@+id/login_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/forgetp"
                android:layout_marginLeft="30dp"
                android:layout_marginTop="15dp"
                android:layout_marginRight="30dp"
                android:background="@drawable/buttonss"
                android:padding="4dp"
                android:text="Login"
                android:textAllCaps="false"
                android:textColor="@android:color/background_light"
                android:textSize="24sp" />
 
            <TextView
                android:id="@+id/needs_new_account"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/login_button"
                android:layout_centerHorizontal="true"
                android:layout_marginStart="15dp"
                android:layout_marginTop="20dp"
                android:layout_marginEnd="23dp"
                android:text="Need new Account?Sign Up Here"
                android:textAlignment="center"
                android:textColor="@color/colorPrimary"
                android:textSize="17sp"
                android:textStyle="bold" />
        </RelativeLayout>
 
    </ScrollView>
 
</LinearLayout>


Working with the LoginActivity.java file. Signing in user with the email and password written by the user. If it fails then we will be showing the error.

mAuth.signInWithEmailAndPassword(emaill, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                if (task.isSuccessful()) {
                    loadingBar.dismiss();

                }
                else {
                    loadingBar.dismiss();
                    Toast.makeText(LoginActivity.this,"Login Failed",Toast.LENGTH_LONG).show();
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                loadingBar.dismiss();
                Toast.makeText(LoginActivity.this,"Error Occurred",Toast.LENGTH_LONG).show();
            }
    });

Below is the code for the LoginActivity.java file. 

Java




package com.example.socialmediaapp;
 
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
 
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
 
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
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 com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
 
import java.util.HashMap;
 
public class LoginActivity extends AppCompatActivity {
 
    private EditText email, password, name;
    private Button mlogin;
    private TextView newdnewaccount, reocverpass;
    FirebaseUser currentUser;
    private ProgressDialog loadingBar;
    private FirebaseAuth mAuth;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("Create Account");
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
         
        // initialising the layout items
        email = findViewById(R.id.login_email);
        password = findViewById(R.id.login_password);
        newdnewaccount = findViewById(R.id.needs_new_account);
        reocverpass = findViewById(R.id.forgetp);
        mAuth = FirebaseAuth.getInstance();
        mlogin = findViewById(R.id.login_button);
        loadingBar = new ProgressDialog(this);
        mAuth = FirebaseAuth.getInstance();
         
        // checking if user is null or not
        if (mAuth != null) {
            currentUser = mAuth.getCurrentUser();
        }
 
        mlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String emaill = email.getText().toString().trim();
                String pass = password.getText().toString().trim();
                 
                // if format of email doesn't matches return null
                if (!Patterns.EMAIL_ADDRESS.matcher(emaill).matches()) {
                    email.setError("Invalid Email");
                    email.setFocusable(true);
 
                } else {
                    loginUser(emaill, pass);
                }
            }
        });
         
        // If new account then move to Registration Activity
        newdnewaccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, RegistrationActivity.class));
            }
        });
         
        // Recover Your Password using email
        reocverpass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showRecoverPasswordDialog();
            }
        });
    }
 
    private void showRecoverPasswordDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Recover Password");
        LinearLayout linearLayout = new LinearLayout(this);
        final EditText emailet = new EditText(this);//write your registered email
        emailet.setText("Email");
        emailet.setMinEms(16);
        emailet.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
        linearLayout.addView(emailet);
        linearLayout.setPadding(10, 10, 10, 10);
        builder.setView(linearLayout);
        builder.setPositiveButton("Recover", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String emaill = emailet.getText().toString().trim();
                beginRecovery(emaill);//send a mail on the mail to recover password
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }
 
    private void beginRecovery(String emaill) {
        loadingBar.setMessage("Sending Email....");
        loadingBar.setCanceledOnTouchOutside(false);
        loadingBar.show();
        
        // send reset password email
        mAuth.sendPasswordResetEmail(emaill).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                loadingBar.dismiss();
                if (task.isSuccessful()) {
                    Toast.makeText(LoginActivity.this, "Done sent", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(LoginActivity.this, "Error Occurred", Toast.LENGTH_LONG).show();
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                loadingBar.dismiss();
                Toast.makeText(LoginActivity.this, "Error Failed", Toast.LENGTH_LONG).show();
            }
        });
    }
 
    private void loginUser(String emaill, String pass) {
        loadingBar.setMessage("Logging In....");
        loadingBar.show();
         
        // sign in with email and password after authenticating
        mAuth.signInWithEmailAndPassword(emaill, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
 
                if (task.isSuccessful()) {
 
                    loadingBar.dismiss();
                    FirebaseUser user = mAuth.getCurrentUser();
 
                    if (task.getResult().getAdditionalUserInfo().isNewUser()) {
                        String email = user.getEmail();
                        String uid = user.getUid();
                        HashMap<Object, String> hashMap = new HashMap<>();
                        hashMap.put("email", email);
                        hashMap.put("uid", uid);
                        hashMap.put("name", "");
                        hashMap.put("onlineStatus", "online");
                        hashMap.put("typingTo", "noOne");
                        hashMap.put("phone", "");
                        hashMap.put("image", "");
                        hashMap.put("cover", "");
                        FirebaseDatabase database = FirebaseDatabase.getInstance();
                         
                        // store the value in Database in "Users" Node
                        DatabaseReference reference = database.getReference("Users");
 
                        // storing the value in Firebase
                        reference.child(uid).setValue(hashMap);
                    }
                    Toast.makeText(LoginActivity.this, "Registered User " + user.getEmail(), Toast.LENGTH_LONG).show();
                    Intent mainIntent = new Intent(LoginActivity.this, DashboardActivity.class);
                    mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(mainIntent);
                    finish();
                } else {
                    loadingBar.dismiss();
                    Toast.makeText(LoginActivity.this, "Login Failed", Toast.LENGTH_LONG).show();
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                loadingBar.dismiss();
                Toast.makeText(LoginActivity.this, "Error Occurred", Toast.LENGTH_LONG).show();
            }
        });
    }
 
    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return super.onSupportNavigateUp();
    }
}


Step 7: Working with the AndroidManifest.xml file 

Navigate to the AndroidManifest.xml file and add the below permission for getting internet permission in the app.

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

Also, make the SplashScreen Activity as the welcome screen. Refer to the following code 

<activity android:name=".SplashScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

 Below is the complete code for the AndroidManifest.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
    package="com.example.socialmediaapp">
 
    <uses-permission android:name="android.permission.INTERNET" />
 
    <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/Theme.SocialMediaApp">
        <activity android:name=".DashboardActivity"></activity>
        <activity android:name=".LoginActivity" />
        <activity android:name=".RegistrationActivity" />
        <activity android:name=".SplashScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>


Step 8: Working with the colors.xml file

Go to the app > res > values > colors.xml file and use the following color

XML




<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#CC2E76BE</color>
    <color name="colorPrimaryDark">#CC073D74</color>
    <color name="colorAccent">#0A527E</color>
    <color name="colorBlack">#000000</color>
    <color name="colorWhite">#fff</color>
    <color name="colorGray">#F5F1F1</color>
    <color name="colorgray01">#959595</color>
    <color name="colorGray02">#f5f0f0</color>
    <color name="colorGreen">#17581A</color>
    <color name="colorRed">#F4511E</color>
    <color name="btnRed">#E61313</color>
    <color name="btnedit">#2CA7E0</color>
    <color name="neutral">#0a1b97</color>
    <color name="colordrawer">#2B2C2E</color>
</resources>


Output:

Registration:

Login:

After successfully authenticated the admin can see the users in the firebase console like the following

For all the drawable file used in this article please refer to this link: https://drive.google.com/drive/folders/1M_knOH_ugCuwSP5nkYzeD4dRp-Honzbe?usp=sharing

Below is the file structure after performing these operations:

Note: In this part, there is nothing to do with the DashboardActivity.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments