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="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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>
[Link] file:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link]. [Link];
import [Link]. [Link];
import [Link]. [Link];
import [Link]. [Link];
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) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
auth = [Link]();
btnLogout = ndViewById([Link].btn_logout);
btnSave = ndViewById([Link].btn_save);
btnDetails = ndViewById([Link].btn_details);
textView = ndViewById([Link].user_details);
etName = ndViewById([Link].et_name);
etRollNumber = ndViewById([Link].et_roll_number);
user = [Link]();
databaseReference = [Link]().getReference().child("Students");
if(user == null){
Intent intent = new Intent(getApplicationContext(), [Link]);
startActivity(intent);
nish();
}
else{
[Link]([Link]());
}
[Link](new [Link]() {
@Override
public void onClick(View view) {
[Link]().signOut();
Intent intent = new Intent(getApplicationContext(), [Link]);
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
startActivity(intent);
nish();
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), student_details_display.class);
startActivity(intent);
nish();
}
});
[Link](new [Link]() {
@Override
public void onClick(View view) {
String name = [Link]().toString().trim();
String rollNumber = [Link]().toString().trim();
if(![Link]() && ![Link]()){
Student student = new Student(name, rollNumber);
[Link]().setValue(student);
[Link]("");
[Link]("");
}
}
});
}
private static class Student {
public String name;
public String rollNumber;
public Student() {
// Default constructor required for calls to [Link]([Link])
}
public Student(String name, String rollNumber) {
[Link] = name;
[Link] = rollNumber;
}
}
}
activity_login.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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"/>
<[Link] [Link]
android:layout_width="match_parent"
android:layout_height="wrap_content">
<[Link] [Link]
android:id="@+id/email"
android:hint="@string/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</[Link] [Link]>
<[Link] [Link]
android:layout_width="match_parent"
android:layout_height="wrap_content">
<[Link] [Link]
android:id="@+id/password"
android:hint="@string/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</[Link] [Link]>
<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>
[Link] file:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
fi
fi
fi
fi
fi
fi
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link] [Link];
import [Link]. [Link];
import [Link]. [Link];
import [Link]. [Link];
public class Login extends AppCompatActivity {
TextInputEditText editTextEmail, editTextPassword;
Button buttonLogin;
FirebaseAuth mAuth;
ProgressBar progressBar;
TextView textView;
@Override
public void onStart() {
[Link]();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = [Link]();
if(currentUser != null){
Intent intent = new Intent(getApplicationContext(), [Link]);
startActivity(intent);
nish();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_login);
editTextEmail = ndViewById([Link]);
editTextPassword = ndViewById([Link]);
buttonLogin = ndViewById([Link].btn_login);
progressBar = ndViewById([Link]);
textView = ndViewById([Link]);
mAuth = [Link]();
[Link](new [Link]() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), [Link]);
startActivity(intent);
nish();
}
});
[Link](new [Link]() {
@Override
public void onClick(View view) {
[Link]([Link]);
String email,password;
email = [Link]([Link]());
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
password = [Link]([Link]());
if([Link](email)){
[Link]([Link], "Enter Email", Toast.LENGTH_SHORT).show();
return;
}
if([Link](password)){
[Link]([Link], "Enter Password", Toast.LENGTH_SHORT).show();
return;
}
[Link](email, password)
.addOnCompleteListener( new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
[Link]([Link]);
if ([Link]()) {
// Sign in success, update UI with the signed-in user's information
[Link](getApplicationContext(), "Login Successful.",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), [Link]);
startActivity(intent);
nish();
} else {
// If sign in fails, display a message to the user.
[Link]([Link], "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}
activity_register.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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"/>
<[Link] [Link]
android:layout_width="match_parent"
android:layout_height="wrap_content">
<[Link] [Link]
fi
fi
fi
android:id="@+id/email"
android:hint="@string/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</[Link] [Link]>
<[Link] [Link]
android:layout_width="match_parent"
android:layout_height="wrap_content">
<[Link] [Link]
android:id="@+id/password"
android:hint="@string/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</[Link] [Link]>
<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>
[Link] file:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link] [Link];
import [Link]. [Link];
import [Link]. [Link];
fi
fi
fi
fi
fi
fi
fi
import [Link]. [Link];
public class Register extends AppCompatActivity {
TextInputEditText editTextEmail, editTextPassword;
Button buttonReg;
FirebaseAuth mAuth;
ProgressBar progressBar;
TextView textView;
@Override
public void onStart() {
[Link]();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = [Link]();
if(currentUser != null){
Intent intent = new Intent(getApplicationContext(), [Link]);
startActivity(intent);
nish();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_register);
editTextEmail = ndViewById([Link]);
editTextPassword = ndViewById([Link]);
buttonReg = ndViewById([Link].btn_register);
progressBar = ndViewById([Link]);
textView = ndViewById([Link]);
mAuth = [Link]();
[Link](new [Link]() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), [Link]);
startActivity(intent);
nish();
}
});
[Link](new [Link]() {
@Override
public void onClick(View view) {
[Link]([Link]);
String email,password;
email = [Link]([Link]());
password = [Link]([Link]());
if([Link](email)){
[Link]([Link], "Enter Email", Toast.LENGTH_SHORT).show();
return;
}
if([Link](password)){
[Link]([Link], "Enter Password", Toast.LENGTH_SHORT).show();
return;
}
[Link](email, password)
fi
fi
fi
fi
fi
fi
fi
fi
.addOnCompleteListener( new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
[Link]([Link]);
if ([Link]()) {
[Link]([Link], "Account Created.",
Toast.LENGTH_SHORT).show();
} else {
// If sign in fails, display a message to the user.
[Link]([Link], "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}
activity_register.xml file:
<?xml version="1.0" encoding="utf-8"?>
<[Link] xmlns:android="[Link]
apk/res/android"
xmlns:app="[Link]
xmlns:tools="[Link]
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" />
</[Link]>
[Link] file:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link]. [Link];
import [Link]. [Link];
import [Link]. [Link];
import [Link]. [Link];
import [Link]. [Link];
import [Link];
public class student_details_display extends AppCompatActivity {
ListView listViewStudents;
DatabaseReference databaseReference;
ArrayList<String> studentsList = new ArrayList<>();
Button btnBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_student_details_display);
btnBack = ndViewById([Link].btn_back);
listViewStudents = ndViewById([Link].list_view_students);
databaseReference = [Link]().getReference().child("Students");
nal ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,
[Link].simple_list_item_1, studentsList);
[Link](arrayAdapter);
[Link](new [Link]() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), [Link]);
startActivity(intent);
nish();
}
});
[Link](new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
[Link]();
for (DataSnapshot snapshot : [Link]()) {
Student student = [Link]([Link]);
fi
fi
fi
fi
fi
fi
fi
fi
fi
String studentInfo = "Name: " + [Link] + "\nRoll Number: " +
[Link];
[Link](studentInfo);
}
[Link]();
}
@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 [Link]([Link])
}
public Student(String name, String rollNumber) {
[Link] = name;
[Link] = rollNumber;
}
}
}
OUTPUT