VOOZH about

URL: https://www.geeksforgeeks.org/android/how-to-retrieve-blog-on-home-page-in-social-media-android-app/

⇱ How to Retrieve Blog On Home Page in Social Media Android App? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Retrieve Blog On Home Page in Social Media Android App?

Last Updated : 23 Jul, 2025

This is the Part 6 of "Build a Social Media App in Android Studio" tutorial, and we are going to cover the following functionalities in this article:

  • We are going to retrieve the blogs written by users on HomeFragment.
  • Here we have only shown the user data and title, description, and image of the blogs, but we are also going to implement the like and comment feature in upcoming blogs.
  • Then We will be showing Post details Activity where users will be able to comment.
  • We have also implemented the Like a Blog functionality.

Step By Step Implementation

Step 1: Create a new layout resource file

Go to the app > res > layout > New > Layout Resource File and name the file as row_posts. Below is the code for the row_posts.xml file.

Step 2: Create a new java class


 

Refer to this article Create Classes in Android Studio and name the class as ModelPost. Below is the code for the ModelPost.java file. Created this class to initialize the key so that we can retrieve the value of the key later. 


 

Similarly, create another java class and name the class as AdapterPosts. Below is the code for the AdapterPosts.java file. 


 

Step 3: Working with the fragment_home.xml file


 

Navigate to the app > res > layout > fragment_home.xml and add the below code to that file. Below is the code for the fragment_home.xml file. 


 

Step 4: Working with the HomeFragment.java file


 

This is how we will be retrieved blogs from Firebase. Every time data changes the value will change accordingly.


 

DatabaseReference databaseReference= FirebaseDatabase.getInstance().getReference("Posts");
 databaseReference.addValueEventListener(new ValueEventListener() {
 @Override
 public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
 posts.clear();
 for (DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
 }
 }

 @Override
 public void onCancelled(@NonNull DatabaseError databaseError) {

 Toast.makeText(getActivity(),databaseError.getMessage(),Toast.LENGTH_LONG).show();
 }
 });


 

Step 5: Create 2 new empty activities and name the activities as PostLikedByActivity and PostDetailsActivity. Keep the file as it is.


 

Output:


 

👁 Image


 

Note: Please Add drawable items before running the Application


 

Below is the file structure after performing these operations:


 

👁 Image


 

Comment
Article Tags:
Article Tags:

Explore