Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainActivity
- ======================
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.TextView;
- 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.HashMap;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- public class MainActivity extends AppCompatActivity {
- private Button add_user;
- private EditText room_name;
- private ListView listView;
- private ArrayAdapter<String> arrayAdapter;
- private ArrayList<String> list_of_users = new ArrayList<>();
- private String name;
- private DatabaseReference root = FirebaseDatabase.getInstance().getReference().getRoot();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- }
- private void setPointer() {
- add_user = (Button) findViewById(R.id.btn_add_room);
- room_name = (EditText) findViewById(R.id.room_name_edittext);
- listView = (ListView) findViewById(R.id.listView);
- arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list_of_users);
- listView.setAdapter(arrayAdapter);
- request_user_name();
- add_user.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Map<String, Object> map = new HashMap<String, Object>();
- map.put(room_name.getText().toString(), "");
- root.updateChildren(map);
- room_name.setText("");
- }
- });
- root.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- Set<String> set = new HashSet<String>();
- Iterator i = dataSnapshot.getChildren().iterator();
- while (i.hasNext()) {
- set.add(((DataSnapshot) i.next()).getKey());
- }
- list_of_users.clear();
- list_of_users.addAll(set);
- arrayAdapter.notifyDataSetChanged();
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- });
- listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
- Intent intent = new Intent(getApplicationContext(), Chat_Room.class);
- intent.putExtra("room_name", ((TextView) view).getText().toString());
- intent.putExtra("user_name", "(" + name + ") ");
- startActivity(intent);
- }
- });
- }
- private void request_user_name() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("Enter name:");
- final EditText input_field = new EditText(this);
- builder.setIcon(R.drawable.chat);
- builder.setView(input_field);
- builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- name = input_field.getText().toString();
- }
- });
- builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.cancel();
- request_user_name();
- }
- });
- builder.show();
- }
- }
- Chat_Users
- ====================
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import com.google.firebase.database.ChildEventListener;
- 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 java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- class Chat_Room extends AppCompatActivity{
- private Button btn_send_msg;
- private EditText input_msg;
- private TextView chat_conversation;
- private String user_name,room_name;
- private DatabaseReference root ;
- private String temp_key;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.chat_room);
- btn_send_msg = (Button) findViewById(R.id.btn_send);
- input_msg = (EditText) findViewById(R.id.msg_input);
- chat_conversation = (TextView) findViewById(R.id.textView);
- user_name = getIntent().getExtras().get("user_name").toString();
- room_name = getIntent().getExtras().get("room_name").toString();
- setTitle(" User - "+room_name);
- root = FirebaseDatabase.getInstance().getReference().child(room_name);
- btn_send_msg.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Map<String,Object> map = new HashMap<String, Object>();
- temp_key = root.push().getKey();
- root.updateChildren(map);
- DatabaseReference message_root = root.child(temp_key);
- Map<String,Object> map2 = new HashMap<String, Object>();
- map2.put("name",user_name);
- map2.put("msg",input_msg.getText().toString());
- input_msg.setText("");
- message_root.updateChildren(map2);
- }
- });
- root.addChildEventListener(new ChildEventListener() {
- @Override
- public void onChildAdded(DataSnapshot dataSnapshot, String s) {
- append_chat_conversation(dataSnapshot);
- }
- @Override
- public void onChildChanged(DataSnapshot dataSnapshot, String s) {
- append_chat_conversation(dataSnapshot);
- }
- @Override
- public void onChildRemoved(DataSnapshot dataSnapshot) {
- }
- @Override
- public void onChildMoved(DataSnapshot dataSnapshot, String s) {
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- });
- }
- private String chat_msg,chat_user_name;
- private void append_chat_conversation(DataSnapshot dataSnapshot) {
- Iterator i = dataSnapshot.getChildren().iterator();
- while (i.hasNext()){
- chat_msg = (String) ((DataSnapshot)i.next()).getValue();
- chat_user_name = (String) ((DataSnapshot)i.next()).getValue();
- chat_conversation.append(chat_user_name +" : "+chat_msg +" \n");
- }
- }
- }
Add Comment
Please, Sign In to add comment