Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make use of RecyclerView to show the chats between two people in your app?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 508
    Comment on it

    If your app wants to show the chats between you n your friend then you can make use of RecyclerView.

    Here in the layout that is being inflated in RecylerView, I have used the visibility concept that the layout consists of two major layouts one layout shows the messages sent by me while other one shows the messages received by me, so below is the code for the same:-

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <LinearLayout
            android:id="@+id/llreceivedLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/fifteen_dp"
            android:orientation="horizontal"
            android:visibility="gone"
            android:weightSum="2">
    
            <RelativeLayout
                android:layout_width="80dp"
                android:layout_height="95dp"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="@dimen/fifteen_dp">
    
                <com.ltj.utility.CircleImageView
                    android:id="@+id/imgReceivedProfilePic"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:src="@drawable/profile_pic" />
    
                <ProgressBar
                    android:id="@+id/pbReceivedProfileImage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:visibility="gone" />
    
            </RelativeLayout>
    
            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="@dimen/ten_dp"
                android:layout_marginRight="@dimen/ten_dp"
                android:layout_weight="2"
                android:orientation="vertical">
    
                <TextView
                    android:id="@+id/tvSenderName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="@dimen/fifteen_sp" />
    
                <ImageView
                    android:id="@+id/imgRedPointer"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:src="@drawable/pointerred" />
    
                <LinearLayout
                    android:id="@+id/llmainReceived"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toRightOf="@+id/imgRedPointer"
                    android:background="@color/received_message_color"
                    android:orientation="vertical">
    
    
                    <RelativeLayout
                        android:id="@+id/rlReceivedAudioMessageReceived"
                        android:layout_width="match_parent"
                        android:layout_height="100dp"
                        android:layout_marginBottom="@dimen/ten_dp"
                        android:layout_marginLeft="@dimen/five_dp"
                        android:layout_marginRight="@dimen/five_dp"
                        android:layout_marginTop="@dimen/ten_dp"
                        android:background="@color/messages_screen_text_view"
                        android:visibility="gone">
    
                        <ImageView
                            android:id="@+id/ivReceivedMessageAudioReceived"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerInParent="true"
                            android:padding="@dimen/fifteen_dp"
                            android:src="@drawable/voice"
                            android:tag="play" />
    
                    </RelativeLayout>
    
                    <LinearLayout
                        android:id="@+id/llVideoVisibility"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="@dimen/ten_dp"
                        android:layout_marginLeft="@dimen/five_dp"
                        android:layout_marginRight="@dimen/five_dp"
                        android:layout_marginTop="@dimen/ten_dp"
                        android:orientation="vertical"
                        android:visibility="gone">
    
                        <RelativeLayout
                            android:id="@+id/rlVideoDummy"
                            android:layout_width="match_parent"
                            android:layout_height="100dp"
                            android:background="@android:color/black">
    
                            <ImageView
                                android:id="@+id/ivMessageVideoReceived"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_centerInParent="true"
                                android:padding="@dimen/five_dp"
                                android:src="@drawable/play" />
    
                        </RelativeLayout>
    
                        <TextView
                            android:id="@+id/tvVideoCaption"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="@color/messages_screen_text_view"
                            android:padding="@dimen/five_dp"
                            android:textSize="@dimen/fifteen_sp" />
    
                    </LinearLayout>
    
    
                    <com.ltj.fonts.RobotoMediumTextView
                        android:id="@+id/tvReceivedMessages"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:lineSpacingExtra="2dp"
                        android:padding="@dimen/five_dp"
                        android:textColor="@android:color/white"
                        android:textSize="@dimen/thirteen_sp"
                        android:visibility="gone" />
    
                    <LinearLayout
                        android:id="@+id/llImageVisibility"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="@dimen/five_dp"
                        android:layout_marginLeft="@dimen/five_dp"
                        android:layout_marginRight="@dimen/five_dp"
                        android:orientation="vertical"
                        android:visibility="gone">
    
                        <ImageView
                            android:id="@+id/imgReceivedImage"
                            android:layout_width="match_parent"
                            android:layout_height="140dp"
                            android:layout_marginTop="@dimen/five_dp"
                            android:adjustViewBounds="true"
                            android:scaleType="centerCrop"
                            android:textColor="@color/greyish"
                            android:textSize="@dimen/thirteen_sp" />
    
                        <TextView
                            android:id="@+id/tvMessageCaption"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:paddingLeft="@dimen/five_dp"
                            android:paddingTop="@dimen/five_dp"
                            android:textSize="@dimen/fifteen_sp" />
                    </LinearLayout>
                </LinearLayout>
    
    
                <com.ltj.fonts.RobotoMediumTextView
                    android:id="@+id/tvReceivedDate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/llmainReceived"
                    android:layout_marginLeft="@dimen/five_dp"
                    android:layout_marginTop="@dimen/ten_dp"
                    android:paddingTop="@dimen/five_dp"
                    android:textColor="@color/greyish" />
    
    
            </RelativeLayout>
    
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/llSentLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/fifteen_dp"
            android:orientation="horizontal"
            android:visibility="gone"
            android:weightSum="2">
    
            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="@dimen/ten_dp"
                android:layout_marginRight="@dimen/ten_dp"
                android:layout_weight="1.9"
                android:orientation="vertical">
    
                <TextView
                    android:id="@+id/tvReceiverName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:textSize="@dimen/fifteen_sp" />
    
                <ImageView
                    android:id="@+id/imgBluePointer"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/pointerblue" />
    
                <LinearLayout
                    android:id="@+id/llMainSent"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/five_dp"
                    android:layout_below="@id/tvReceiverName"
                    android:layout_centerVertical="true"
                    android:layout_toLeftOf="@+id/imgBluePointer"
                    android:background="@color/sent_message_color"
                    android:orientation="vertical">
    
    
                    <LinearLayout
                        android:id="@+id/llSentVideoVisibility"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="@dimen/ten_dp"
                        android:layout_marginLeft="@dimen/five_dp"
                        android:layout_marginRight="@dimen/five_dp"
                        android:layout_marginTop="@dimen/ten_dp"
                        android:orientation="vertical"
                        android:visibility="gone">
    
                        <RelativeLayout
                            android:id="@+id/rlSentVideoDummy"
                            android:layout_width="match_parent"
                            android:layout_height="100dp"
                            android:background="@android:color/black">
    
                            <ImageView
                                android:id="@+id/ivSentMessageVideoReceived"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_centerInParent="true"
                                android:padding="@dimen/five_dp"
                                android:src="@drawable/play" />
    
                        </RelativeLayout>
    
                        <TextView
                            android:id="@+id/tvSentVideoCaption"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="@color/messages_screen_text_view"
                            android:padding="@dimen/five_dp"
                            android:textSize="@dimen/fifteen_sp" />
    
                    </LinearLayout>
    
    
                    <com.ltj.fonts.RobotoMediumTextView
                        android:id="@+id/tvSentMessages"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:lineSpacingExtra="2dp"
                        android:padding="@dimen/five_dp"
                        android:textColor="@android:color/white"
                        android:textSize="@dimen/thirteen_sp"
                        android:visibility="gone" />
    
                    <RelativeLayout
                        android:id="@+id/rlSentAudioMessageReceived"
                        android:layout_width="match_parent"
                        android:layout_height="100dp"
                        android:layout_marginBottom="@dimen/ten_dp"
                        android:layout_marginLeft="@dimen/five_dp"
                        android:layout_marginRight="@dimen/five_dp"
                        android:layout_marginTop="@dimen/ten_dp"
                        android:background="@color/messages_screen_text_view"
                        android:visibility="gone">
    
                        <ImageView
                            android:id="@+id/ivSentMessageAudioReceived"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerInParent="true"
                            android:padding="@dimen/fifteen_dp"
                            android:src="@drawable/voice"
                            android:tag="play" />
    
                    </RelativeLayout>
    
                    <LinearLayout
                        android:id="@+id/llSentImageVisibility"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="@dimen/five_dp"
                        android:layout_marginLeft="@dimen/five_dp"
                        android:layout_marginRight="@dimen/five_dp"
                        android:orientation="vertical"
                        android:visibility="gone">
    
                        <ImageView
                            android:id="@+id/imgSentImage"
                            android:layout_width="match_parent"
                            android:layout_height="140dp"
                            android:layout_gravity="center_vertical"
                            android:layout_marginTop="@dimen/five_dp"
                            android:adjustViewBounds="true"
                            android:scaleType="centerCrop"
                            android:textColor="@color/greyish"
                            android:textSize="@dimen/thirteen_sp" />
    
                        <TextView
                            android:id="@+id/tvSentMessageCaption"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:padding="@dimen/five_dp"
                            android:textSize="@dimen/fifteen_sp" />
                    </LinearLayout>
                </LinearLayout>
    
    
                <com.ltj.fonts.RobotoMediumTextView
                    android:id="@+id/tvSentDate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/llMainSent"
                    android:layout_marginTop="@dimen/fifteen_dp"
                    android:textColor="@color/greyish" />
    
    
            </RelativeLayout>
    
    
            <RelativeLayout
                android:layout_width="80dp"
                android:layout_height="95dp"
                android:layout_alignParentRight="true"
                android:layout_gravity="center_vertical">
    
                <com.ltj.utility.CircleImageView
                    android:id="@+id/imgProfilePic"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:src="@drawable/profile_pic" />
    
                <ProgressBar
                    android:id="@+id/pbProfileImage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:visibility="gone" />
            </RelativeLayout>
    
    
        </LinearLayout>
    
    </LinearLayout>
    

     

    Above is the layout that I have used and below is the RecyclerView adapter code, I have made the changes in onBindViewHolder

     

    public class MessageHistoryRViewAdapter extends RecyclerView.Adapter<MessageHistoryRViewAdapter.ViewHolder> {
    
        private Context context;
        private ArrayList<MessageHistory> messageHistoryList;
        private String type;
        private String status;
        private ProgressDialog progressDialog;
        private HashMap<String, String> mParams;
        private HashMap<String, String> mHeaders;
        private CommonHttpHandler commonHttpHandler;
        private Gson gson;
        private String user_Pic;
        private String my_pic;
        private String name;
        private String group_Name;
        private AppSharedPreferences appSharedPreferences;
    
        public MessageHistoryRViewAdapter(Context context, ArrayList<MessageHistory> data, String profile_pic, String myPic, String person_name) {
            this.context = context;
            messageHistoryList = data;
            user_Pic = profile_pic;
            my_pic = myPic;
            name = person_name;
    
            this.type = type;
            this.status = status;
    
            initVariables();
        }
    
        private void initVariables() {
    
            progressDialog = new ProgressDialog(context);
            progressDialog.setMessage(AppConstants.sPleaseWait);
            progressDialog.setCancelable(false);
    
            gson = new Gson();
            appSharedPreferences = AppSharedPreferences.getInstance(context);
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
    
            ImageView ivSentMessageAudioReceived, ivSentMessageVideoReceived, imgReceivedProfilePic, imgProfilePic, imgReceivedImage, imgSentImage, ivReceivedMessageAudioReceived, ivMessageVideoReceived;
            TextView tvReceiverName, tvSenderName, tvReceivedMessage, tvSentMessage, tvReceivedDate, tvReceivedTime, tvSentDate, tvSentTime, tvSentVideoCaption, tvSentMessageCaption, tvMessageCaption, tvVideoCaption;
            LinearLayout llreceivedLayout, llSentLayout, llImageVisibility, llSentImageVisibility, llVideoVisibility, llSentVideoVisibility;
            ProgressBar pbReceivedProfileImage, pbProfileImage;
            RelativeLayout rlReceivedAudioMessageReceived, rlSentAudioMessageReceived;
    
    
            private ViewHolder(View itemLayoutView) {
                super(itemLayoutView);
                ivReceivedMessageAudioReceived = (ImageView) itemLayoutView.findViewById(R.id.ivReceivedMessageAudioReceived);
                ivMessageVideoReceived = (ImageView) itemLayoutView.findViewById(R.id.ivMessageVideoReceived);
                ivSentMessageVideoReceived = (ImageView) itemLayoutView.findViewById(R.id.ivSentMessageVideoReceived);
                ivSentMessageAudioReceived = (ImageView) itemLayoutView.findViewById(R.id.ivSentMessageAudioReceived);
    
                llVideoVisibility = (LinearLayout) itemLayoutView.findViewById(R.id.llVideoVisibility);
                llSentVideoVisibility = (LinearLayout) itemLayoutView.findViewById(R.id.llSentVideoVisibility);
    
                rlReceivedAudioMessageReceived = (RelativeLayout) itemLayoutView.findViewById(R.id.rlReceivedAudioMessageReceived);
                rlSentAudioMessageReceived = (RelativeLayout) itemLayoutView.findViewById(R.id.rlSentAudioMessageReceived);
    
                llImageVisibility = (LinearLayout) itemLayoutView.findViewById(R.id.llImageVisibility);
                llSentImageVisibility = (LinearLayout) itemLayoutView.findViewById(R.id.llSentImageVisibility);
    
    
                imgReceivedProfilePic = (ImageView) itemLayoutView.findViewById(R.id.imgReceivedProfilePic);
                imgProfilePic = (ImageView) itemLayoutView.findViewById(R.id.imgProfilePic);
    
                imgReceivedImage = (ImageView) itemLayoutView.findViewById(R.id.imgReceivedImage);
                imgSentImage = (ImageView) itemLayoutView.findViewById(R.id.imgSentImage);
    
                tvSentVideoCaption = (TextView) itemLayoutView.findViewById(R.id.tvSentVideoCaption);
                tvSentMessageCaption = (TextView) itemLayoutView.findViewById(R.id.tvSentMessageCaption);
    
                tvReceivedDate = (TextView) itemLayoutView.findViewById(R.id.tvReceivedDate);
                tvSenderName = (TextView) itemLayoutView.findViewById(R.id.tvSenderName);
                tvReceiverName = (TextView) itemLayoutView.findViewById(R.id.tvReceiverName);
    
    
                tvMessageCaption = (TextView) itemLayoutView.findViewById(R.id.tvMessageCaption);
                tvVideoCaption = (TextView) itemLayoutView.findViewById(R.id.tvVideoCaption);
    
                tvSentDate = (TextView) itemLayoutView.findViewById(R.id.tvSentDate);
    
                tvSentMessage = (TextView) itemLayoutView.findViewById(R.id.tvSentMessages);
                tvReceivedMessage = (TextView) itemLayoutView.findViewById(R.id.tvReceivedMessages);
    
                llreceivedLayout = (LinearLayout) itemLayoutView.findViewById(R.id.llreceivedLayout);
                llSentLayout = (LinearLayout) itemLayoutView.findViewById(R.id.llSentLayout);
                pbProfileImage = (ProgressBar) itemLayoutView.findViewById(R.id.pbProfileImage);
                pbReceivedProfileImage = (ProgressBar) itemLayoutView.findViewById(R.id.pbReceivedProfileImage);
    
    
            }
        }
    
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_history_layout, parent, false);
            return new ViewHolder(itemLayoutView);
        }
    
        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
    
    
            if (messageHistoryList.get(position).getMessage().getSender_id().equals(appSharedPreferences.getString(AppConstants.KEY_SHARED_PREFERENCE_USER_ID, ""))) {
    
                holder.llSentLayout.setVisibility(View.VISIBLE);
                String date = parseDateToddMMYYYY(messageHistoryList.get(position).getMessage().getCreated());
                holder.tvSentDate.setText(date);
                holder.pbReceivedProfileImage.setVisibility(View.GONE);
                holder.llreceivedLayout.setVisibility(View.GONE);
    
                if (messageHistoryList.get(position).getMessage().getGroup_id() != null && messageHistoryList.get(position).getMessage().getMessage().equalsIgnoreCase("You have an invitation from a group.")) {
                    holder.tvReceiverName.setText(appSharedPreferences.getString(AppConstants.KEY_FIRST_NAME, "") + " " + appSharedPreferences.getString(AppConstants.KEY_LAST_NAME, ""));
    
                } else {
                    holder.tvReceiverName.setText(appSharedPreferences.getString(AppConstants.KEY_FIRST_NAME, "") + " " + appSharedPreferences.getString(AppConstants.KEY_LAST_NAME, "") + "(" + messageHistoryList.get(position).getGroup().getName() + ")");
    
                }
    
                if (messageHistoryList.get(position).getMessage().getAudio() != null) {
    
                    holder.rlSentAudioMessageReceived.setVisibility(View.VISIBLE);
                    holder.llSentVideoVisibility.setVisibility(View.GONE);
                    holder.llSentImageVisibility.setVisibility(View.GONE);
                    holder.tvSentMessage.setVisibility(View.GONE);
    
                } else if (messageHistoryList.get(position).getMessage().getVideo() != null) {
    
                    holder.llSentVideoVisibility.setVisibility(View.VISIBLE);
                    holder.rlSentAudioMessageReceived.setVisibility(View.GONE);
                    holder.llSentImageVisibility.setVisibility(View.GONE);
                    holder.tvSentMessage.setVisibility(View.GONE);
    
    
                    if (messageHistoryList.get(position).getMessage().getMessage() != null) {
                        holder.tvSentVideoCaption.setText(messageHistoryList.get(position).getMessage().getMessage());
                    } else {
                        holder.tvSentVideoCaption.setVisibility(View.GONE);
                    }
    
                } else if (messageHistoryList.get(position).getMessage().getImage() != null) {
    
                    holder.llSentImageVisibility.setVisibility(View.VISIBLE);
                    holder.rlSentAudioMessageReceived.setVisibility(View.GONE);
                    holder.llSentVideoVisibility.setVisibility(View.GONE);
                    holder.tvSentMessage.setVisibility(View.GONE);
    
                    loadImageInImageSend(messageHistoryList.get(position).getMessage().getImage(), holder.imgSentImage);
                    if (messageHistoryList.get(position).getMessage().getMessage() != null) {
                        holder.tvSentMessageCaption.setText(messageHistoryList.get(position).getMessage().getMessage());
                    } else {
                        holder.tvSentMessageCaption.setVisibility(View.GONE);
                    }
                } else {
                    holder.tvSentMessage.setVisibility(View.VISIBLE);
                    holder.llSentImageVisibility.setVisibility(View.GONE);
                    holder.rlSentAudioMessageReceived.setVisibility(View.GONE);
                    holder.llSentVideoVisibility.setVisibility(View.GONE);
                    holder.tvSentMessage.setText(messageHistoryList.get(position).getMessage().getMessage());
                }
    
                String imgUrl = my_pic;
    
                if (!imgUrl.equals("")) {
                    holder.pbProfileImage.setVisibility(View.VISIBLE);
                    Glide.with(context).load(imgUrl).diskCacheStrategy(DiskCacheStrategy.ALL).listener(new RequestListener<String, GlideDrawable>() {
                        @Override
                        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                            holder.pbProfileImage.setVisibility(View.GONE);
                            holder.imgProfilePic.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.profile_pic));
                            return false;
                        }
    
                        @Override
                        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                            holder.pbProfileImage.setVisibility(View.GONE);
                            return false;
                        }
                    }).into(holder.imgProfilePic);
                } else {
                    holder.pbProfileImage.setVisibility(View.GONE);
                    holder.imgProfilePic.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.profile_pic));
                }
    
    
            } else {
    
                holder.llreceivedLayout.setVisibility(View.VISIBLE);
                holder.llSentLayout.setVisibility(View.GONE);
                holder.pbReceivedProfileImage.setVisibility(View.GONE);
    
                if (messageHistoryList.get(position).getMessage().getGroup_id() != null) {
                    if (messageHistoryList.get(position).getMessage().getMessage().equalsIgnoreCase("You have an invitation from a group."))
                        holder.tvSenderName.setText(name);
                    else {
                        holder.tvSenderName.setText(name + "(" + messageHistoryList.get(position).getGroup().getName() + ")");
                    }
                } else
                    holder.tvSenderName.setText(name);
    
    
                String date = parseDateToddMMYYYY(messageHistoryList.get(position).getMessage().getCreated());
                holder.tvReceivedDate.setText(date);
    
    
                if (messageHistoryList.get(position).getMessage().getAudio() != null) {
    
                    holder.rlReceivedAudioMessageReceived.setVisibility(View.VISIBLE);
    
                    holder.llVideoVisibility.setVisibility(View.GONE);
                    holder.llImageVisibility.setVisibility(View.GONE);
                    holder.tvReceivedMessage.setVisibility(View.GONE);
    
                } else if (messageHistoryList.get(position).getMessage().getVideo() != null) {
    
    
                    holder.llVideoVisibility.setVisibility(View.VISIBLE);
    
                    holder.rlReceivedAudioMessageReceived.setVisibility(View.GONE);
                    holder.llImageVisibility.setVisibility(View.GONE);
                    holder.tvReceivedMessage.setVisibility(View.GONE);
    
                    if (messageHistoryList.get(position).getMessage().getMessage() != null) {
                        holder.tvVideoCaption.setText(messageHistoryList.get(position).getMessage().getMessage());
                    } else {
                        holder.tvVideoCaption.setVisibility(View.GONE);
                    }
    
                } else if (messageHistoryList.get(position).getMessage().getImage() != null) {
    
    
                    holder.llImageVisibility.setVisibility(View.VISIBLE);
    
                    holder.rlReceivedAudioMessageReceived.setVisibility(View.GONE);
                    holder.llVideoVisibility.setVisibility(View.GONE);
                    holder.tvReceivedMessage.setVisibility(View.GONE);
    
                    loadImageInImageSend(messageHistoryList.get(position).getMessage().getImage(), holder.imgReceivedImage);
                    if (messageHistoryList.get(position).getMessage().getMessage() != null) {
                        holder.tvMessageCaption.setText(messageHistoryList.get(position).getMessage().getMessage());
                    } else {
                        holder.tvMessageCaption.setVisibility(View.GONE);
                    }
                } else {
                    holder.tvReceivedMessage.setVisibility(View.VISIBLE);
    
                    holder.rlReceivedAudioMessageReceived.setVisibility(View.GONE);
                    holder.llVideoVisibility.setVisibility(View.GONE);
                    holder.llImageVisibility.setVisibility(View.GONE);
    
                    holder.tvReceivedMessage.setText(messageHistoryList.get(position).getMessage().getMessage());
                }
    
    
                String imgUrl = user_Pic;
    
                if (!imgUrl.equals("")) {
                    holder.pbReceivedProfileImage.setVisibility(View.VISIBLE);
                    Glide.with(context).load(imgUrl).diskCacheStrategy(DiskCacheStrategy.ALL).listener(new RequestListener<String, GlideDrawable>() {
                        @Override
                        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                            holder.pbReceivedProfileImage.setVisibility(View.GONE);
                            holder.imgReceivedProfilePic.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.profile_pic));
                            return false;
                        }
    
                        @Override
                        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                            holder.pbReceivedProfileImage.setVisibility(View.GONE);
                            return false;
                        }
                    }).into(holder.imgReceivedProfilePic);
                } else {
                    holder.pbReceivedProfileImage.setVisibility(View.GONE);
                    holder.imgReceivedProfilePic.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.profile_pic));
                }
    
            }
    
            holder.imgSentImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    if (holder.imgSentImage.getVisibility() == (View.VISIBLE)) {
                        Intent imageShowingIntent = new Intent(context, ImageShowingActivity.class);
    
    
                        imageShowingIntent.putExtra("imagePath", messageHistoryList.get(position).getMessage().getImage());
    
    
                        context.startActivity(imageShowingIntent);
                    }
    
                }
            });
    
            holder.imgReceivedImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (holder.imgReceivedImage.getVisibility() == (View.VISIBLE)) {
                        Intent imageShowingIntent = new Intent(context, ImageShowingActivity.class);
                        imageShowingIntent.putExtra("imagePath", messageHistoryList.get(position).getMessage().getImage());
                        context.startActivity(imageShowingIntent);
                    }
                }
            });
    
            holder.ivReceivedMessageAudioReceived.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (holder.rlReceivedAudioMessageReceived.getVisibility() == (View.VISIBLE)) {
                        Intent audioPlayIntent = new Intent(context, AudioPlayingActivity.class);
                        audioPlayIntent.putExtra("audioPath", messageHistoryList.get(position).getMessage().getAudio());
                        context.startActivity(audioPlayIntent);
                    }
                }
            });
            
            holder.ivMessageVideoReceived.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    if (holder.llVideoVisibility.getVisibility() == (View.VISIBLE)) {
                        Intent videoPlayingIntent = new Intent(context, VideoPlayingActivity.class);
                        videoPlayingIntent.putExtra("videoPath", messageHistoryList.get(position).getMessage().getVideo());
                        context.startActivity(videoPlayingIntent);
                    }
                }
            });
            holder.ivSentMessageVideoReceived.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    if (holder.llSentVideoVisibility.getVisibility() == (View.VISIBLE)) {
                        Intent videoPlayingIntent = new Intent(context, VideoPlayingActivity.class);
    
                        videoPlayingIntent.putExtra("videoPath", messageHistoryList.get(position).getMessage().getVideo());
    
                        context.startActivity(videoPlayingIntent);
                    }
                }
            });
            holder.ivSentMessageAudioReceived.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    if (holder.rlSentAudioMessageReceived.getVisibility() == (View.VISIBLE)) {
                        Intent audioPlayIntent = new Intent(context, AudioPlayingActivity.class);
    
                        audioPlayIntent.putExtra("audioPath", messageHistoryList.get(position).getMessage().getAudio());
    
                        context.startActivity(audioPlayIntent);
                    }
                }
            });
    
    
        }
    
        @Override
        public int getItemCount() {
            return messageHistoryList.size();
        }
    
    
        private void loadImageInImageSend(String profile_image, final ImageView imageView) {
    
            Glide.with(context).load(profile_image).diskCacheStrategy(DiskCacheStrategy.ALL).listener(new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                    Toast.makeText(context, "Failed to get Image From server..Please try again", Toast.LENGTH_SHORT).show();
                    return false;
                }
    
                @Override
                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    return false;
                }
            }).into(imageView);
    
        }
    
    
        private String parseDateToddMMYYYY(String time) {
    
            String inputPattern = "yyyy-MM-dd HH:mm:ss";
            String outputPattern = "dd-MMM h:mm a";
            SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
            SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
    
            Date date;
            String str = null;
    
            try {
                date = inputFormat.parse(time);
                str = outputFormat.format(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: