Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- activity_main.xml
- ===================
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="enter your msg"
- android:layout_weight="1"
- android:id="@+id/txtMsg"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="3"
- android:text="send"
- android:background="#009fff"
- android:textColor="#fff"
- android:textSize="22sp"
- android:id="@+id/btnSend"/>
- </LinearLayout>
- <ListView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/lstMsg"
- android:layout_weight="1"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="enter user name"
- android:layout_weight="1"
- android:id="@+id/txtUserName"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="2"
- android:text="add"
- android:background="#009fff"
- android:textColor="#fff"
- android:textSize="16sp"
- android:id="@+id/btnAddUser"
- android:layout_margin="3dp"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="2"
- android:text="list"
- android:background="#009fff"
- android:textColor="#fff"
- android:textSize="16sp"
- android:id="@+id/btnUserList"
- android:layout_margin="3dp"/>
- </LinearLayout>
- </LinearLayout>
- MainActivity.java
- =====================
- package com.example.hackeru.mychitchat;
- import android.content.Context;
- import android.support.annotation.NonNull;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.Toast;
- 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;
- import java.util.UUID;
- public class MainActivity extends AppCompatActivity {
- ListView myMsgList;
- EditText myMsg,txtUser;
- Context context;
- List<String> msgList;
- final String REF_ID="message";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- setMessageListener();
- }
- private void setPointer() {
- this.context=this;
- msgList=new ArrayList<>();
- txtUser=findViewById(R.id.txtUserName);
- myMsg=findViewById(R.id.txtMsg);
- myMsgList=findViewById(R.id.lstMsg);
- findViewById(R.id.btnSend).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- sendMessage();
- }
- });
- findViewById(R.id.btnAddUser).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- addUser();
- }
- });
- findViewById(R.id.btnUserList).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- getUserList();
- }
- });
- }
- private void getUserList() {
- //creating instance to the database
- FirebaseDatabase database=FirebaseDatabase.getInstance();
- //creating a reference to the database
- DatabaseReference userRef = database.getReference("Users");
- //adding an event listener for getting the information back
- userRef.addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
- //moving on entire data that we recived from the database
- String allUsers="";
- for (DataSnapshot item:dataSnapshot.getChildren())
- {
- //get a single value, and add to our string.
- Users reciviedUser = item.getValue(Users.class);
- allUsers+=reciviedUser.userName+" ";
- }
- Toast.makeText(context, allUsers, Toast.LENGTH_LONG).show();
- Log.e("Immanual", "onDataChange: "+allUsers );
- }
- @Override
- public void onCancelled(@NonNull DatabaseError databaseError) {
- }
- });
- }
- private void addUser() {
- String userName = txtUser.getText().toString();
- txtUser.setText("");
- Users user = new Users(userName, UUID.randomUUID().toString(),"12345","HackerU");
- user.saveData();
- }
- private void sendMessage() {
- String msg = myMsg.getText().toString();
- myMsg.setText("");
- //fire base will give us singleton of his instance
- //creating an instance to the database
- FirebaseDatabase database = FirebaseDatabase.getInstance();
- //creating a referance to message object
- DatabaseReference msgRef = database.getReference(REF_ID);
- //set value to the database
- msgRef.setValue(msg);
- }
- private void setMessageListener() {
- //create an instance to the database
- FirebaseDatabase database = FirebaseDatabase.getInstance();
- //create a referance to the database
- DatabaseReference msgRef = database.getReference(REF_ID);
- //create an event listner with callback to our Reference
- msgRef.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
- //this method is called once with the initial value and again
- //whenever data at this location is updated
- String value=dataSnapshot.getValue(String.class);
- msgList.add(0,value);
- ChatAdapter myAdapter = new ChatAdapter(context,msgList);
- myMsgList.setAdapter(myAdapter);
- }
- @Override
- public void onCancelled(@NonNull DatabaseError databaseError) {
- }
- });
- }
- }
- Users.java
- ================
- package com.example.hackeru.mychitchat;
- import android.net.Uri;
- import android.util.Log;
- import com.google.android.gms.tasks.OnSuccessListener;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- import com.google.firebase.storage.FirebaseStorage;
- import com.google.firebase.storage.StorageReference;
- import com.google.firebase.storage.UploadTask;
- import java.io.File;
- public class Users {
- public String userName;
- public String userId;
- public String userPass;
- public String userLocation;
- public String userImg;
- public Users(String userName, String userId, String userPass, String userLocation) {
- this.userName = userName;
- this.userId = userId;
- this.userPass = userPass;
- this.userLocation=userLocation;
- }
- public Users(){}
- public void saveData(String uri)
- {
- if (uri==null){
- uri="";
- }
- //create connection blat
- FirebaseDatabase database = FirebaseDatabase.getInstance();
- DatabaseReference userRef = database.getReference("Users");
- Users savedUser = new Users(this.userName,this.userId,this.userPass,this.userLocation);
- savedUser.userImg=uri;
- //save the data
- userRef.child(userId).setValue(savedUser);
- }
- public void savePic(String fileLocation, String folder)
- {
- //check if bucket exists in GCS, if not create one with S3
- StorageReference storageReference = FirebaseStorage.getInstance().getReference();
- //create the file in the memory.
- Uri file = Uri.fromFile(new File(fileLocation));
- //ref to the folder (bucket)
- final StorageReference fileRef = storageReference.child(folder);
- //let's put the file in the bucket.
- fileRef.putFile(file).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
- @Override
- public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
- fileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
- @Override
- public void onSuccess(Uri uri) {
- Uri downloadURL = uri;
- Log.e("FireBase", "onSuccess: "+downloadURL);
- //https://firebasestorage.googleapis.com/v0/b/mychitchat-4a003.appspot.com/o/Moti%2Fimg.jpg?alt=media&token=8d1e1061-ced8-4eeb-9acd-d77243445b27
- saveData(uri.toString());
- }
- });
- }
- });
- }
- }
- ChatAdapter.java
- ==================
- package com.example.hackeru.mychitchat;
- import android.content.Context;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.TextView;
- import java.util.List;
- public class ChatAdapter extends BaseAdapter {
- Context context;
- List<String> msgList;
- public ChatAdapter(Context context, List<String> msgList) {
- this.context = context;
- this.msgList = msgList;
- }
- @Override
- public int getCount() {
- return msgList.size();
- }
- @Override
- public Object getItem(int position) {
- return null;
- }
- @Override
- public long getItemId(int position) {
- return 0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- TextView txt = new TextView(context);
- txt.setText(msgList.get(position));
- return txt;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement