activity_main.xml
<?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:padding="16dp"
tools:context="com.example.malik.firebaseappdemom.MainActivity">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter name" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter email" />
<EditText
android:id="@+id/contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Contact" />
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="Send" />
<Button
android:id="@+id/viewData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View Data" />
</LinearLayout>
MainActivity.java
package com.example.malik.firebaseappdemom;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity {
EditText name, email, contact;
Button send;
Button viewData;
DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.name);
email = (EditText) findViewById(R.id.email);
contact = (EditText) findViewById(R.id.contact);
send = (Button) findViewById(R.id.send);
viewData = (Button) findViewById(R.id.viewData);
viewData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,ViewData.class);
startActivity(intent);
}
});
databaseReference = FirebaseDatabase.getInstance().getReference("persons");
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addInfo();
}
});
}
private void addInfo(){
String NAME = name.getText().toString();
String EMAIL = email.getText().toString();
String CONTACT = contact.getText().toString();
if(TextUtils.isEmpty(NAME) || TextUtils.isEmpty(EMAIL) || TextUtils.isEmpty(CONTACT)){
Toast.makeText(getApplicationContext(),"Feel all data",Toast.LENGTH_LONG).show();
} else {
String id = databaseReference.push().getKey();
Person person = new Person(id,NAME,EMAIL,CONTACT);
databaseReference.child(id).setValue(person);
Toast.makeText(getApplicationContext(),"Data saved",Toast.LENGTH_LONG).show();
name.setText("");
email.setText("");
contact.setText("");
}
}
}
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/name_here"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Name here"
android:textSize="28sp" />
<TextView
android:id="@+id/email_here"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Email here"
android:textSize="28sp" />
<TextView
android:id="@+id/contact_here"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Contact here"
android:textSize="28sp" />
</LinearLayout>
Person.java
package com.example.malik.firebaseappdemom;
/**
* Created by malik on 27/07/2017.
*/
public class Person {
String id;
String name;
String email;
String contact;
public Person() {
}
public Person(String id, String name, String email, String contact) {
this.name = name;
this.email = email;
this.contact = contact;
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
}
PersonAdapter.java
package com.example.malik.firebaseappdemom;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by malik on 27/07/2017.
*/
public class PersonAdapter extends ArrayAdapter<Person> {
private Activity activity;
private List<Person> list;
public PersonAdapter(Activity activity, List<Person> list) {
super(activity, R.layout.custom_layout,list);
this.activity = activity;
this.list = list;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater layoutInflater = activity.getLayoutInflater();
View view = layoutInflater.inflate(R.layout.custom_layout,null,true);
TextView textViewName = (TextView) view.findViewById(R.id.name_here);
TextView textViewEmail = (TextView) view.findViewById(R.id.email_here);
TextView textViewContact = (TextView) view.findViewById(R.id.contact_here);
Person person = list.get(position);
textViewName.setText(person.getName());
textViewEmail.setText(person.getEmail());
textViewContact.setText(person.getContact());
return view;
}
}
activity_view_data.xml
<?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"
tools:context="com.example.malik.firebaseappdemom.ViewData">
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
ViewData.java
package com.example.malik.firebaseappdemom;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
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;
import java.util.ArrayList;
import java.util.List;
public class ViewData extends AppCompatActivity {
ListView listView;
DatabaseReference databaseReference;
List<Person> personList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_data);
listView = (ListView) findViewById(R.id.list1);
personList = new ArrayList<>();
databaseReference = FirebaseDatabase.getInstance().getReference("persons");
}
@Override
protected void onStart() {
super.onStart();
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
personList.clear();
for (DataSnapshot datas : dataSnapshot.getChildren()){
Person person = datas.getValue(Person.class);
personList.add(person);
}
PersonAdapter personAdapter = new PersonAdapter(ViewData.this,personList);
listView.setAdapter(personAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.malik.firebaseappdemom">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
<activity android:name=".ViewData"></activity>
</application>
</manifest>