VOOZH about

URL: https://www.geeksforgeeks.org/android/implement-comment-on-a-particular-blog-functionality-in-social-media-android-app/

⇱ Implement Comment on a Particular Blog Functionality in Social Media Android App - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implement Comment on a Particular Blog Functionality in Social Media Android App

Last Updated : 23 May, 2024

This is the Part 11 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 comment on the blog.
  • Here we are going to write a comment, and then we will be showing the comments and will be updating the comment count.
  • The comment feature is the best feature in any blogging app. It helps in interacting with the user who has written the blog and much more.

Step By Step Implementation 

Step 1: Create a new layout resource file

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

Step 2: Create a new java class and name the class as ModelComment

Working with the ModelComment.java file. Created this activity to initialize the key so that we can retrieve the value of the key later. Below is the code for the ModelComment.java file.

Step 3: Create another new java class and name the class as AdapterComment

Working with the AdapterComment.java file. Below is the code for the AdapterComment.java file.

Step 4:Working with the PostDetailsActivity Activity

Working with the activity_postdetails.xml file.

Working with the PostDetailsActivity.java file

Post Comment Using this:

DatabaseReference datarf= FirebaseDatabase.getInstance().getReference("Posts").child(postId).child("Comments");
 HashMap<String ,Object> hashMap=new HashMap<>();
 hashMap.put("cId",timestamp);
 hashMap.put("comment",commentss);
 hashMap.put("ptime",timestamp);
 hashMap.put("uid",myuid);
 hashMap.put("uemail",myemail);
 hashMap.put("udp",mydp);
 hashMap.put("uname",myname);
 datarf.child(timestamp).setValue(hashMap);

Showing Comment like this:

DatabaseReference reference= FirebaseDatabase.getInstance().getReference("Posts").child(postId).child("Comments");
 reference.addValueEventListener(new ValueEventListener() {
 @Override
 public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

 commentList.clear();
 for (DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
 
 }
 }

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

 }
 });

Below is the code for the PostDetailsActivity.java file.

Output:

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