Skip to content

Instantly share code, notes, and snippets.

View kenny-io's full-sized avatar
😀
Always available

Ekene Eze kenny-io

😀
Always available
View GitHub Profile
public class MainActivity extends AppCompatActivity {
private static final String NAME_KEY = "Name";
private static final String EMAIL_KEY = "Email";
private static final String PHONE_KEY = "Phone";
FirebaseFirestore db;
TextView textDisplay;
@Override
private void addNewContact(){
Map<String, Object>newContact = new HashMap<>();
newContact.put(NAME_KEY, "John");
newContact.put(EMAIL_KEY, "[email protected]");
newContact.put(PHONE_KEY, "080-0808-009");
db.collection("PhoneBook").document("Contacts").set(newContact)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity.this, "User Registered",
private void ReadSingleContact() {
DocumentReference user = db.collection("PhoneBook").document("Contacts");
user.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
DocumentSnapshot doc = task.getResult();
StringBuilder fields = new StringBuilder("");
fields.append("Name: ").append(doc.get("Name"));
fields.append("\nEmail: ").append(doc.get("Email"));
private void UpdateData() {
DocumentReference contact = db.collection("PhoneBook").document("Contacts");
contact.update(NAME_KEY, "Kenny");
contact.update(EMAIL_KEY, "[email protected]");
contact.update(PHONE_KEY, "090-911-419")
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity.this, "Updated Successfully",
Toast.LENGTH_SHORT).show();
private void addRealtimeUpdate() {
DocumentReference contactListener = db.collection("PhoneBook").document("Contacts");
contactListener.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreException e) {
if (e != null ){
Log.d("ERROR", e.getMessage());
return;
}
if (documentSnapshot != null && documentSnapshot.exists()){
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
android:layout_margin="16dp"
tools:context="com.example.ekene.firestore.MainActivity">
public class MainActivity extends AppCompatActivity {
private static final String NAME_KEY = "Name";
private static final String EMAIL_KEY = "Email";
private static final String PHONE_KEY = "Phone";
FirebaseFirestore db;
TextView textDisplay;
TextView message;
EditText name, email, phone;
Button save;
private void addNewContact(){
save = findViewById(R.id.save);
name = findViewById(R.id.name);
email = findViewById(R.id.email);
phone = findViewById(R.id.phone);
String mName = name.getText().toString();
String mEmail = email.getText().toString();
String mPhone = phone.getText().toString();
private void addRealtimeUpdate() {
DocumentReference contactListener = db.collection("PhoneBook").document("Contacts");
contactListener.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreException e) {
if (e != null ){
Log.d("ERROR", e.getMessage());
return;
}
if (documentSnapshot != null && documentSnapshot.exists()){
compile 'com.android.support:cardview-v7:26.0.+'
compile 'com.squareup.picasso:picasso:2.5.2'
compile group: 'com.cloudinary', name: 'cloudinary-android', version: '1.20.0'