Advertisement
Guest User

MyChitChat

a guest
Aug 30th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.84 KB | None | 0 0
  1. activity_main.xml
  2. ===================
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:orientation="vertical">
  7.  
  8.     <LinearLayout
  9.         android:layout_width="match_parent"
  10.         android:layout_height="wrap_content"
  11.         android:orientation="horizontal">
  12.  
  13.         <EditText
  14.             android:layout_width="match_parent"
  15.             android:layout_height="wrap_content"
  16.             android:hint="enter your msg"
  17.             android:layout_weight="1"
  18.             android:id="@+id/txtMsg"/>
  19.         <Button
  20.             android:layout_width="match_parent"
  21.             android:layout_height="wrap_content"
  22.             android:layout_weight="3"
  23.             android:text="send"
  24.             android:background="#009fff"
  25.             android:textColor="#fff"
  26.             android:textSize="22sp"
  27.             android:id="@+id/btnSend"/>
  28.     </LinearLayout>
  29.     <ListView
  30.         android:layout_width="match_parent"
  31.         android:layout_height="match_parent"
  32.         android:id="@+id/lstMsg"
  33.         android:layout_weight="1"/>
  34.     <LinearLayout
  35.         android:layout_width="match_parent"
  36.         android:layout_height="wrap_content"
  37.         android:orientation="horizontal">
  38.         <EditText
  39.             android:layout_width="match_parent"
  40.             android:layout_height="wrap_content"
  41.             android:hint="enter user name"
  42.             android:layout_weight="1"
  43.             android:id="@+id/txtUserName"/>
  44.         <Button
  45.             android:layout_width="match_parent"
  46.             android:layout_height="wrap_content"
  47.             android:layout_weight="2"
  48.             android:text="add"
  49.             android:background="#009fff"
  50.             android:textColor="#fff"
  51.             android:textSize="16sp"
  52.             android:id="@+id/btnAddUser"
  53.             android:layout_margin="3dp"/>
  54.         <Button
  55.             android:layout_width="match_parent"
  56.             android:layout_height="wrap_content"
  57.             android:layout_weight="2"
  58.             android:text="list"
  59.             android:background="#009fff"
  60.             android:textColor="#fff"
  61.             android:textSize="16sp"
  62.             android:id="@+id/btnUserList"
  63.             android:layout_margin="3dp"/>
  64.     </LinearLayout>
  65. </LinearLayout>
  66.  
  67.  
  68. MainActivity.java
  69. =====================
  70. package com.example.hackeru.mychitchat;
  71.  
  72. import android.content.Context;
  73. import android.support.annotation.NonNull;
  74. import android.support.v7.app.AppCompatActivity;
  75. import android.os.Bundle;
  76. import android.util.Log;
  77. import android.view.View;
  78. import android.widget.EditText;
  79. import android.widget.ListView;
  80. import android.widget.Toast;
  81.  
  82. import com.google.firebase.database.DataSnapshot;
  83. import com.google.firebase.database.DatabaseError;
  84. import com.google.firebase.database.DatabaseReference;
  85. import com.google.firebase.database.FirebaseDatabase;
  86. import com.google.firebase.database.ValueEventListener;
  87.  
  88. import java.util.ArrayList;
  89. import java.util.List;
  90. import java.util.UUID;
  91.  
  92. public class MainActivity extends AppCompatActivity {
  93.  
  94.     ListView myMsgList;
  95.     EditText myMsg,txtUser;
  96.     Context context;
  97.     List<String> msgList;
  98.     final String REF_ID="message";
  99.  
  100.     @Override
  101.     protected void onCreate(Bundle savedInstanceState) {
  102.         super.onCreate(savedInstanceState);
  103.         setContentView(R.layout.activity_main);
  104.         setPointer();
  105.         setMessageListener();
  106.     }
  107.  
  108.  
  109.     private void setPointer() {
  110.         this.context=this;
  111.         msgList=new ArrayList<>();
  112.         txtUser=findViewById(R.id.txtUserName);
  113.         myMsg=findViewById(R.id.txtMsg);
  114.         myMsgList=findViewById(R.id.lstMsg);
  115.         findViewById(R.id.btnSend).setOnClickListener(new View.OnClickListener() {
  116.             @Override
  117.             public void onClick(View v) {
  118.                 sendMessage();
  119.             }
  120.         });
  121.         findViewById(R.id.btnAddUser).setOnClickListener(new View.OnClickListener() {
  122.             @Override
  123.             public void onClick(View v) {
  124.                 addUser();
  125.             }
  126.         });
  127.         findViewById(R.id.btnUserList).setOnClickListener(new View.OnClickListener() {
  128.             @Override
  129.             public void onClick(View v) {
  130.                 getUserList();
  131.             }
  132.         });
  133.     }
  134.  
  135.     private void getUserList() {
  136.         //creating instance to the database
  137.         FirebaseDatabase database=FirebaseDatabase.getInstance();
  138.         //creating a reference to the database
  139.         DatabaseReference userRef = database.getReference("Users");
  140.         //adding an event listener for getting the information back
  141.         userRef.addListenerForSingleValueEvent(new ValueEventListener() {
  142.             @Override
  143.             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  144.                 //moving on entire data that we recived from the database
  145.                 String allUsers="";
  146.                 for (DataSnapshot item:dataSnapshot.getChildren())
  147.                 {
  148.                     //get a single value, and add to our string.
  149.                     Users reciviedUser = item.getValue(Users.class);
  150.                     allUsers+=reciviedUser.userName+" ";
  151.                 }
  152.                 Toast.makeText(context, allUsers, Toast.LENGTH_LONG).show();
  153.                 Log.e("Immanual", "onDataChange: "+allUsers );
  154.             }
  155.  
  156.             @Override
  157.             public void onCancelled(@NonNull DatabaseError databaseError) {
  158.  
  159.             }
  160.         });
  161.     }
  162.  
  163.     private void addUser() {
  164.         String userName = txtUser.getText().toString();
  165.         txtUser.setText("");
  166.         Users user = new Users(userName, UUID.randomUUID().toString(),"12345","HackerU");
  167.         user.saveData();
  168.  
  169.     }
  170.  
  171.     private void sendMessage() {
  172.         String msg = myMsg.getText().toString();
  173.         myMsg.setText("");
  174.         //fire base will give us singleton of his instance
  175.  
  176.         //creating an instance to the database
  177.         FirebaseDatabase database = FirebaseDatabase.getInstance();
  178.         //creating a referance to message object
  179.         DatabaseReference msgRef = database.getReference(REF_ID);
  180.         //set value to the database
  181.         msgRef.setValue(msg);
  182.     }
  183.  
  184.     private void setMessageListener() {
  185.         //create an instance to the database
  186.         FirebaseDatabase database = FirebaseDatabase.getInstance();
  187.         //create a referance to the database
  188.         DatabaseReference msgRef = database.getReference(REF_ID);
  189.         //create an event listner with callback to our Reference
  190.         msgRef.addValueEventListener(new ValueEventListener() {
  191.             @Override
  192.             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  193.                 //this method is called once with the initial value and again
  194.                 //whenever data at this location is updated
  195.                 String value=dataSnapshot.getValue(String.class);
  196.                 msgList.add(0,value);
  197.                 ChatAdapter myAdapter = new ChatAdapter(context,msgList);
  198.                 myMsgList.setAdapter(myAdapter);
  199.             }
  200.  
  201.             @Override
  202.             public void onCancelled(@NonNull DatabaseError databaseError) {
  203.  
  204.             }
  205.         });
  206.     }
  207.  
  208. }
  209.  
  210.  
  211. Users.java
  212. ================
  213. package com.example.hackeru.mychitchat;
  214.  
  215. import android.net.Uri;
  216. import android.util.Log;
  217.  
  218. import com.google.android.gms.tasks.OnSuccessListener;
  219. import com.google.firebase.database.DatabaseReference;
  220. import com.google.firebase.database.FirebaseDatabase;
  221. import com.google.firebase.storage.FirebaseStorage;
  222. import com.google.firebase.storage.StorageReference;
  223. import com.google.firebase.storage.UploadTask;
  224.  
  225. import java.io.File;
  226.  
  227. public class Users {
  228.  
  229.     public String userName;
  230.     public String userId;
  231.     public String userPass;
  232.     public String userLocation;
  233.     public String userImg;
  234.  
  235.     public Users(String userName, String userId, String userPass, String userLocation) {
  236.         this.userName = userName;
  237.         this.userId = userId;
  238.         this.userPass = userPass;
  239.         this.userLocation=userLocation;
  240.     }
  241.  
  242.     public Users(){}
  243.  
  244.     public void saveData(String uri)
  245.     {
  246.         if (uri==null){
  247.             uri="";
  248.         }
  249.         //create connection blat
  250.         FirebaseDatabase database = FirebaseDatabase.getInstance();
  251.         DatabaseReference userRef = database.getReference("Users");
  252.         Users savedUser = new Users(this.userName,this.userId,this.userPass,this.userLocation);
  253.         savedUser.userImg=uri;
  254.         //save the data
  255.         userRef.child(userId).setValue(savedUser);
  256.     }
  257.  
  258.     public void savePic(String fileLocation, String folder)
  259.     {
  260.         //check if bucket exists in GCS, if not create one with S3
  261.         StorageReference storageReference = FirebaseStorage.getInstance().getReference();
  262.         //create the file in the memory.
  263.         Uri file = Uri.fromFile(new File(fileLocation));
  264.         //ref to the folder (bucket)
  265.         final StorageReference fileRef = storageReference.child(folder);
  266.         //let's put the file in the bucket.
  267.         fileRef.putFile(file).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
  268.             @Override
  269.             public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
  270.                 fileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
  271.                     @Override
  272.                     public void onSuccess(Uri uri) {
  273.                         Uri downloadURL = uri;
  274.                         Log.e("FireBase", "onSuccess: "+downloadURL);
  275.                         //https://firebasestorage.googleapis.com/v0/b/mychitchat-4a003.appspot.com/o/Moti%2Fimg.jpg?alt=media&token=8d1e1061-ced8-4eeb-9acd-d77243445b27
  276.                         saveData(uri.toString());
  277.                     }
  278.                 });
  279.             }
  280.         });
  281.  
  282.     }
  283. }
  284.  
  285.  
  286. ChatAdapter.java
  287. ==================
  288. package com.example.hackeru.mychitchat;
  289.  
  290. import android.content.Context;
  291. import android.view.View;
  292. import android.view.ViewGroup;
  293. import android.widget.BaseAdapter;
  294. import android.widget.TextView;
  295.  
  296. import java.util.List;
  297.  
  298. public class ChatAdapter extends BaseAdapter {
  299.     Context context;
  300.     List<String> msgList;
  301.  
  302.     public ChatAdapter(Context context, List<String> msgList) {
  303.         this.context = context;
  304.         this.msgList = msgList;
  305.     }
  306.  
  307.     @Override
  308.     public int getCount() {
  309.         return msgList.size();
  310.     }
  311.  
  312.     @Override
  313.     public Object getItem(int position) {
  314.         return null;
  315.     }
  316.  
  317.     @Override
  318.     public long getItemId(int position) {
  319.         return 0;
  320.     }
  321.  
  322.     @Override
  323.     public View getView(int position, View convertView, ViewGroup parent) {
  324.         TextView txt = new TextView(context);
  325.         txt.setText(msgList.get(position));
  326.         return txt;
  327.     }
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement