1. Create an XML layout file for the custom bubble
First, create a new XML layout resource file for your custom bubble, we will name it my_custom_bubble_layout.xml. You are free to customize the custom bubble layout as you wish. In this example, we will be using a simple image and text bubble.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:background="@color/tapWhite">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@mipmap/ic_launcher"
app:layout_constraintTop_toTopOf="parent" />
<!--We are using TapTalk'io's default message body style for this TextView-->
<TextView
android:id="@+id/textView"
style="@style/tapLeftBubbleMessageBodyStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
app:layout_constraintTop_toBottomOf="@id/imageView"
tools:text="Message body" />
</android.support.constraint.ConstraintLayout>
Note: TapTalk.io's default message bubble layouts generally have a 8dp bottom margin and no top margin.
2. Create a custom listener
In order to listen to event callbacks that happens in your custom bubble, we will create a custom interface that extends TapTalkBaseCustomInterface. We will name this interface MyCustomBubbleListener.
importio.taptalk.TapTalk.Interface.TapTalkBaseCustomInterface;importio.taptalk.TapTalk.Model.TAPMessageModel;publicinterfaceMyCustomBubbleListenerextendsTapTalkBaseCustomInterface {// Use this to notify that the user tapped your custom bubblevoidonBubbleTapped(TAPMessageModel message);}
import io.taptalk.TapTalk.Interface.TapTalkBaseCustomInterfaceimport io.taptalk.TapTalk.Model.TAPMessageModelinterfaceMyCustomBubbleListener : TapTalkBaseCustomInterface {// Use this to notify that the user tapped your custom bubblefunonBubbleTapped(message: TAPMessageModel)}
3. Create a custom bubble ViewHolder
To display your custom bubble layout in a chat room, you will need to create a custom ViewHolder that extends TAPBaseChatViewHolder. We will name this class MyCustomBubbleViewHolder.
importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.ImageView;importandroid.widget.TextView;importio.taptalk.TapTalk.Model.TAPMessageModel;importio.taptalk.TapTalk.View.Adapter.TAPBaseChatViewHolder;publicclassMyCustomBubbleViewHolderextendsTAPBaseChatViewHolder {privateImageView imageView;privateTextView textView;privateMyCustomBubbleListener listener; // The listener that you just created above will be used here// Add MyCustomBubbleListener to the constructor parameterMyCustomBubbleViewHolder(ViewGroup parent,int itemLayoutId,MyCustomBubbleListener listener) { super(parent, itemLayoutId);// Bind views on constructor imageView =itemView.findViewById(R.id.imageView); textView =itemView.findViewById(R.id.textView);// Pass the listener from parameterthis.listener= listener; } @OverrideprotectedvoidonBind(TAPMessageModel message,int index) { super.onBind(message, index);// Set message texttextView.setText(message.getBody());// Set click listenerimageView.setOnClickListener(new View.OnClickListener() { @OverridepublicvoidonClick(View view) {// Trigger listener and pass the clicked messagelistener.onBubbleTapped(message); } }); }}
import android.view.ViewGroupimport android.widget.ImageViewimport android.widget.TextViewimport io.taptalk.TapTalk.Model.TAPMessageModelimport io.taptalk.TapTalk.View.Adapter.TAPBaseChatViewHolderclassMyCustomBubbleViewHolderinternalconstructor( parent: ViewGroup, itemLayoutId: Int,privateval listener: MyCustomBubbleListener) : TAPBaseChatViewHolder(parent, itemLayoutId) {privateval imageView: ImageView= itemView.findViewById(R.id.imageView)privateval textView: TextView= itemView.findViewById(R.id.textView)overridefunonBind(message: TAPMessageModel?, index: Int) {super.onBind(message, index)// Set message text textView.text = message!!.body// Pass the listener from parameter imageView.setOnClickListener { // Trigger listener and pass the clicked message listener.onBubbleTapped(message) } }}
4. Create a custom bubble class
Using all the things you have created above, we will create a custom bubble class that extends TAPBaseCustomBubble. This class instance will then be added to the custom bubble list.
To add your custom bubble to TapTalk.io, use the TapUI class and call the addCustomBubble() method. This is typically done in your Application class, after init() has already been called.
TapTalk.init(...);// Insert the XML layout resource, message type, and your custom listener as parameterTapUI.getInstance().addCustomBubble(newMyCustomBubbleClass(R.layout.my_custom_bubble_layout,3001,new MyCustomBubbleListener() { @Overridepublicvoid onBubbleTapped(TAPMessageModel message) {// My custom bubble was tapped by user }}));
TapTalk.init(...)// Insert the XML layout resource, message type, and your custom listener as parameterTapUI.getInstance().addCustomBubble(MyCustomBubbleClass(R.layout.my_custom_bubble_layout,3001,object : MyCustomBubbleListener() {funonBubbleTapped(message: TAPMessageModel) {// My custom bubble was tapped by user } }))
Note: Please note that the message type for custom bubbles should start with 3 as the prefix. For more information, check the message type page.
After completing all the steps above, your custom bubble will be ready to use.
Showing Your Custom Bubble in Chat
You can use your customized chat bubble in a chat room by constructing a custom TAPMessageModel with constructTapTalkMessageModel() method, then sending the constructed message to the chat room with sendCustomMessage(). Both methods can be accessed from the TapCoreMessageManager class.
To construct a message, you are required to have a TAPRoomModel as a parameter. You can use getPersonalChatRoom() or getGroupChatRoom() method in TapCoreChatRoomManager class to obtain a chat room model.
Below is an example to send a custom message with the steps mentioned above.
// First, we will retrieve a TAPRoomModel required to construct a messageTapCoreChatRoomManager.getInstance().getPersonalChatRoom(RECIPIENT_USER,newTapCoreGetRoomListener() { @OverridepublicvoidonSuccess(TAPRoomModel room) {// Retrieved room data successfully, we will use this room to construct a TAPMessageModelTAPMessageModel customBubbleMessage =TapCoreMessageManager.getInstance().constructTapTalkMessageModel("This is a custom message",// MESSAGE_BODY room,// ROOM3001,// MESSAGE_TYPEnull// MESSAGE_DATA );// We will then send the constructed message to its corresponding chat roomTapCoreMessageManager.getInstance().sendCustomMessage(customBubbleMessage,newTapCoreSendMessageListener() { @OverridepublicvoidonSuccess(TAPMessageModel message) {// Custom message was sent successfully } @OverridepublicvoidonError(String errorCode,String errorMessage) { } }); }});
// First, we will retrieve a TAPRoomModel required to construct a messageTapCoreChatRoomManager.getInstance().getPersonalChatRoom(RECIPIENT_USER, object : TapCoreGetRoomListener() {overridefunonSuccess(room: TAPRoomModel?) {// Retrieved room data successfully, we will use this room to construct a TAPMessageModelval customBubbleMessage = TapCoreMessageManager.getInstance().constructTapTalkMessageModel("This is a custom message", // MESSAGE_BODY room!!, // ROOM3001, // MESSAGE_TYPEnull// MESSAGE_DATA )// We will then send the constructed message to its corresponding chat room TapCoreMessageManager.getInstance().sendCustomMessage(customBubbleMessage, object : TapCoreSendMessageListener() {overridefunonSuccess(message: TAPMessageModel?) {// Custom message was sent successfully }overridefunonError(errorCode: String?, errorMessage: String?) { } }) }})