Android and Firebase

Firebase offers you the tools and infrastructure you need to build better apps and grow successful businesses. Firebase stores and syncs the data (json format) in real time so on your android app you will have the latest data without doing a new request.

This platform assures also the persistence of the data so even the app is offline you will have the data on your device.

Firebase has a lot of new features and these features are able to work independently, and they work even better together.

1. Use your gmail account on the Firebase website: https://console.firebase.google.com

2. Create a new project

3. Select the Database option from the left and go to the Rules tab and add these rules:

{
"rules": {
".read": true,
".write": true
}
}

4. Create a new Android project

5. Add this permission on your manifest:    

<uses-permission android:name="android.permission.INTERNET" />

6. Add in gradle (project level) this library:


classpath 'com.google.gms:google-services:3.0.0'

7. Add in gradle (module level) these libraries:

apply plugin: 'com.google.gms.google-services'
compile 'com.firebase:firebase-client-android:2.4.0'

8. Create a new class in your android project and that class will contain the firebase url:

public class Config {
public static final String FIREBASE_URL = "url";
}

9. Create a model class called User with name and address.

public class User {
private String name;
private String address;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}

10. Create a new layout in order to add users in Firebase.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name" />

<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Address" />

<EditText
android:id="@+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/buttonSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ADD" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Users"
android:textStyle="bold" />

<TextView
android:id="@+id/textViewPersons"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

11. Create an instance for Firebase connection


//Creating firebase object
Firebase ref = new Firebase(Config.FIREBASE_URL);

12. Send the data to the Firebase

//Getting values to store
String name = editTextName.getText().toString().trim();
String address = editTextAddress.getText().toString().trim();

//Creating User object
User user = new User();

//Adding values
user.setName(name);
user.setAddress(address);

//Storing values to firebase
ref.child(”User”).setValue(user);

13. Obtain data from Firebase

//Value event listener for realtime data update
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
//Getting the data from snapshot
User user = postSnapshot.getValue(User.class);

//Adding it to a string
String string = "Name: " + user.getName() + "\nAddress: " + user.getAddress() + "\n\n";

//Displaying it on textview
textViewPersons.setText(string);
}
}

@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});

14. Run your app 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s