This is the Part 8 of “Build a Social Media App on Android Studio” tutorial, and we are going to cover the following functionalities in this article:
- We are going to Show all the Registered Users of our App.
- We are going to show the list of users in UsersFragment.
Step By Step Implementation
Step 1: Create a new java class
Working with the ModelUsers.java file. Created this activity to initialize the key so that we can retrieve the value of the key later.
package com.example.socialmediaapp;
public class ModelUsers {
String name;
public ModelUsers() {
}
String onlineStatus;
String typingTo;
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public String getTypingTo() {
return typingTo;
}
public void setTypingTo(String typingTo) {
this .typingTo = typingTo;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this .email = email;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this .image = image;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this .uid = uid;
}
public ModelUsers(String name, String onlineStatus, String typingTo, String email, String image, String uid) {
this .name = name;
this .onlineStatus = onlineStatus;
this .typingTo = typingTo;
this .email = email;
this .image = image;
this .uid = uid;
}
String email;
String image;
String uid;
} |
Step 2: Create another new java class
Working with the AdapterUsers.java file.
package com.example.socialmediaapp;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.google.firebase.auth.FirebaseAuth;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
public class AdapterUsers extends RecyclerView.Adapter<AdapterUsers.MyHolder> {
Context context;
FirebaseAuth firebaseAuth;
String uid;
public AdapterUsers(Context context, List<ModelUsers> list) {
this .context = context;
this .list = list;
firebaseAuth = FirebaseAuth.getInstance();
uid = firebaseAuth.getUid();
}
List<ModelUsers> list;
@NonNull
@Override
public MyHolder onCreateViewHolder( @NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_users, parent, false );
return new MyHolder(view);
}
@Override
public void onBindViewHolder( @NonNull MyHolder holder, final int position) {
final String hisuid = list.get(position).getUid();
String userImage = list.get(position).getImage();
String username = list.get(position).getName();
String usermail = list.get(position).getEmail();
holder.name.setText(username);
holder.email.setText(usermail);
try {
Glide.with(context).load(userImage).into(holder.profiletv);
} catch (Exception e) {
}
}
@Override
public int getItemCount() {
return list.size();
}
class MyHolder extends RecyclerView.ViewHolder {
CircleImageView profiletv;
TextView name, email;
public MyHolder( @NonNull View itemView) {
super (itemView);
profiletv = itemView.findViewById(R.id.imagep);
name = itemView.findViewById(R.id.namep);
email = itemView.findViewById(R.id.emailp);
}
}
} |
Step 3: Working with the fragment_user.xml file
Navigate to the app > res > layout > fragment_user.xml and add the below code to that file. Below is the code for the fragment_user.xml file.
<? xml version = "1.0" encoding = "utf-8" ?>
< FrameLayout android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".UsersFragment" >
<!-- TODO: Update blank fragment layout -->
< androidx.recyclerview.widget.RecyclerView
android:id = "@+id/recyclep"
android:layout_width = "match_parent"
android:layout_height = "match_parent" />
</ FrameLayout >
|
Step 4: Working with the row_users.xml file
Create a new layout resource file and name the file as row_users. Below is the code for the row_users.xml file.
<? xml version = "1.0" encoding = "utf-8" ?>
< androidx.cardview.widget.CardView android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:orientation = "vertical"
app:cardBackgroundColor = "@color/colorWhite"
app:cardCornerRadius = "2dp"
app:cardElevation = "2dp"
app:cardUseCompatPadding = "true"
app:contentPadding = "3dp" >
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:orientation = "horizontal" >
< de.hdodenhof.circleimageview.CircleImageView
android:id = "@+id/imagep"
android:layout_width = "70dp"
android:layout_height = "70dp"
android:src = "@drawable/profile_image" />
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_marginLeft = "5dp"
android:layout_weight = "1"
android:orientation = "vertical" >
< TextView
android:id = "@+id/namep"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "Person NAme"
android:textColor = "@color/colorBlack"
android:textSize = "18sp" />
< TextView
android:id = "@+id/emailp"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "Person Email"
android:textColor = "@color/colorBlack" />
</ LinearLayout >
</ LinearLayout >
</ androidx.cardview.widget.CardView >
|
Step 5: Working with the UsersFragmnet.java file
Go to the UsersFragmnet.java file and refer to the following code. Below is the code for the UsersFragmnet.java file.
package com.example.socialmediaapp;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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;
/** * A simple {@link Fragment} subclass.
*/
public class UsersFragment extends Fragment {
RecyclerView recyclerView;
AdapterUsers adapterUsers;
List<ModelUsers> usersList;
FirebaseAuth firebaseAuth;
public UsersFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_users, container, false );
recyclerView = view.findViewById(R.id.recyclep);
recyclerView.setHasFixedSize( true );
recyclerView.setLayoutManager( new LinearLayoutManager(getActivity()));
usersList = new ArrayList<>();
firebaseAuth = FirebaseAuth.getInstance();
getAllUsers();
return view;
}
private void getAllUsers() {
final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference( "Users" );
reference.addValueEventListener( new ValueEventListener() {
@Override
public void onDataChange( @NonNull DataSnapshot dataSnapshot) {
usersList.clear();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
ModelUsers modelUsers = dataSnapshot1.getValue(ModelUsers. class );
if (modelUsers.getUid() != null && !modelUsers.getUid().equals(firebaseUser.getUid())) {
usersList.add(modelUsers);
}
adapterUsers = new AdapterUsers(getActivity(), usersList);
recyclerView.setAdapter(adapterUsers);
}
}
@Override
public void onCancelled( @NonNull DatabaseError databaseError) {
}
});
}
@Override
public void onCreate( @Nullable Bundle savedInstanceState) {
setHasOptionsMenu( true );
super .onCreate(savedInstanceState);
}
} |
Output:
For all the drawable file used in this article please refer to this link: https://drive.google.com/drive/folders/1M_knOH_ugCuwSP5nkYzeD4dRp-Honzbe?usp=sharing
Below is the file structure after performing these operations: