send data to firebase database in android studio
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">
<EditText
android:id="@+id/messageET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type Message" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/sendBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Send Message to Firebase" />
</LinearLayout>
MainActivity.java
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
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();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// getting TextViews from xml file
final EditText messageET = findViewById(R.id.messageET);
final AppCompatButton sendMessageBtn = findViewById(R.id.sendBtn);
// sending data to firebase on button click
sendMessageBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// getting messsage from EditText
final String getMessageTxt = messageET.getText().toString();
// check if user has entered a message or not
if(getMessageTxt.isEmpty()){
Toast.makeText(MainActivity.this, "Please type message", Toast.LENGTH_SHORT).show();
}
else{
// here you can write name(message) in which this message will be saved.
// like key_1, key_2
databaseReference.child("message").setValue(getMessageTxt);
}
}
});
}
}
Projects with Source Code + Video Tutorials
You can download our Java and Android Studio Projects with Source Code and Video Tutorials.

Simple Calculator App for Android

Simple Music Player App for Android (Offline Version)

How to create Modern Login and Register screen in Android Studio | Material UI Design | Part - 06
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.