Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- activity_main.xml
- =======================
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout 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"
- >
- <EditText
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/room_name_edittext"
- android:layout_alignParentBottom="true"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_toLeftOf="@+id/btn_add_room"
- android:layout_toStartOf="@+id/btn_add_room" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Add user"
- android:id="@+id/btn_add_room"
- android:layout_alignParentBottom="true"
- android:layout_alignParentRight="true"
- android:layout_alignParentEnd="true" />
- <ListView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/listView"
- android:layout_alignParentTop="true"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_above="@+id/room_name_edittext" />
- </RelativeLayout>
- MainActivity.java
- ===========================
- 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_room;
- private EditText room_name;
- private ListView listView;
- private ArrayAdapter<String> arrayAdapter;
- private ArrayList<String> list_of_rooms = 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);
- add_room = (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_rooms);
- listView.setAdapter(arrayAdapter);
- request_user_name();
- add_room.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_rooms.clear();
- list_of_rooms.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_Room.java
- ==========================
- 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;
- /**
- * Created by arabi_000 on 11/27/2017.
- */
- 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(" Room - "+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");
- }
- }
- }
- chat_room.xml
- =====================
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent" android:layout_height="match_parent">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Send"
- android:id="@+id/btn_send"
- android:layout_alignParentBottom="true"
- android:layout_alignParentRight="true"
- android:layout_alignParentEnd="true" />
- <EditText
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/msg_input"
- android:layout_alignTop="@+id/btn_send"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_toLeftOf="@+id/btn_send"
- android:layout_toStartOf="@+id/btn_send" />
- <ScrollView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/scrollView"
- android:layout_alignParentTop="true"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_above="@+id/btn_send"
- android:layout_alignRight="@+id/btn_send"
- android:layout_alignEnd="@+id/btn_send">
- <TextView
- android:layout_width="match_parent"
- android:padding="15dp"
- android:layout_height="match_parent"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:id="@+id/textView" />
- </ScrollView>
- </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement