Hello friends, welcome to this samset.blogspot.com.
Today we learn a very interesting point so Do you know about Firebase. No don't worry i telling you in this session what is Firebase and how to use and configure this tool.
What is Firebase ?
Firebase is a platform for building mobile and web application. You can build application quickly with real time data update. Using firebase is very easy and it stores data in JSON format. You do not need to configure your server when you use firebase. Every thing will be handled by firebase automatically. So no coding on server side. It will save time and will make you more productive.
No need to write codes such as volley or retrofit to connect with DB server. It Simplifies everything.
Why use Firebase ?
1 : Super easy and quick to implement.
2 : No server side configuration needed. No PHP Scripts and No Database Designs.
3 : Realtime update without using GCM.
4 : Stores data in JSON format.
5 : No need to use Volley, retrofit.
How to use Firebase ?
Needs some Technical knowledge to handle firebase dashboard. Because, it stores data in JSON format.
Configure Firebase account
1 : First you need to create a firebase account.
2 : When you click this link see this screen.
4 : Just go to Firebase and Signup for a free plan. The Free Plan gives upto 1GB space. You can simply sign in with google.
3 : After signing in you will see your dashboard. Here you need to create an app. Just click on create
a new app put the app name and URL and click on Create a New App.
if your firebase deprecated then so this screen
if your firebase deprecated then so this screen
4 : Now click on the app you created and you will see your app’s dashboard. Here you will see your data.
5 : Now you click Add Firebase to your android app. Now you see below screen and enter your android sample packege name and SH1 hash code.
5 : Copy the URL you specified at the time of creating your application, You will need this in Your Android Project..
5 : Copy the URL you specified at the time of creating your application, You will need this in Your Android Project..
Now you create new project in Android studio and follow indstructions..
And now see example
actvity_main.xml
<?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"
tools:context="com.samset.firebaseappexample.MainActivity">
<TextView
android:textSize="20dp"
android:text="Registration"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_marginTop="5dp"
android:textSize="20dp"
android:id="@+id/etfname"
android:hint="FName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_marginTop="5dp"
android:textSize="20dp"
android:id="@+id/etlname"
android:hint="LName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_marginTop="5dp"
android:textSize="20dp"
android:id="@+id/etemail"
android:hint="Email"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_marginTop="5dp"
android:textSize="20dp"
android:id="@+id/etcontact"
android:hint="Contact"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_marginTop="5dp"
android:textSize="25dp"
android:id="@+id/btnSave"
android:hint="Save"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<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"
tools:context="com.samset.firebaseappexample.MainActivity">
<TextView
android:textSize="20dp"
android:text="Registration"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_marginTop="5dp"
android:textSize="20dp"
android:id="@+id/etfname"
android:hint="FName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_marginTop="5dp"
android:textSize="20dp"
android:id="@+id/etlname"
android:hint="LName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_marginTop="5dp"
android:textSize="20dp"
android:id="@+id/etemail"
android:hint="Email"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_marginTop="5dp"
android:textSize="20dp"
android:id="@+id/etcontact"
android:hint="Contact"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_marginTop="5dp"
android:textSize="25dp"
android:id="@+id/btnSave"
android:hint="Save"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
After create new project go to Project Structure and add firebase library
now you click left Cloud tab
and enable checkbox and click OK ,now configure firebase in your android studio.
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.samset.firebaseappexample"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE-FIREBASE.txt'
exclude 'META-INF/NOTICE'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0-alpha2'
compile 'com.firebase:firebase-client-android:2.3.1'
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.samset.firebaseappexample"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE-FIREBASE.txt'
exclude 'META-INF/NOTICE'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0-alpha2'
compile 'com.firebase:firebase-client-android:2.3.1'
}
ActvityMain.java
package com.samset.firebaseappexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.client.ChildEventListener;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.samset.firebaseappexample.adapter.UserAdapter;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
// this link is very useful for the firebase intraction db
private static final String URL="https://crackling-inferno-7719.firebaseio.com";
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.client.ChildEventListener;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.samset.firebaseappexample.adapter.UserAdapter;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
// this link is very useful for the firebase intraction db
private static final String URL="https://crackling-inferno-7719.firebaseio.com";
private ListView listView;
private Button btnsave,btnget;
private EditText fname,lname,email,contect;
// Initilize firebase class
Firebase mRef;
String FNAME,LNAME,MAIL,CONTACT;
private ArrayList<User> listdata=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fname=(EditText)findViewById(R.id.etfname);
lname=(EditText)findViewById(R.id.etlname);
email=(EditText)findViewById(R.id.etemail);
contect=(EditText)findViewById(R.id.etcontact);
listView=(ListView)findViewById(R.id.list);
btnsave=(Button)findViewById(R.id.btnSave);
}
@Override
protected void onStart() {
super.onStart();
Firebase mRef;
String FNAME,LNAME,MAIL,CONTACT;
private ArrayList<User> listdata=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fname=(EditText)findViewById(R.id.etfname);
lname=(EditText)findViewById(R.id.etlname);
email=(EditText)findViewById(R.id.etemail);
contect=(EditText)findViewById(R.id.etcontact);
listView=(ListView)findViewById(R.id.list);
btnsave=(Button)findViewById(R.id.btnSave);
}
@Override
protected void onStart() {
super.onStart();
// pass url in firebase constructor
mRef=new Firebase(URL);
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveData();
}
});
showData();
}
//if below code not work then use this code for show data
/*
private void showData()
{
// get all data from firebase database
mRef=new Firebase(URL);
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveData();
}
});
showData();
}
//if below code not work then use this code for show data
/*
private void showData2()
{
mRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
//Getting the data from snapshot
User person = postSnapshot.getValue(User.class);
listdata.add(person);
//Adding it to a string
String string = "Name: " + person.getFirstname() + "\nAddress: " + person.getContact() + "\n\n";
Log.e("Main", "data dd " + string);
if (listdata.size()>0)
{
UserAdapter userAdapter=new UserAdapter(MainActivity.this,listdata);
listView.setAdapter(userAdapter);
}else
{
Toast.makeText(MainActivity.this,"No Data",Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
//Getting the data from snapshot
User person = postSnapshot.getValue(User.class);
listdata.add(person);
//Adding it to a string
String string = "Name: " + person.getFirstname() + "\nAddress: " + person.getContact() + "\n\n";
Log.e("Main", "data dd " + string);
if (listdata.size()>0)
{
UserAdapter userAdapter=new UserAdapter(MainActivity.this,listdata);
listView.setAdapter(userAdapter);
}else
{
Toast.makeText(MainActivity.this,"No Data",Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
} */
{
mRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
//Getting the data from snapshot
User person = postSnapshot.getValue(User.class);
listdata.add(person);
//Adding it to a string
String string = "Name: " + person.getFirstname() + "\nAddress: " + person.getContact() + "\n\n";
Log.e("Main", "data dd " + string);
if (listdata.size()>0)
{
UserAdapter userAdapter=new UserAdapter(MainActivity.this,listdata);
listView.setAdapter(userAdapter);
}else
{
Toast.makeText(MainActivity.this,"No Data",Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
//Getting the data from snapshot
User person = postSnapshot.getValue(User.class);
listdata.add(person);
//Adding it to a string
String string = "Name: " + person.getFirstname() + "\nAddress: " + person.getContact() + "\n\n";
Log.e("Main", "data dd " + string);
if (listdata.size()>0)
{
UserAdapter userAdapter=new UserAdapter(MainActivity.this,listdata);
listView.setAdapter(userAdapter);
}else
{
Toast.makeText(MainActivity.this,"No Data",Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
} */
private void showData()
{
// get all data from firebase database
// you want create a node like userdata
mRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
updateData(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
updateData(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
public void updateData(DataSnapshot snapshot)
{
listdata.clear();
for (DataSnapshot data:snapshot.getChildren())
{
User user=new User();
user.setFirstname(data.getValue(User.class).firstname);
user.setLastname(data.getValue(User.class).lastname);
user.setEmail(data.getValue(User.class).email);
user.setContact(data.getValue(User.class).contact);
listdata.add(user);
}
if (listdata.size()>0)
{
UserAdapter userAdapter=new UserAdapter(MainActivity.this,listdata);
listView.setAdapter(userAdapter);
}else
{
Toast.makeText(MainActivity.this,"No Data",Toast.LENGTH_SHORT).show();
}
}
private void saveData()
{
FNAME=fname.getText().toString();
LNAME=lname.getText().toString();
MAIL=email.getText().toString();
CONTACT=contect.getText().toString();
if (FNAME.isEmpty())
{
Toast.makeText(this,"Fill FirstName",Toast.LENGTH_SHORT).show();
}else if (FNAME.isEmpty() && LNAME.isEmpty())
{
Toast.makeText(this,"Fill LastName",Toast.LENGTH_SHORT).show();
}else if (!FNAME.isEmpty() && !LNAME.isEmpty() && email.getText().toString().isEmpty())
{
Toast.makeText(this,"Fill Email",Toast.LENGTH_SHORT).show();
} else if (!FNAME.isEmpty() && !LNAME.isEmpty() && !MAIL.isEmpty() && CONTACT.isEmpty()) {
Toast.makeText(this, "Fill Contact",Toast.LENGTH_SHORT).show();
}else if (!FNAME.isEmpty() && !LNAME.isEmpty() && !MAIL.isEmpty() && !CONTACT.isEmpty())
{
User user=new User(FNAME,LNAME,MAIL,CONTACT);
// you want create a node like userdata by name UserInfo then add child
mRef.child("UserInfo").push().setValue(user);
fname.setText("");
lname.setText("");
email.setText("");
contect.setText("");
}
}
}
mRef.child("UserInfo").push().setValue(user);
fname.setText("");
lname.setText("");
email.setText("");
contect.setText("");
}
}
}
User.java
package com.samset.firebaseappexample;
/**
* Created by samset on 02/05/16.
*/
public class User {
public String firstname;
public String lastname;
public String email;
public String contact;
public User()
{
}
public User(String fname, String lanme, String mail, String con) {
this.firstname =fname;
this.lastname=lanme;
this.email=mail;
this.contact=con;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
}
/**
* Created by samset on 02/05/16.
*/
public class User {
public String firstname;
public String lastname;
public String email;
public String contact;
public User()
{
}
public User(String fname, String lanme, String mail, String con) {
this.firstname =fname;
this.lastname=lanme;
this.email=mail;
this.contact=con;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
}
UserAdapter.java
package com.samset.firebaseappexample.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.samset.firebaseappexample.R;
import com.samset.firebaseappexample.User;
import java.util.ArrayList;
/**
* Created by samset on 02/05/16.
*/
public class UserAdapter extends BaseAdapter {
ArrayList<User> list;
Context context;
public UserAdapter(Context ctx, ArrayList<User> data) {
this.list = data;
this.context = ctx;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
MyViewHolder myViewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.items, parent, false);
myViewHolder = new MyViewHolder(convertView);
convertView.setTag(myViewHolder);
} else {
myViewHolder = (MyViewHolder) convertView.getTag();
}
User user = list.get(position);
myViewHolder.fname.setText(user.firstname);
myViewHolder.lname.setText(user.lastname);
myViewHolder.mail.setText(user.email);
myViewHolder.contact.setText(user.contact);
return convertView;
}
public class MyViewHolder {
TextView fname, lname, mail, contact;
public MyViewHolder(View view) {
fname = (TextView) view.findViewById(R.id.tvfname);
lname = (TextView) view.findViewById(R.id.tvlname);
mail = (TextView) view.findViewById(R.id.tvemail);
contact = (TextView) view.findViewById(R.id.tvcontact);
}
}
}
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.samset.firebaseappexample.R;
import com.samset.firebaseappexample.User;
import java.util.ArrayList;
/**
* Created by samset on 02/05/16.
*/
public class UserAdapter extends BaseAdapter {
ArrayList<User> list;
Context context;
public UserAdapter(Context ctx, ArrayList<User> data) {
this.list = data;
this.context = ctx;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
MyViewHolder myViewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.items, parent, false);
myViewHolder = new MyViewHolder(convertView);
convertView.setTag(myViewHolder);
} else {
myViewHolder = (MyViewHolder) convertView.getTag();
}
User user = list.get(position);
myViewHolder.fname.setText(user.firstname);
myViewHolder.lname.setText(user.lastname);
myViewHolder.mail.setText(user.email);
myViewHolder.contact.setText(user.contact);
return convertView;
}
public class MyViewHolder {
TextView fname, lname, mail, contact;
public MyViewHolder(View view) {
fname = (TextView) view.findViewById(R.id.tvfname);
lname = (TextView) view.findViewById(R.id.tvlname);
mail = (TextView) view.findViewById(R.id.tvemail);
contact = (TextView) view.findViewById(R.id.tvcontact);
}
}
}
AndroidMainifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.samset.firebaseappexample" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".CrowdWeather"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.samset.firebaseappexample" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".CrowdWeather"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment