Skip to content

Instantly share code, notes, and snippets.

@anandrex5
Created July 27, 2022 12:02
Show Gist options
  • Save anandrex5/5c98435d9e13feccd689e623f5594560 to your computer and use it in GitHub Desktop.
Save anandrex5/5c98435d9e13feccd689e623f5594560 to your computer and use it in GitHub Desktop.
Realtime_FireBase Data Insertion
<?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"/>
</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 com.example.realtime_firebase.databinding.ActivityMainBinding
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
class MainActivity : AppCompatActivity() {
private lateinit var binding : ActivityMainBinding
lateinit var editTextName : 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")
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 {
//send data to the database
val userName: String = editTextName.text.toString()
reference.child("userName").setValue(userName)
Toast.makeText(applicationContext,"Data enter successfully",Toast.LENGTH_SHORT).show()
}
// binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
// binding.editTextName.text
// binding.buttonSend.hasOnClickListeners()
//give value to the child
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment