Skip to content

Instantly share code, notes, and snippets.

@anandrex5
Created July 27, 2022 12:59
Show Gist options
  • Save anandrex5/dca58b04b72aba0962c20d401381432e to your computer and use it in GitHub Desktop.
Save anandrex5/dca58b04b72aba0962c20d401381432e to your computer and use it in GitHub Desktop.
Realtime_FireBase Data Insertion and Retrieve
<?xml version="1.0" encoding="utf-8"?>
<layout
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"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextName"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="250dp"
android:hint="Enter your name"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/buttonSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Data"
app:layout_constraintTop_toBottomOf="@+id/editTextName"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:id="@+id/textViewName"
android:layout_width="300dp"
android:layout_height="30dp"
android:gravity="center"
android:text="TextView"
android:textStyle="bold"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonSend" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'com.google.gms.google-services'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.example.realtime_firebase"
minSdk 21
targetSdk 31
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
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding = true
dataBinding = true
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.firebase:firebase-database:20.0.5'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.google.android.gms:play-services-location:20.0.0'
}
package com.example.realtime_firebase
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import com.example.realtime_firebase.databinding.ActivityMainBinding
import com.google.firebase.database.*
class MainActivity : AppCompatActivity() {
private lateinit var binding : ActivityMainBinding
// var editTextName:EditText = EditText()
// lateinit var buttonSend : Button
val database : FirebaseDatabase = FirebaseDatabase.getInstance()
//reach to the main database and add data
val reference : DatabaseReference = database.reference.child("Users")
val reference2 : DatabaseReference = database.reference
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// editTextName = findViewById(R.id.editTextName)
// buttonSend = findViewById(R.id.buttonSend)
// buttonSend.setOnClickListener {
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.buttonSend.setOnClickListener {
reference2.addValueEventListener(object : ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
val realName : String = snapshot.child("Users").child("name").value as String
binding.textViewName.text= realName
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
//send the data to the database
val userName: String = binding.editTextName.text.toString()
reference.child("userName").setValue(userName)
Toast.makeText(applicationContext,"Data enter successfully",Toast.LENGTH_SHORT).show()
}
}
}
@anandrex5
Copy link
Author

Q - If you want to show data in a toast that changes in database?
Ans -

Replace this onDataChange method from previous one in main Activity --

override fun onDataChange(snapshot: DataSnapshot) {
// val realName : String = snapshot.child("Users").child("name").value as String
// binding.textViewName.text= realName

                    val children = snapshot.children
                    
                       children.forEach {
                         Toast.makeText(applicationContext,it.toString(),Toast.LENGTH_LONG).show()
                    }
                }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment