Saturday, March 4, 2017

android - Firebase Realtime database example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#bcbcbc"
    >
    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="top|center_horizontal"
        android:layout_margin="50dp"
        >
        <Button
            android:id="@+id/btn_red"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="#FF0000"
            android:textColor="#FF0000"
            />
        <Button
            android:id="@+id/btn_green"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="#00FF00"
            android:textColor="#00FF00"
            />
        <Button
            android:id="@+id/btn_blue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="#0000FF"
            android:textColor="#0000FF"
            />
    </LinearLayout>
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="30sp"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;


public class MainActivity extends AppCompatActivity{
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private TextView mTextView;
    private Button mButtonRed;
    private Button mButtonGreen;
    private Button mButtonBlue;

    // Get the reference of firebase real time database
    DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
    // Get the reference of firebase real time database child layout_color
    DatabaseReference mLayoutColorRef = mRootRef.child("layout_color");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mTextView = (TextView) findViewById(R.id.tv);
        mButtonRed = (Button) findViewById(R.id.btn_red);
        mButtonGreen = (Button) findViewById(R.id.btn_green);
        mButtonBlue = (Button) findViewById(R.id.btn_blue);
    }

    @Override
    protected void onStart(){
        super.onStart();

        // Catch the firebase database change on real time
        mLayoutColorRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // Do something when data changed on firebase real time database
                String colorString = dataSnapshot.getValue(String.class);

                // Change the layout background color
                mCLayout.setBackgroundColor(Color.parseColor(colorString));
                // Also display the changed value on text view
                mTextView.setText(colorString);

                Snackbar.make(mCLayout,"Value received : " + colorString,
                        Snackbar.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        // Update the fire base database value on real time
        mButtonRed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Change the firebase database value on real time
                mLayoutColorRef.setValue(mButtonRed.getText());

                Toast.makeText(mContext,"Value changed : "
                        + mButtonRed.getText(),Toast.LENGTH_LONG).show();
            }
        });
        mButtonGreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Change the firebase database value on real time
                mLayoutColorRef.setValue(mButtonGreen.getText());

                Toast.makeText(mContext,"Value changed : "
                        + mButtonGreen.getText(),Toast.LENGTH_LONG).show();
            }
        });
        mButtonBlue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Change the firebase database value on real time
                mLayoutColorRef.setValue(mButtonBlue.getText());

                Toast.makeText(mContext,"Value changed : "
                        + mButtonBlue.getText(),Toast.LENGTH_LONG).show();
            }
        });
    }
}
build.gradle dependencies (app level)

compile 'com.google.firebase:firebase-database:10.2.0'