Skip to content

Instantly share code, notes, and snippets.

@kyodgorbek
Created December 8, 2020 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyodgorbek/3d284add2d2f2b8b3fc68ddc2ad0ce9b to your computer and use it in GitHub Desktop.
Save kyodgorbek/3d284add2d2f2b8b3fc68ddc2ad0ce9b to your computer and use it in GitHub Desktop.
Firestore project
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/inputFirstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your first name"
android:layout_margin="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/inputLastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:layout_constraintStart_toStartOf="parent"
android:hint="Enter your last name"
app:layout_constraintTop_toBottomOf="@id/inputFirstName"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="76dp"
android:id="@+id/saveButton"
android:text="Save"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/inputLastName" />
</androidx.constraintlayout.widget.ConstraintLayout>
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val firstName = inputFirstName.text.toString()
val secondName = inputLastName.text.toString()
saveButton.setOnClickListener{
saveFireStore(firstName, secondName)
}
}
fun saveFireStore(firstname:String, lastname:String){
val db = FirebaseFirestore.getInstance()
val user : MutableMap<String, Any> = HashMap()
user["firstName"] = firstname
user["lastName"] = lastname
db.collection("users")
.add(user)
.addOnSuccessListener{
Toast.makeText(this@MainActivity, "Record Added Successfully", Toast.LENGTH_SHORT).show()
Log.d(String, firstname + lastname)
}
.addOnFailureListener{
Toast.makeText(this@MainActivity, "Record Failed", Toast.LENGTH_SHORT).show()
}
}
companion object {
const val String = "show logs"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment