Created
July 27, 2022 12:17
-
-
Save anandrex5/487ad8590ade8268cffb278451df1623 to your computer and use it in GitHub Desktop.
Realtime_FireBase Data Insertion (DATA BINDING)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.DatabaseReference | |
import com.google.firebase.database.FirebaseDatabase | |
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") | |
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 | |
// | |
binding = DataBindingUtil.setContentView(this, R.layout.activity_main) | |
binding.editTextName.text | |
binding.buttonSend.setOnClickListener { | |
val userName: String = binding.editTextName.text.toString() | |
reference.child("userName").setValue(userName) | |
Toast.makeText(applicationContext,"Data enter successfully",Toast.LENGTH_SHORT).show() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For retrieving data (Realtime_FireBase Data Insertion and Retrieve) ---
https://gist.github.com/anandrex5/dca58b04b72aba0962c20d401381432e