Attaching Click Listeners to messages

Once you have added the DeliveredConversationFragment to your app, you can now add click listeners to messages displayed by it. If you have not set up your conversation Activity yet, please refer to the Initial setup section before continuing.

Adding click functionality is achieved using the methods setOnMessageClickListener and/or setOnMessageLongClickListener. Start by implementing the OnMessageClickListener and/or OnMessageLongClickListener interfaces in your conversation Activity as follows:

public class ConversationActivity extends AppCompatActivity 
									implements OnMessageClickListener,
        				                     OnMessageLongClickListener {
        ...
        
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    FragmentManager fragmentManager = getSupportFragmentManager();
	    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
	
	    mConversationFragment = DeliveredConversationFragment.newInstance(mChatId);
	    fragmentTransaction.add(R.id.conversation_fragment_container, mConversationFragment);
	    fragmentTransaction.commit();
	
	    mConversationFragment.setOnMessageClickListener(this);
	    mConversationFragment.setOnMessageLongClickListener(this);
	}
	
	...
	
}

Next, override the onMessageClick and/or onMessageLongClick methods and add the code you want to be executed when the user clicks or long-clicks on a message:

@Override
public boolean onMessageClick(@NonNull Message message) {
	 // Here you can implement any code to be executed when a message is clicked.
    Toast.makeText(getActivity(), "Clicked: " + message.getContent(), Toast.LENGTH_SHORT).show();
    
    return true;
}

@Override
public boolean onMessageLongClick(@NonNull Message message) {
    // Here you can implement any code to be executed when a message is long-clicked.
    Toast.makeText(getActivity(), "Long clicked: " + message.getContent(), Toast.LENGTH_SHORT).show();
    
    return false;
}

It is important to notice that both overridden methods have a Boolean type return value. This has to do with the default message click behaviour provided by the DeliveredConversationFragment. For single clicks the default behaviour is to show a message timestamp and status (sent/received). If you wish to replace this behaviour with your own functionality, simply return false in the overridden method.