Registration form in android studio using firebase
AndroidManifest.xml
<!-- adding Internet permissions-->
<uses-permission android:name="android.permission.INTERNET"/>
gradle.build (project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
// always use latest version of dependencies
classpath "com.android.tools.build:gradle:4.1.3"
classpath 'com.google.gms:google-services:4.3.10'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
gradle.build (app)
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}
android {
compileSdkVersion 32
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.learnoset.firebasetutorials"
minSdkVersion 19
targetSdkVersion 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
testImplementation 'junit:junit:4.+'
// Firebase Bom Dependency
implementation platform('com.google.firebase:firebase-bom:29.0.3')
// Firebase Realtime Dependency
implementation 'com.google.firebase:firebase-database'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
activity_main.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Register"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="@+id/fullnameET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="FullName" />
<EditText
android:id="@+id/mobileET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Mobile" />
<EditText
android:id="@+id/emailET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/registerBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Register Now" />
</LinearLayout>
MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
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 com.google.firebase.database.ValueEventListener;
public class MainActivity extends AppCompatActivity {
private final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
// to get available / registered users in Firebase Database
private int availableUsers = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// getting TextViews from xml file
final EditText fullnameET = findViewById(R.id.fullnameET);
final EditText emailET = findViewById(R.id.emailET);
final EditText mobileET = findViewById(R.id.mobileET);
final AppCompatButton registerBtn = findViewById(R.id.registerBtn);
// getting available users from Firebase Database
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
// check if database has users as root
if (snapshot.hasChild("users")) {
// getting available / registerd users
availableUsers = (int) snapshot.child("users").getChildrenCount();
} else {
// if there is no available user in the Firebase Database then availableUsers remain 0
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
// register new user
registerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// getting strings from EditTexts
final String fullnameTxt = fullnameET.getText().toString();
final String emailTxt = emailET.getText().toString();
final String mobileTxt = mobileET.getText().toString();
// sending data to firebase into users root
// availableUsers + 1 increasing user index to save new user details in it
databaseReference.child("users").child(String.valueOf(availableUsers + 1)).child("fullname").setValue(fullnameTxt);// adding fullname
databaseReference.child("users").child(String.valueOf(availableUsers + 1)).child("email").setValue(emailTxt); // adding email
databaseReference.child("users").child(String.valueOf(availableUsers + 1)).child("mobile").setValue(mobileTxt); // adding mobile
Toast.makeText(MainActivity.this, "Data Added", Toast.LENGTH_SHORT).show();
}
});
}
}
Projects with Source Code + Video Tutorials
You can download our Java and Android Studio Projects with Source Code and Video Tutorials.
Online Quiz Application using Firebase and Admob
Settings and Profile page UI design in Android Studio with Example and Source Code
Create your own Web Browser Source Code
If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com
Follow us to learn Coding and get in touch with new Technologies.