0% found this document useful (0 votes)
10 views

Firebase Introduction

Uploaded by

manasdere2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Firebase Introduction

Uploaded by

manasdere2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

PROBLEM STATEMENT

You are tasked with developing a signup and login system. The objective is to implement
authentication functionalities using Firebase database services. Additionally, you are required to
integrate and utilise a form previously created in a prior assignment for gathering user information.
The collected data from this form should be stored in Firebase's realtime database. Furthermore,
you must design a mechanism to retrieve this stored information from the database and display it
within the application.

CODE
activity_main.xml file:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<TextView
android:id="@+id/user_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />

<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name"/>

<EditText
android:id="@+id/et_roll_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Roll Number"/>

<Button
android:text="Save"
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:text="View student details"
android:id="@+id/btn_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:text="@string/log_out"
android:id="@+id/btn_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>
</RelativeLayout>

MainActivity.java file:
package com.example.loginsignupexample;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google. rebase.auth.FirebaseAuth;


import com.google. rebase.auth.FirebaseUser;
import com.google. rebase.database.DatabaseReference;
import com.google. rebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity {

FirebaseAuth auth;
Button btnLogout, btnSave, btnDetails;
TextView textView;
EditText etName, etRollNumber;
FirebaseUser user;
DatabaseReference databaseReference;

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

auth = FirebaseAuth.getInstance();
btnLogout = ndViewById(R.id.btn_logout);
btnSave = ndViewById(R.id.btn_save);
btnDetails = ndViewById(R.id.btn_details);
textView = ndViewById(R.id.user_details);
etName = ndViewById(R.id.et_name);
etRollNumber = ndViewById(R.id.et_roll_number);
user = auth.getCurrentUser();
databaseReference = FirebaseDatabase.getInstance().getReference().child("Students");

if(user == null){
Intent intent = new Intent(getApplicationContext(), Login.class);
startActivity(intent);
nish();
}
else{
textView.setText(user.getEmail());
}

btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseAuth.getInstance().signOut();
Intent intent = new Intent(getApplicationContext(), Login.class);
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
startActivity(intent);
nish();
}
});

btnDetails.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), student_details_display.class);
startActivity(intent);
nish();
}
});

btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = etName.getText().toString().trim();
String rollNumber = etRollNumber.getText().toString().trim();

if(!name.isEmpty() && !rollNumber.isEmpty()){


Student student = new Student(name, rollNumber);
databaseReference.push().setValue(student);
etName.setText("");
etRollNumber.setText("");
}
}
});
}

private static class Student {


public String name;
public String rollNumber;

public Student() {
// Default constructor required for calls to DataSnapshot.getValue(Student.class)
}

public Student(String name, String rollNumber) {


this.name = name;
this.rollNumber = rollNumber;
}
}
}

activity_login.xml file:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="15dp"
fi
fi
tools:context=".Login">

<TextView
android:text="@string/login"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<com.google.android.material.text eld.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.text eld.TextInputEditText
android:id="@+id/email"
android:hint="@string/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.text eld.TextInputLayout>

<com.google.android.material.text eld.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.text eld.TextInputEditText
android:id="@+id/password"
android:hint="@string/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.text eld.TextInputLayout>
<ProgressBar
android:id="@+id/progressBar"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:text="@string/login"
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/registerNow"
android:text="@string/don_t_have_an_account_register_now"
android:layout_marginTop="20dp"
android:textSize="20sp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>

Login.java file:
package com.example.loginsignupexample;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
fi
fi
fi
fi
fi
fi
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
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.android.material.text eld.TextInputEditText;
import com.google. rebase.auth.AuthResult;
import com.google. rebase.auth.FirebaseAuth;
import com.google. rebase.auth.FirebaseUser;

public class Login extends AppCompatActivity {

TextInputEditText editTextEmail, editTextPassword;


Button buttonLogin;
FirebaseAuth mAuth;
ProgressBar progressBar;
TextView textView;

@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null){
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
nish();
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
editTextEmail = ndViewById(R.id.email);
editTextPassword = ndViewById(R.id.password);
buttonLogin = ndViewById(R.id.btn_login);
progressBar = ndViewById(R.id.progressBar);
textView = ndViewById(R.id.registerNow);
mAuth = FirebaseAuth.getInstance();

textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), Register.class);
startActivity(intent);
nish();
}
});

buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressBar.setVisibility(View.VISIBLE);
String email,password;
email = String.valueOf(editTextEmail.getText());
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
password = String.valueOf(editTextPassword.getText());

if(TextUtils.isEmpty(email)){
Toast.makeText(Login.this, "Enter Email", Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(Login.this, "Enter Password", Toast.LENGTH_SHORT).show();
return;
}
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener( new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Toast.makeText(getApplicationContext(), "Login Successful.",
Toast.LENGTH_SHORT).show();

Intent intent = new Intent(getApplicationContext(), MainActivity.class);


startActivity(intent);
nish();

} else {
// If sign in fails, display a message to the user.
Toast.makeText(Login.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();

}
}
});
}
});
}
}

activity_register.xml file:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="15dp"
tools:context=".Register">

<TextView
android:text="@string/register"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<com.google.android.material.text eld.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.text eld.TextInputEditText
fi
fi
fi
android:id="@+id/email"
android:hint="@string/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.text eld.TextInputLayout>

<com.google.android.material.text eld.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.text eld.TextInputEditText
android:id="@+id/password"
android:hint="@string/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.text eld.TextInputLayout>
<ProgressBar
android:id="@+id/progressBar"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:text="@string/register"
android:id="@+id/btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/loginNow"
android:text="@string/already_have_an_account_login"
android:layout_marginTop="20dp"
android:textSize="20sp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>

Register.java file:
package com.example.loginsignupexample;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
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.android.material.text eld.TextInputEditText;
import com.google. rebase.auth.AuthResult;
import com.google. rebase.auth.FirebaseAuth;
fi
fi
fi
fi
fi
fi
fi
import com.google. rebase.auth.FirebaseUser;

public class Register extends AppCompatActivity {

TextInputEditText editTextEmail, editTextPassword;


Button buttonReg;
FirebaseAuth mAuth;
ProgressBar progressBar;
TextView textView;

@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null){
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
nish();
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
editTextEmail = ndViewById(R.id.email);
editTextPassword = ndViewById(R.id.password);
buttonReg = ndViewById(R.id.btn_register);
progressBar = ndViewById(R.id.progressBar);
textView = ndViewById(R.id.loginNow);
mAuth = FirebaseAuth.getInstance();

textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), Login.class);
startActivity(intent);
nish();
}
});

buttonReg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressBar.setVisibility(View.VISIBLE);
String email,password;
email = String.valueOf(editTextEmail.getText());
password = String.valueOf(editTextPassword.getText());

if(TextUtils.isEmpty(email)){
Toast.makeText(Register.this, "Enter Email", Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(Register.this, "Enter Password", Toast.LENGTH_SHORT).show();
return;
}
mAuth.createUserWithEmailAndPassword(email, password)
fi
fi
fi
fi
fi
fi
fi
fi
.addOnCompleteListener( new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
if (task.isSuccessful()) {

Toast.makeText(Register.this, "Account Created.",


Toast.LENGTH_SHORT).show();

} else {
// If sign in fails, display a message to the user.

Toast.makeText(Register.this, "Authentication failed.",


Toast.LENGTH_SHORT).show();

}
}
});
}
});
}
}

activity_register.xml file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".student_details_display">

<ListView
android:id="@+id/list_view_students"
android:layout_width="0dp"
android:layout_height="0dp"
android:divider="@android:color/black"
android:dividerHeight="1dp"
app:layout_constraintBottom_toTopOf="@id/btn_back"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/list_view_students" />

</androidx.constraintlayout.widget.ConstraintLayout>
Login.java file:
package com.example.loginsignupexample;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.google. rebase.database.DataSnapshot;


import com.google. rebase.database.DatabaseError;
import com.google. rebase.database.DatabaseReference;
import com.google. rebase.database.FirebaseDatabase;
import com.google. rebase.database.ValueEventListener;

import java.util.ArrayList;

public class student_details_display extends AppCompatActivity {

ListView listViewStudents;
DatabaseReference databaseReference;
ArrayList<String> studentsList = new ArrayList<>();

Button btnBack;

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

btnBack = ndViewById(R.id.btn_back);

listViewStudents = ndViewById(R.id.list_view_students);
databaseReference = FirebaseDatabase.getInstance().getReference().child("Students");

nal ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,


android.R.layout.simple_list_item_1, studentsList);
listViewStudents.setAdapter(arrayAdapter);

btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
nish();
}
});

databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
studentsList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Student student = snapshot.getValue(Student.class);
fi
fi
fi
fi
fi
fi
fi
fi
fi
String studentInfo = "Name: " + student.name + "\nRoll Number: " +
student.rollNumber;
studentsList.add(studentInfo);
}
arrayAdapter.notifyDataSetChanged();
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}

private static class Student {


public String name;
public String rollNumber;

public Student() {
// Default constructor required for calls to DataSnapshot.getValue(Student.class)
}

public Student(String name, String rollNumber) {


this.name = name;
this.rollNumber = rollNumber;
}
}
}

OUTPUT

You might also like