How to save data to firebase with android studio
Android Studio is a great platform for creating apps that can be deployed on the google play store. If you're new to the series go and check out the Android app development Playlist. There, you will be able to see how to create basic apps so you can become a developer.
What is Firebase ?
Google Firebase is a Google-backed application development software that enables developers to develop iOS, Android and Web apps. Firebase provides tools for tracking analytics, reporting and fixing app crashes, creating marketing and product experiment.
Creating a realtime database and saving the data into the firebase
database with android studio using setvalue function and store
the attendance status for the created attendance app .
🠷🠷🠷Tutorial 🠷🠷🠷
Part 1: https://youtu.be/nKhXtul24os
Part 2: https://youtu.be/gr2wcAqPnKY
Part 3: https://youtu.be/Dg26qEQGFRA
🠷🠷🠷Code 🠷🠷🠷
Home.java
package com.example.attendance;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class Home extends AppCompatActivity {
FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mAuth=FirebaseAuth.getInstance();
}
@Override
protected void onStart() {
super.onStart();
FirebaseUser user=mAuth.getCurrentUser();
if(user==null){
startActivity(new Intent(this,Register.class));
}
}
public void mark(View view) {
Intent i=new Intent(Home.this,MainActivity.class);
startActivity(i);
}
public void view(View view) {
Intent i=new Intent(Home.this,ViewAttendance.class);
startActivity(i);
}
}
Register.java
package com.example.attendance;
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.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 Register extends AppCompatActivity {
FirebaseAuth mAuth;
EditText emailId,password;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mAuth=FirebaseAuth.getInstance();
emailId=findViewById(R.id.email);
password=findViewById(R.id.pass);
}
public void register(View view) {
String EmailId=emailId.getText().toString();
String Password=password.getText().toString();
if(TextUtils.isEmpty(EmailId)){
emailId.setError("Cannot
be Empty");
emailId.requestFocus();
}
else if(TextUtils.isEmpty(Password)){
password.setError("Cannot
be Empty");
password.requestFocus();
}
else {
mAuth.createUserWithEmailAndPassword(EmailId,Password).addOnCompleteListener(new
OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull
Task<AuthResult> task) {
if(task.isSuccessful())
{
Toast.makeText(Register.this,
"Account registered successfully", Toast.LENGTH_SHORT).show();
startActivity(new
Intent(Register.this,Home.class));
}
else{
Toast.makeText(Register.this,
"Failed", Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
Login.java
package com.example.attendance;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
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 {
FirebaseAuth mAuth1;
EditText emailId1,password1;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mAuth1=FirebaseAuth.getInstance();
emailId1=findViewById(R.id.email1);
password1=findViewById(R.id.pass1);
}
public void login(View view) {
String EmailId=emailId1.getText().toString();
String Password=password1.getText().toString();
if(TextUtils.isEmpty(EmailId)){
emailId1.setError("Cannot
be Empty");
emailId1.requestFocus();
}
else if(TextUtils.isEmpty(Password)){
password1.setError("Cannot
be Empty");
password1.requestFocus();
}
else {
mAuth1.signInWithEmailAndPassword(EmailId,Password).addOnCompleteListener(new
OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull
Task<AuthResult> task) {
if(task.isSuccessful())
{
Toast.makeText(Login.this,
"Logged in successfully", Toast.LENGTH_SHORT).show();
startActivity(new
Intent(Login.this,Home.class));
}
else{
Toast.makeText(Login.this,
"Failed", Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
MainActivity.java
package com.example.attendance;
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.Toast;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity {
Button mark,viewattendance;
EditText name;
private DatabaseReference mdatabaseReference;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mark=findViewById(R.id.btn);
viewattendance=findViewById(R.id.btn2);
name=findViewById(R.id.name);
mdatabaseReference=FirebaseDatabase.getInstance().getReference();
mark.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View
view) {
String mname=name.getText().toString();
mdatabaseReference.child(mname).setValue("Present");
Toast.makeText(MainActivity.this,
"Attendance Marked", Toast.LENGTH_SHORT).show();
}
});
viewattendance.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View
view) {
String mname=name.getText().toString();
mdatabaseReference.child(mname).setValue("Absent");
Toast.makeText(MainActivity.this,
"Attendance Marked", Toast.LENGTH_SHORT).show();
}
});
}
public void login(View view) {
startActivity(new Intent(this,Login.class));
}
}
ViewAttendance.java
package com.example.attendance;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.ArrayList;
public class ViewAttendance extends AppCompatActivity {
ListView present,absent;
ArrayList<String> arr1=new ArrayList<>();
ArrayList<String> arr2=new ArrayList<>();
DatabaseReference dref;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_attendance);
present=findViewById(R.id.ls1);
absent=findViewById(R.id.ls2);
ArrayAdapter arrayAdapter1=new ArrayAdapter(ViewAttendance.this,
android.R.layout.simple_list_item_1,arr1);
ArrayAdapter arrayAdapter2=new ArrayAdapter(ViewAttendance.this,
android.R.layout.simple_list_item_1,arr2);
dref= FirebaseDatabase.getInstance().getReference();
dref.addChildEventListener(new ChildEventListener()
{
@Override
public void onChildAdded(@NonNull
DataSnapshot snapshot, @Nullable String previousChildName) {
String
name=snapshot.getKey();
String
status=snapshot.getValue().toString();
if(status.equalsIgnoreCase("Present")){
arr1.add(name);
arrayAdapter1.notifyDataSetChanged();
}
else {
arr2.add(name);
arrayAdapter2.notifyDataSetChanged();
}
}
@Override
public void onChildChanged(@NonNull
DataSnapshot snapshot, @Nullable String previousChildName) {
arrayAdapter1.notifyDataSetChanged();
arrayAdapter2.notifyDataSetChanged();
}
@Override
public void onChildRemoved(@NonNull
DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull
DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull
DatabaseError error) {
}
});
present.setAdapter(arrayAdapter1);
absent.setAdapter(arrayAdapter2);
}
}
activity_home.xml
<?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=".Home">
<Button
android:id="@+id/button"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginTop="176dp"
android:text="Mark
Attendance"
android:onClick="mark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<Button
android:layout_width="200dp"
android:layout_height="40dp"
android:text="View
Attendance"
android:onClick="view"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button"
app:layout_constraintVertical_bias="0.317"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_login.xml
<?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=".Login">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/loginpage"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textColor="#FFFF"
android:textSize="35sp"
android:layout_marginTop="65dp"
android:layout_marginLeft="25dp"/>
<EditText
android:id="@+id/email1"
android:hint="Email"
android:layout_width="match_parent"
android:layout_marginTop="150dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress"
/>
<EditText
android:id="@+id/pass1"
android:hint="Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textColor="#FFFF"
android:text="Login"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:onClick="login"
android:background="@drawable/mybutton"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not
registered yet"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:textStyle="bold"
android:textColor="#448AFF"
android:text="Register"/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Activity_main.xml
<?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=".MainActivity">
<ImageView
android:id="@+id/image"
android:layout_width="150dp"
android:layout_height="150dp"
android:onClick="login"
android:src="@drawable/attendance"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="40dp"
app:layout_constraintTop_toTopOf="parent"
/>
<EditText
android:id="@+id/name"
android:layout_width="200dp"
android:layout_height="50dp"
android:hint="Name"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.511"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/image"
app:layout_constraintVertical_bias="0.512"
/>
<Button
android:id="@+id/btn"
android:layout_width="150dp"
android:layout_height="80dp"
android:layout_below="@+id/image"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:gravity="center"
android:text="Mark
Present!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btn2"
app:layout_constraintHorizontal_bias="0.421"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/image"
app:layout_constraintVertical_bias="0.722"
/>
<Button
android:id="@+id/btn2"
android:layout_width="150dp"
android:layout_height="80dp"
android:layout_below="@+id/image"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:layout_marginEnd="4dp"
android:gravity="center"
android:text="Mark
Absent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/image"
app:layout_constraintVertical_bias="0.722"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Activity_register.xml
<?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=".Register">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/loginpage"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:textColor="#FFFF"
android:textSize="35sp"
android:layout_marginTop="65dp"
android:layout_marginLeft="25dp"/>
<EditText
android:id="@+id/email"
android:hint="Email"
android:layout_width="match_parent"
android:layout_marginTop="150dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress"
/>
<EditText
android:id="@+id/pass"
android:hint="Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
/>
<Button
android:layout_width="120dp"
android:layout_height="wrap_content"
android:textColor="#FFFF"
android:text="Register"
android:onClick="register"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:background="@drawable/mybutton"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Already
registered"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:textStyle="bold"
android:textColor="#448AFF"
android:text="Login"/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Activity_view_attendance.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ViewAttendance">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Present"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/ls1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Absent"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/ls2"
/>
</LinearLayout>
🠷🠷🠷Source Elements 🠷🠷🠷
Comments
Post a Comment