VOOZH about

URL: https://www.geeksforgeeks.org/android/implement-delete-messages-functionality-in-social-media-android-app/

⇱ Implement Delete Messages Functionality in Social Media Android App - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implement Delete Messages Functionality in Social Media Android App

Last Updated : 23 May, 2024

This is the Part 15 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 delete the message in the ChatActivity.
  • We are going to delete text and image messages. When we click on a text then an AlertBox will come.
  • There will be two options to either delete that message or cancel the event. After Click on delete message will be deleted from both sides.

Step By Step Implementation

Step 1: Working with the AdapterChat.java file

We can delete a message only if we are the sender of the message. There are two methods to delete the message. You can use any one of them. In one, we have removed the value using datasnapshot and in another, we have updated the message as "This Message Was Deleted" as we see in WhatsApp.

if(dataSnapshot1.child("sender").getValue().equals(myuid)) {
// any two of below can be used
dataSnapshot1.getRef().removeValue();

// <-----------------------------------> 
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("message", "This Message Was Deleted");
dataSnapshot1.getRef().updateChildren(hashMap);
Toast.makeText(context,"Message Deleted.....",Toast.LENGTH_LONG).show();
}

Below is the code for the AdapterChat.java file.

Output: 

Note: Please Add drawable items before running the Application

Below is the file structure after performing these operations:

👁 Image
👁 Image


 

Comment
Article Tags:
Article Tags:

Explore