How to Build a Facebook Like Custom RecyclerView in Android?
Last Updated :
22 Jul, 2024
We have seen implementing RecyclerView in Android with simple data inside our app. In this article, we will take a look at the implementation of Facebook like Custom RecyclerView in Android.
What we are going to build in this article?
We will be building a simple application in which we will display the Facebook like RecyclerView in our Android app. We will be fetching this data from a simple API (https://jsonkeeper.com/b/OB3B) which is given in the article. With the help of this API, we will be displaying that data in our RecyclerView. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Add the below dependency in your build.gradle file Below is the dependency for Volley which we will be using to get the data from API. For adding this dependency navigate to the app > Gradle Scripts > build.gradle(app) and add the below dependency in the dependencies section.
// dependency for loading data from json file.
implementation ‘com.android.volley:volley:1.2.1’
// dependency for loading image from URL.
implementation ‘com.squareup.picasso:picasso:2.71828’
// dependency for creating a circle image.
implementation ‘de.hdodenhof:circleimageview:3.1.0’
Step 3: Adding internet permissions in your AndroidManifest.xml file
As we are loading data from the internet, for that, we will have to add the internet permissions to our AndroidManifest.xml file. Navigate to the app > AndroidManifest.xml file and add the below code to it.
XML
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Step 4: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
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"
tools:context=".MainActivity">
<!--recycler view for displaying
our Facebook posts-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/idRVInstaFeeds"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--progressbar for displaying
our loading indicator-->
<ProgressBar
android:id="@+id/idLoadingPB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible" />
</RelativeLayout>
Step 5: Creating a custom drawable file for our background image
Navigate to the app > res > drawable > Right-click on it > New > drawable resource file and name your file as background_drawable and add the below code to it for our custom background.
XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--radius for our shape-->
<corners android:radius="20dp" />
<!--background color for our shape-->
<solid android:color="#EDEDED" />
</shape>
Step 6: Creating a layout file for our item of RecyclerView
Navigate to the app > res > layout > Right-click on it > New > layout Resource file and name it as facebook_feed_rv_item and add the below code to it. Comments are added in the code to get to know in more detail.
Note: Icons used in this file are present in the drawable folder. Navigate to the app > res > drawable folder and add your icons to that folder.
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:elevation="8dp"
app:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/idLLTopBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dp">
<!--circle image for displaying the user image-->
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/idCVAuthor"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="5dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<!--text view for displaying user name-->
<TextView
android:id="@+id/idTVAuthorName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:padding="3dp"
android:text="geeks_for_geeks"
android:textColor="@color/black"
android:textStyle="bold" />
<!--textview for displaying post publish time-->
<TextView
android:id="@+id/idTVTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="time"
android:textColor="@color/black"
android:textSize="11sp" />
</LinearLayout>
</LinearLayout>
<!--text view for displaying post description-->
<TextView
android:id="@+id/idTVDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idLLTopBar"
android:layout_margin="5dp"
android:padding="5dp"
android:text="Description"
android:textColor="@color/black"
android:textSize="11sp" />
<!--image view to display the post image-->
<ImageView
android:id="@+id/idIVPost"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="@id/idTVDescription"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
<!--linear layout for displaying facebook actions-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idIVPost"
android:layout_margin="5dp"
android:orientation="horizontal"
android:padding="2dp"
android:weightSum="3">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/background_drawable"
android:orientation="horizontal"
android:weightSum="2">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:src="@drawable/ic_like" />
<TextView
android:id="@+id/idTVLikes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="4dp"
android:layout_weight="1"
android:background="@drawable/background_drawable"
android:drawablePadding="3dp"
android:padding="5dp"
android:text="likes"
android:textColor="@color/black"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/background_drawable"
android:orientation="horizontal"
android:weightSum="2">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:src="@drawable/ic_comment" />
<TextView
android:id="@+id/idTVComments"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:layout_weight="1"
android:drawablePadding="3dp"
android:padding="5dp"
android:text="12"
android:textColor="@color/black"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/idLLShare"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/background_drawable"
android:orientation="horizontal"
android:weightSum="2">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:src="@drawable/ic_share" />
<TextView
android:id="@+id/idTVShare"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="4dp"
android:layout_weight="1"
android:background="@drawable/background_drawable"
android:drawablePadding="3dp"
android:padding="5dp"
android:text="Share"
android:textColor="@color/black"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
Step 7: Creating a modal class for storing our data
Navigate to the app > java > your app’s package name > Right-click on it > New > Java class and name your class as FacebookFeedModal and add the below code to it.
Java
public class FacebookFeedModal {
// variables for storing our author image,
// author name, postDate,postDescription,
// post image,post likes,post comments.
private String authorImage;
private String authorName;
private String postDate;
private String postDescription;
private String postIV;
private String postLikes;
private String postComments;
// creating getter and setter methods.
public String getAuthorImage() {
return authorImage;
}
public void setAuthorImage(String authorImage) {
this.authorImage = authorImage;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public String getPostDate() {
return postDate;
}
public void setPostDate(String postDate) {
this.postDate = postDate;
}
public String getPostDescription() {
return postDescription;
}
public void setPostDescription(String postDescription) {
this.postDescription = postDescription;
}
public String getPostIV() {
return postIV;
}
public void setPostIV(String postIV) {
this.postIV = postIV;
}
public String getPostLikes() {
return postLikes;
}
public void setPostLikes(String postLikes) {
this.postLikes = postLikes;
}
public String getPostComments() {
return postComments;
}
public void setPostComments(String postComments) {
this.postComments = postComments;
}
// creating a constructor class.
public FacebookFeedModal(String authorImage, String authorName, String postDate,
String postDescription, String postIV, String postLikes,
String postComments) {
this.authorImage = authorImage;
this.authorName = authorName;
this.postDate = postDate;
this.postDescription = postDescription;
this.postIV = postIV;
this.postLikes = postLikes;
this.postComments = postComments;
}
}
Step 8: Creating an Adapter class for setting data to our item of RecyclerView
Navigate to the app > java > your app’s package name > Right-click on it > New Java class and name it as FacebookFeedRVAdapter and add the below code to it.
Java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
public class FacebookFeedRVAdapter extends RecyclerView.Adapter<FacebookFeedRVAdapter.ViewHolder> {
// arraylist for our facebook feeds.
private ArrayList<FacebookFeedModal> facebookFeedModalArrayList;
private Context context;
// creating a constructor for our adapter class.
public FacebookFeedRVAdapter(ArrayList<FacebookFeedModal> facebookFeedModalArrayList, Context context) {
this.facebookFeedModalArrayList = facebookFeedModalArrayList;
this.context = context;
}
@NonNull
@Override
public FacebookFeedRVAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// inflating our layout for item of recycler view item.
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.facebook_feed_rv_item, parent, false);
return new FacebookFeedRVAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull FacebookFeedRVAdapter.ViewHolder holder, int position) {
// getting data from array list and setting it to our modal class.
FacebookFeedModal modal = facebookFeedModalArrayList.get(position);
// setting data to each view of recyclerview item.
Picasso.get().load(modal.getAuthorImage()).into(holder.authorIV);
holder.authorNameTV.setText(modal.getAuthorName());
holder.timeTV.setText(modal.getPostDate());
holder.descTV.setText(modal.getPostDescription());
Picasso.get().load(modal.getPostIV()).into(holder.postIV);
holder.likesTV.setText(modal.getPostLikes());
holder.commentsTV.setText(modal.getPostComments());
}
@Override
public int getItemCount() {
// returning the size of our array list.
return facebookFeedModalArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
// creating variables for our views
// of recycler view items.
private CircleImageView authorIV;
private TextView authorNameTV, timeTV, descTV;
private ImageView postIV;
private TextView likesTV, commentsTV;
private LinearLayout shareLL;
public ViewHolder(@NonNull View itemView) {
super(itemView);
// initializing our variables
shareLL = itemView.findViewById(R.id.idLLShare);
authorIV = itemView.findViewById(R.id.idCVAuthor);
authorNameTV = itemView.findViewById(R.id.idTVAuthorName);
timeTV = itemView.findViewById(R.id.idTVTime);
descTV = itemView.findViewById(R.id.idTVDescription);
postIV = itemView.findViewById(R.id.idIVPost);
likesTV = itemView.findViewById(R.id.idTVLikes);
commentsTV = itemView.findViewById(R.id.idTVComments);
}
}
}
Step 9: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
// creating variables for our requestqueue,
// array list, progressbar, edittext,
// image button and our recycler view.
private RequestQueue mRequestQueue;
private ArrayList<FacebookFeedModal> instaModalArrayList;
private ArrayList<FacebookFeedModal> facebookFeedModalArrayList;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing our views.
progressBar = findViewById(R.id.idLoadingPB);
// calling method to load
// data in recycler view.
getFacebookFeeds();
}
private void getFacebookFeeds() {
facebookFeedModalArrayList = new ArrayList<>();
// below line is use to initialize the variable for our request queue.
mRequestQueue = Volley.newRequestQueue(MainActivity.this);
// below line is use to clear
// cache this will be use when
// our data is being updated.
mRequestQueue.getCache().clear();
// below is the url stored in
// string for our sample data
String url = "https://jsonkeeper.com/b/OB3B";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
progressBar.setVisibility(View.GONE);
try {
// in below line we are extracting the data from json object.
String authorName = response.getString("authorName");
String authorImage = response.getString("authorImage");
// below line is to get json array from our json object.
JSONArray feedsArray = response.getJSONArray("feeds");
// running a for loop to add data to our array list
for (int i = 0; i < feedsArray.length(); i++) {
// getting json object of our json array.
JSONObject feedsObj = feedsArray.getJSONObject(i);
// extracting data from our json object.
String postDate = feedsObj.getString("postDate");
String postDescription = feedsObj.getString("postDescription");
String postIV = feedsObj.getString("postIV");
String postLikes = feedsObj.getString("postLikes");
String postComments = feedsObj.getString("postComments");
// adding data to our modal class.
FacebookFeedModal feedModal = new FacebookFeedModal(authorImage, authorName, postDate, postDescription, postIV, postLikes, postComments);
facebookFeedModalArrayList.add(feedModal);
// below line we are creating an adapter class and adding our array list in it.
FacebookFeedRVAdapter adapter = new FacebookFeedRVAdapter(facebookFeedModalArrayList, MainActivity.this);
RecyclerView instRV = findViewById(R.id.idRVInstaFeeds);
// below line is for setting linear layout manager to our recycler view.
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this, RecyclerView.VERTICAL, false);
// below line is to set layout
// manager to our recycler view.
instRV.setLayoutManager(linearLayoutManager);
// below line is to set adapter
// to our recycler view.
instRV.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Fail to get data with error " + error, Toast.LENGTH_SHORT).show();
}
});
mRequestQueue.add(jsonObjectRequest);
}
}
Now run your app and see the output of the app.
Output:
You can check the project on the below link: https://github.com/ChaitanyaMunje/LibraryApp/tree/FacebookBranch
Similar Reads
How to Build a Facebook Like Custom RecyclerView in Android?
We have seen implementing RecyclerView in Android with simple data inside our app. In this article, we will take a look at the implementation of Facebook like Custom RecyclerView in Android. What we are going to build in this article? We will be building a simple application in which we will display
9 min read
How to Build an Instagram Like Custom RecyclerView in Android?
We have seen implementing RecyclerView in Android with simple data inside our app. In this article, we will take a look at the implementation of Instagram-like Custom RecyclerView in Android. What we are going to build in this article? We will be building a simple application in which we will be dis
8 min read
How to Animate RecyclerView Items in Android?
RecyclerView Item animation is one of the modern features that we can add to our Android app, the basic working of this is when any user opens our app then the data items that are present in recycler view will animate and then it will show to the user.it is so easy to implement also it can enhance t
5 min read
How to add Bullet list in a RecyclerView in Android?
Recycler View allows us to show a list of items but to convert our list into the bulleted list we have to do some extra work. You can do this task by following these simple steps given below:- Add the support Library in build.gradle file for Recycler View in the dependencies section. C/C++ Code
2 min read
How to create a nested RecyclerView in Android
A nested RecyclerView is an implementation of a RecyclerView within a RecyclerView. An example of such a layout can be seen in a variety of apps such as the Play Store, where the outer (parent) RecyclerView is of vertical orientation, whereas the inner (child) RecyclerViews are of horizontal orienta
6 min read
How to Add Dividers in Android RecyclerView?
In the article Android RecyclerView in Kotlin, it's been demonstrated how to implement the Recycler View in Android. But in the case of User Experience, the items need to be distinguished with the divider and proper padding and margins in each item. In this case, the Recycler View Item Decoration co
9 min read
How to Add Drag And Drop Feature in Android RecyclerView?
Drag And Drop feature is very helpful. It is very simple and easy to implement. It is used in many apps that are famous, and if we're doing some project it is very convenient to implement it. You should know the RecyclerAdapter Class. A sample video is given below to get an idea about what we are go
4 min read
How to Add Fade and Shrink Animation in RecyclerView in Android?
In this article, we are going to show the fade and shrink animation in RecyclerView. When we move downward then the item at the top will be fading out and then it will shrink. In the output, we can see how it is happening. We will be implementing this Java programming language. [video mp4="https://m
3 min read
How to Delete Multiple RecyclerView Items in Android?
RecyclerView is an advanced version of ListView with improved performance. When you have a long list of items to show you can use RecyclerView. It has the ability to reuse its views. In RecyclerView when the View goes out of the screen or is not visible to the user it won’t destroy it, it will reuse
8 min read
How to Use View Binding in RecyclerView Adapter Class in Android?
View Binding is a part of Android Jetpack which provides the views to bind with the classes by replacing the findViewById() method in a null safe way. In this article, we will see its implementations in the Adapter class of RecyclerView. Note that we are going to implement this project using the Kot
5 min read
How to Implement RecyclerView in a Fragment in Android?
In Android, a fragment is a modular section of a user interface that can be combined with other fragments to create a flexible and responsive application. A fragment represents a behavior or a portion of the user interface in an Activity, which can be reused in different activities or layouts. It ha
12 min read
How to Update RecyclerView Adapter Data in Android?
RecyclerView is used in many android applications to display the list of data within android applications. We can dynamically add or remove data from our recycler view. For updating data in the RecyclerView, we have to use the Adapter class. Adapter handles all the data within our recycler view. In
7 min read
How to insert Slide From Bottom animation in RecyclerView in Android
In this article, the animation that makes the items slide from the bottom is added in the recycler view. Here we don`t use any other library to add the animation. Adding animations make the application attractive and give a better user experience. Approach:Step 1: Create "anim" resource directory. R
2 min read
How to Disable RecyclerView Scrolling in Android?
RecyclerView is a view group used for displaying data from arrays and databases. RecyclerView basically is a list of items from the data. RecyclerView is often referred to as a successor of GridView and ListView. More about RecyclerView could be found at RecyclerView in Android with Example. Recycle
3 min read
How to Create a Custom Stepper Form in Android?
In this article, we are going to implement the multi-step form in modern Android apps. The basic working of that is when User Clicks on the Next Button then a new form comes with different input fields. Also, we have added a step indicator feature to track the user's progress through the steps. Also
5 min read
How to Build an Image Gallery Android App with RecyclerView and Glide?
In today's tech-savvy world, we are accustomed to having all our photos at our fingertips. Whether you're developing a social media app or a photo-editing tool, integrating a user-friendly image gallery is crucial. This article will walk you through the process of creating a simple yet effective ima
4 min read
How to Create RecyclerView with Multiple ViewType in Android?
RecyclerView forms a crucial part of the UI in Android app development. It is especially important to optimize memory consumption when displaying a long list of items. A RecyclerView inflates a customized list of items, which can have either similar layouts or multiple distinct layouts. Here, we dev
6 min read
How to Create Options Menu for RecyclerView in Android using Kotlin?
RecyclerView is a ViewGroup added to the android studio as a successor of the GridView and ListView. It is an improvement on both of them and can be found in the latest v-7 support packages. It has been created to make possible construction of any lists with XML layouts as an item that can be custom
6 min read
How to Build a Simple Swiping Game in Android?
In this article, we are going to create a simple swiping game using RecyclerView in Android Studio. RecyclerView is the ViewGroup that contains the views corresponding to your data. It's a view itself, so you add RecyclerView into your layout the way you would add any other UI element. We are going
6 min read
How to add Custom Spinner in android?
Spinner is a widget that is used to select an item from a list of items. When the user tap on a spinner a drop-down menu is visible to the user. In this article, we will learn how to add custom spinner in the app. If you want to know more about spinner in detail then click on this link. Approach: Cr
4 min read