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
    • 530
    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:-

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="match_parent"
    4. android:layout_height="wrap_content"
    5. android:orientation="vertical">
    6.  
    7. <LinearLayout
    8. android:id="@+id/llreceivedLayout"
    9. android:layout_width="match_parent"
    10. android:layout_height="wrap_content"
    11. android:layout_marginTop="@dimen/fifteen_dp"
    12. android:orientation="horizontal"
    13. android:visibility="gone"
    14. android:weightSum="2">
    15.  
    16. <RelativeLayout
    17. android:layout_width="80dp"
    18. android:layout_height="95dp"
    19. android:layout_gravity="center_vertical"
    20. android:layout_marginLeft="@dimen/fifteen_dp">
    21.  
    22. <com.ltj.utility.CircleImageView
    23. android:id="@+id/imgReceivedProfilePic"
    24. android:layout_width="wrap_content"
    25. android:layout_height="wrap_content"
    26. android:layout_centerVertical="true"
    27. android:src="@drawable/profile_pic" />
    28.  
    29. <ProgressBar
    30. android:id="@+id/pbReceivedProfileImage"
    31. android:layout_width="wrap_content"
    32. android:layout_height="wrap_content"
    33. android:layout_centerHorizontal="true"
    34. android:layout_centerVertical="true"
    35. android:visibility="gone" />
    36.  
    37. </RelativeLayout>
    38.  
    39. <RelativeLayout
    40. android:layout_width="0dp"
    41. android:layout_height="wrap_content"
    42. android:layout_gravity="center"
    43. android:layout_marginLeft="@dimen/ten_dp"
    44. android:layout_marginRight="@dimen/ten_dp"
    45. android:layout_weight="2"
    46. android:orientation="vertical">
    47.  
    48. <TextView
    49. android:id="@+id/tvSenderName"
    50. android:layout_width="wrap_content"
    51. android:layout_height="wrap_content"
    52. android:textSize="@dimen/fifteen_sp" />
    53.  
    54. <ImageView
    55. android:id="@+id/imgRedPointer"
    56. android:layout_width="wrap_content"
    57. android:layout_height="wrap_content"
    58. android:layout_centerVertical="true"
    59. android:src="@drawable/pointerred" />
    60.  
    61. <LinearLayout
    62. android:id="@+id/llmainReceived"
    63. android:layout_width="match_parent"
    64. android:layout_height="wrap_content"
    65. android:layout_centerVertical="true"
    66. android:layout_toRightOf="@+id/imgRedPointer"
    67. android:background="@color/received_message_color"
    68. android:orientation="vertical">
    69.  
    70.  
    71. <RelativeLayout
    72. android:id="@+id/rlReceivedAudioMessageReceived"
    73. android:layout_width="match_parent"
    74. android:layout_height="100dp"
    75. android:layout_marginBottom="@dimen/ten_dp"
    76. android:layout_marginLeft="@dimen/five_dp"
    77. android:layout_marginRight="@dimen/five_dp"
    78. android:layout_marginTop="@dimen/ten_dp"
    79. android:background="@color/messages_screen_text_view"
    80. android:visibility="gone">
    81.  
    82. <ImageView
    83. android:id="@+id/ivReceivedMessageAudioReceived"
    84. android:layout_width="wrap_content"
    85. android:layout_height="wrap_content"
    86. android:layout_centerInParent="true"
    87. android:padding="@dimen/fifteen_dp"
    88. android:src="@drawable/voice"
    89. android:tag="play" />
    90.  
    91. </RelativeLayout>
    92.  
    93. <LinearLayout
    94. android:id="@+id/llVideoVisibility"
    95. android:layout_width="match_parent"
    96. android:layout_height="wrap_content"
    97. android:layout_marginBottom="@dimen/ten_dp"
    98. android:layout_marginLeft="@dimen/five_dp"
    99. android:layout_marginRight="@dimen/five_dp"
    100. android:layout_marginTop="@dimen/ten_dp"
    101. android:orientation="vertical"
    102. android:visibility="gone">
    103.  
    104. <RelativeLayout
    105. android:id="@+id/rlVideoDummy"
    106. android:layout_width="match_parent"
    107. android:layout_height="100dp"
    108. android:background="@android:color/black">
    109.  
    110. <ImageView
    111. android:id="@+id/ivMessageVideoReceived"
    112. android:layout_width="wrap_content"
    113. android:layout_height="wrap_content"
    114. android:layout_centerInParent="true"
    115. android:padding="@dimen/five_dp"
    116. android:src="@drawable/play" />
    117.  
    118. </RelativeLayout>
    119.  
    120. <TextView
    121. android:id="@+id/tvVideoCaption"
    122. android:layout_width="match_parent"
    123. android:layout_height="match_parent"
    124. android:background="@color/messages_screen_text_view"
    125. android:padding="@dimen/five_dp"
    126. android:textSize="@dimen/fifteen_sp" />
    127.  
    128. </LinearLayout>
    129.  
    130.  
    131. <com.ltj.fonts.RobotoMediumTextView
    132. android:id="@+id/tvReceivedMessages"
    133. android:layout_width="match_parent"
    134. android:layout_height="wrap_content"
    135. android:lineSpacingExtra="2dp"
    136. android:padding="@dimen/five_dp"
    137. android:textColor="@android:color/white"
    138. android:textSize="@dimen/thirteen_sp"
    139. android:visibility="gone" />
    140.  
    141. <LinearLayout
    142. android:id="@+id/llImageVisibility"
    143. android:layout_width="match_parent"
    144. android:layout_height="wrap_content"
    145. android:layout_marginBottom="@dimen/five_dp"
    146. android:layout_marginLeft="@dimen/five_dp"
    147. android:layout_marginRight="@dimen/five_dp"
    148. android:orientation="vertical"
    149. android:visibility="gone">
    150.  
    151. <ImageView
    152. android:id="@+id/imgReceivedImage"
    153. android:layout_width="match_parent"
    154. android:layout_height="140dp"
    155. android:layout_marginTop="@dimen/five_dp"
    156. android:adjustViewBounds="true"
    157. android:scaleType="centerCrop"
    158. android:textColor="@color/greyish"
    159. android:textSize="@dimen/thirteen_sp" />
    160.  
    161. <TextView
    162. android:id="@+id/tvMessageCaption"
    163. android:layout_width="match_parent"
    164. android:layout_height="match_parent"
    165. android:paddingLeft="@dimen/five_dp"
    166. android:paddingTop="@dimen/five_dp"
    167. android:textSize="@dimen/fifteen_sp" />
    168. </LinearLayout>
    169. </LinearLayout>
    170.  
    171.  
    172. <com.ltj.fonts.RobotoMediumTextView
    173. android:id="@+id/tvReceivedDate"
    174. android:layout_width="wrap_content"
    175. android:layout_height="wrap_content"
    176. android:layout_below="@+id/llmainReceived"
    177. android:layout_marginLeft="@dimen/five_dp"
    178. android:layout_marginTop="@dimen/ten_dp"
    179. android:paddingTop="@dimen/five_dp"
    180. android:textColor="@color/greyish" />
    181.  
    182.  
    183. </RelativeLayout>
    184.  
    185. </LinearLayout>
    186.  
    187. <LinearLayout
    188. android:id="@+id/llSentLayout"
    189. android:layout_width="match_parent"
    190. android:layout_height="wrap_content"
    191. android:layout_marginTop="@dimen/fifteen_dp"
    192. android:orientation="horizontal"
    193. android:visibility="gone"
    194. android:weightSum="2">
    195.  
    196. <RelativeLayout
    197. android:layout_width="0dp"
    198. android:layout_height="wrap_content"
    199. android:layout_gravity="center"
    200. android:layout_marginLeft="@dimen/ten_dp"
    201. android:layout_marginRight="@dimen/ten_dp"
    202. android:layout_weight="1.9"
    203. android:orientation="vertical">
    204.  
    205. <TextView
    206. android:id="@+id/tvReceiverName"
    207. android:layout_width="wrap_content"
    208. android:layout_height="wrap_content"
    209. android:layout_alignParentTop="true"
    210. android:textSize="@dimen/fifteen_sp" />
    211.  
    212. <ImageView
    213. android:id="@+id/imgBluePointer"
    214. android:layout_width="wrap_content"
    215. android:layout_height="wrap_content"
    216. android:layout_alignParentRight="true"
    217. android:layout_centerVertical="true"
    218. android:src="@drawable/pointerblue" />
    219.  
    220. <LinearLayout
    221. android:id="@+id/llMainSent"
    222. android:layout_width="match_parent"
    223. android:layout_height="wrap_content"
    224. android:layout_marginTop="@dimen/five_dp"
    225. android:layout_below="@id/tvReceiverName"
    226. android:layout_centerVertical="true"
    227. android:layout_toLeftOf="@+id/imgBluePointer"
    228. android:background="@color/sent_message_color"
    229. android:orientation="vertical">
    230.  
    231.  
    232. <LinearLayout
    233. android:id="@+id/llSentVideoVisibility"
    234. android:layout_width="match_parent"
    235. android:layout_height="wrap_content"
    236. android:layout_marginBottom="@dimen/ten_dp"
    237. android:layout_marginLeft="@dimen/five_dp"
    238. android:layout_marginRight="@dimen/five_dp"
    239. android:layout_marginTop="@dimen/ten_dp"
    240. android:orientation="vertical"
    241. android:visibility="gone">
    242.  
    243. <RelativeLayout
    244. android:id="@+id/rlSentVideoDummy"
    245. android:layout_width="match_parent"
    246. android:layout_height="100dp"
    247. android:background="@android:color/black">
    248.  
    249. <ImageView
    250. android:id="@+id/ivSentMessageVideoReceived"
    251. android:layout_width="wrap_content"
    252. android:layout_height="wrap_content"
    253. android:layout_centerInParent="true"
    254. android:padding="@dimen/five_dp"
    255. android:src="@drawable/play" />
    256.  
    257. </RelativeLayout>
    258.  
    259. <TextView
    260. android:id="@+id/tvSentVideoCaption"
    261. android:layout_width="match_parent"
    262. android:layout_height="match_parent"
    263. android:background="@color/messages_screen_text_view"
    264. android:padding="@dimen/five_dp"
    265. android:textSize="@dimen/fifteen_sp" />
    266.  
    267. </LinearLayout>
    268.  
    269.  
    270. <com.ltj.fonts.RobotoMediumTextView
    271. android:id="@+id/tvSentMessages"
    272. android:layout_width="wrap_content"
    273. android:layout_height="wrap_content"
    274. android:layout_gravity="center_vertical"
    275. android:lineSpacingExtra="2dp"
    276. android:padding="@dimen/five_dp"
    277. android:textColor="@android:color/white"
    278. android:textSize="@dimen/thirteen_sp"
    279. android:visibility="gone" />
    280.  
    281. <RelativeLayout
    282. android:id="@+id/rlSentAudioMessageReceived"
    283. android:layout_width="match_parent"
    284. android:layout_height="100dp"
    285. android:layout_marginBottom="@dimen/ten_dp"
    286. android:layout_marginLeft="@dimen/five_dp"
    287. android:layout_marginRight="@dimen/five_dp"
    288. android:layout_marginTop="@dimen/ten_dp"
    289. android:background="@color/messages_screen_text_view"
    290. android:visibility="gone">
    291.  
    292. <ImageView
    293. android:id="@+id/ivSentMessageAudioReceived"
    294. android:layout_width="wrap_content"
    295. android:layout_height="wrap_content"
    296. android:layout_centerInParent="true"
    297. android:padding="@dimen/fifteen_dp"
    298. android:src="@drawable/voice"
    299. android:tag="play" />
    300.  
    301. </RelativeLayout>
    302.  
    303. <LinearLayout
    304. android:id="@+id/llSentImageVisibility"
    305. android:layout_width="match_parent"
    306. android:layout_height="wrap_content"
    307. android:layout_marginBottom="@dimen/five_dp"
    308. android:layout_marginLeft="@dimen/five_dp"
    309. android:layout_marginRight="@dimen/five_dp"
    310. android:orientation="vertical"
    311. android:visibility="gone">
    312.  
    313. <ImageView
    314. android:id="@+id/imgSentImage"
    315. android:layout_width="match_parent"
    316. android:layout_height="140dp"
    317. android:layout_gravity="center_vertical"
    318. android:layout_marginTop="@dimen/five_dp"
    319. android:adjustViewBounds="true"
    320. android:scaleType="centerCrop"
    321. android:textColor="@color/greyish"
    322. android:textSize="@dimen/thirteen_sp" />
    323.  
    324. <TextView
    325. android:id="@+id/tvSentMessageCaption"
    326. android:layout_width="match_parent"
    327. android:layout_height="match_parent"
    328. android:padding="@dimen/five_dp"
    329. android:textSize="@dimen/fifteen_sp" />
    330. </LinearLayout>
    331. </LinearLayout>
    332.  
    333.  
    334. <com.ltj.fonts.RobotoMediumTextView
    335. android:id="@+id/tvSentDate"
    336. android:layout_width="wrap_content"
    337. android:layout_height="wrap_content"
    338. android:layout_below="@+id/llMainSent"
    339. android:layout_marginTop="@dimen/fifteen_dp"
    340. android:textColor="@color/greyish" />
    341.  
    342.  
    343. </RelativeLayout>
    344.  
    345.  
    346. <RelativeLayout
    347. android:layout_width="80dp"
    348. android:layout_height="95dp"
    349. android:layout_alignParentRight="true"
    350. android:layout_gravity="center_vertical">
    351.  
    352. <com.ltj.utility.CircleImageView
    353. android:id="@+id/imgProfilePic"
    354. android:layout_width="wrap_content"
    355. android:layout_height="wrap_content"
    356. android:layout_centerVertical="true"
    357. android:src="@drawable/profile_pic" />
    358.  
    359. <ProgressBar
    360. android:id="@+id/pbProfileImage"
    361. android:layout_width="wrap_content"
    362. android:layout_height="wrap_content"
    363. android:layout_centerHorizontal="true"
    364. android:layout_centerVertical="true"
    365. android:visibility="gone" />
    366. </RelativeLayout>
    367.  
    368.  
    369. </LinearLayout>
    370.  
    371. </LinearLayout>
    372.  

     

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

     

    1. public class MessageHistoryRViewAdapter extends RecyclerView.Adapter<MessageHistoryRViewAdapter.ViewHolder> {
    2.  
    3. private Context context;
    4. private ArrayList<MessageHistory> messageHistoryList;
    5. private String type;
    6. private String status;
    7. private ProgressDialog progressDialog;
    8. private HashMap<String, String> mParams;
    9. private HashMap<String, String> mHeaders;
    10. private CommonHttpHandler commonHttpHandler;
    11. private Gson gson;
    12. private String user_Pic;
    13. private String my_pic;
    14. private String name;
    15. private String group_Name;
    16. private AppSharedPreferences appSharedPreferences;
    17.  
    18. public MessageHistoryRViewAdapter(Context context, ArrayList<MessageHistory> data, String profile_pic, String myPic, String person_name) {
    19. this.context = context;
    20. messageHistoryList = data;
    21. user_Pic = profile_pic;
    22. my_pic = myPic;
    23. name = person_name;
    24.  
    25. this.type = type;
    26. this.status = status;
    27.  
    28. initVariables();
    29. }
    30.  
    31. private void initVariables() {
    32.  
    33. progressDialog = new ProgressDialog(context);
    34. progressDialog.setMessage(AppConstants.sPleaseWait);
    35. progressDialog.setCancelable(false);
    36.  
    37. gson = new Gson();
    38. appSharedPreferences = AppSharedPreferences.getInstance(context);
    39. }
    40.  
    41. public class ViewHolder extends RecyclerView.ViewHolder {
    42.  
    43. ImageView ivSentMessageAudioReceived, ivSentMessageVideoReceived, imgReceivedProfilePic, imgProfilePic, imgReceivedImage, imgSentImage, ivReceivedMessageAudioReceived, ivMessageVideoReceived;
    44. TextView tvReceiverName, tvSenderName, tvReceivedMessage, tvSentMessage, tvReceivedDate, tvReceivedTime, tvSentDate, tvSentTime, tvSentVideoCaption, tvSentMessageCaption, tvMessageCaption, tvVideoCaption;
    45. LinearLayout llreceivedLayout, llSentLayout, llImageVisibility, llSentImageVisibility, llVideoVisibility, llSentVideoVisibility;
    46. ProgressBar pbReceivedProfileImage, pbProfileImage;
    47. RelativeLayout rlReceivedAudioMessageReceived, rlSentAudioMessageReceived;
    48.  
    49.  
    50. private ViewHolder(View itemLayoutView) {
    51. super(itemLayoutView);
    52. ivReceivedMessageAudioReceived = (ImageView) itemLayoutView.findViewById(R.id.ivReceivedMessageAudioReceived);
    53. ivMessageVideoReceived = (ImageView) itemLayoutView.findViewById(R.id.ivMessageVideoReceived);
    54. ivSentMessageVideoReceived = (ImageView) itemLayoutView.findViewById(R.id.ivSentMessageVideoReceived);
    55. ivSentMessageAudioReceived = (ImageView) itemLayoutView.findViewById(R.id.ivSentMessageAudioReceived);
    56.  
    57. llVideoVisibility = (LinearLayout) itemLayoutView.findViewById(R.id.llVideoVisibility);
    58. llSentVideoVisibility = (LinearLayout) itemLayoutView.findViewById(R.id.llSentVideoVisibility);
    59.  
    60. rlReceivedAudioMessageReceived = (RelativeLayout) itemLayoutView.findViewById(R.id.rlReceivedAudioMessageReceived);
    61. rlSentAudioMessageReceived = (RelativeLayout) itemLayoutView.findViewById(R.id.rlSentAudioMessageReceived);
    62.  
    63. llImageVisibility = (LinearLayout) itemLayoutView.findViewById(R.id.llImageVisibility);
    64. llSentImageVisibility = (LinearLayout) itemLayoutView.findViewById(R.id.llSentImageVisibility);
    65.  
    66.  
    67. imgReceivedProfilePic = (ImageView) itemLayoutView.findViewById(R.id.imgReceivedProfilePic);
    68. imgProfilePic = (ImageView) itemLayoutView.findViewById(R.id.imgProfilePic);
    69.  
    70. imgReceivedImage = (ImageView) itemLayoutView.findViewById(R.id.imgReceivedImage);
    71. imgSentImage = (ImageView) itemLayoutView.findViewById(R.id.imgSentImage);
    72.  
    73. tvSentVideoCaption = (TextView) itemLayoutView.findViewById(R.id.tvSentVideoCaption);
    74. tvSentMessageCaption = (TextView) itemLayoutView.findViewById(R.id.tvSentMessageCaption);
    75.  
    76. tvReceivedDate = (TextView) itemLayoutView.findViewById(R.id.tvReceivedDate);
    77. tvSenderName = (TextView) itemLayoutView.findViewById(R.id.tvSenderName);
    78. tvReceiverName = (TextView) itemLayoutView.findViewById(R.id.tvReceiverName);
    79.  
    80.  
    81. tvMessageCaption = (TextView) itemLayoutView.findViewById(R.id.tvMessageCaption);
    82. tvVideoCaption = (TextView) itemLayoutView.findViewById(R.id.tvVideoCaption);
    83.  
    84. tvSentDate = (TextView) itemLayoutView.findViewById(R.id.tvSentDate);
    85.  
    86. tvSentMessage = (TextView) itemLayoutView.findViewById(R.id.tvSentMessages);
    87. tvReceivedMessage = (TextView) itemLayoutView.findViewById(R.id.tvReceivedMessages);
    88.  
    89. llreceivedLayout = (LinearLayout) itemLayoutView.findViewById(R.id.llreceivedLayout);
    90. llSentLayout = (LinearLayout) itemLayoutView.findViewById(R.id.llSentLayout);
    91. pbProfileImage = (ProgressBar) itemLayoutView.findViewById(R.id.pbProfileImage);
    92. pbReceivedProfileImage = (ProgressBar) itemLayoutView.findViewById(R.id.pbReceivedProfileImage);
    93.  
    94.  
    95. }
    96. }
    97.  
    98. @Override
    99. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    100. View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_history_layout, parent, false);
    101. return new ViewHolder(itemLayoutView);
    102. }
    103.  
    104. @Override
    105. public void onBindViewHolder(final ViewHolder holder, final int position) {
    106.  
    107.  
    108. if (messageHistoryList.get(position).getMessage().getSender_id().equals(appSharedPreferences.getString(AppConstants.KEY_SHARED_PREFERENCE_USER_ID, ""))) {
    109.  
    110. holder.llSentLayout.setVisibility(View.VISIBLE);
    111. String date = parseDateToddMMYYYY(messageHistoryList.get(position).getMessage().getCreated());
    112. holder.tvSentDate.setText(date);
    113. holder.pbReceivedProfileImage.setVisibility(View.GONE);
    114. holder.llreceivedLayout.setVisibility(View.GONE);
    115.  
    116. if (messageHistoryList.get(position).getMessage().getGroup_id() != null && messageHistoryList.get(position).getMessage().getMessage().equalsIgnoreCase("You have an invitation from a group.")) {
    117. holder.tvReceiverName.setText(appSharedPreferences.getString(AppConstants.KEY_FIRST_NAME, "") + " " + appSharedPreferences.getString(AppConstants.KEY_LAST_NAME, ""));
    118.  
    119. } else {
    120. holder.tvReceiverName.setText(appSharedPreferences.getString(AppConstants.KEY_FIRST_NAME, "") + " " + appSharedPreferences.getString(AppConstants.KEY_LAST_NAME, "") + "(" + messageHistoryList.get(position).getGroup().getName() + ")");
    121.  
    122. }
    123.  
    124. if (messageHistoryList.get(position).getMessage().getAudio() != null) {
    125.  
    126. holder.rlSentAudioMessageReceived.setVisibility(View.VISIBLE);
    127. holder.llSentVideoVisibility.setVisibility(View.GONE);
    128. holder.llSentImageVisibility.setVisibility(View.GONE);
    129. holder.tvSentMessage.setVisibility(View.GONE);
    130.  
    131. } else if (messageHistoryList.get(position).getMessage().getVideo() != null) {
    132.  
    133. holder.llSentVideoVisibility.setVisibility(View.VISIBLE);
    134. holder.rlSentAudioMessageReceived.setVisibility(View.GONE);
    135. holder.llSentImageVisibility.setVisibility(View.GONE);
    136. holder.tvSentMessage.setVisibility(View.GONE);
    137.  
    138.  
    139. if (messageHistoryList.get(position).getMessage().getMessage() != null) {
    140. holder.tvSentVideoCaption.setText(messageHistoryList.get(position).getMessage().getMessage());
    141. } else {
    142. holder.tvSentVideoCaption.setVisibility(View.GONE);
    143. }
    144.  
    145. } else if (messageHistoryList.get(position).getMessage().getImage() != null) {
    146.  
    147. holder.llSentImageVisibility.setVisibility(View.VISIBLE);
    148. holder.rlSentAudioMessageReceived.setVisibility(View.GONE);
    149. holder.llSentVideoVisibility.setVisibility(View.GONE);
    150. holder.tvSentMessage.setVisibility(View.GONE);
    151.  
    152. loadImageInImageSend(messageHistoryList.get(position).getMessage().getImage(), holder.imgSentImage);
    153. if (messageHistoryList.get(position).getMessage().getMessage() != null) {
    154. holder.tvSentMessageCaption.setText(messageHistoryList.get(position).getMessage().getMessage());
    155. } else {
    156. holder.tvSentMessageCaption.setVisibility(View.GONE);
    157. }
    158. } else {
    159. holder.tvSentMessage.setVisibility(View.VISIBLE);
    160. holder.llSentImageVisibility.setVisibility(View.GONE);
    161. holder.rlSentAudioMessageReceived.setVisibility(View.GONE);
    162. holder.llSentVideoVisibility.setVisibility(View.GONE);
    163. holder.tvSentMessage.setText(messageHistoryList.get(position).getMessage().getMessage());
    164. }
    165.  
    166. String imgUrl = my_pic;
    167.  
    168. if (!imgUrl.equals("")) {
    169. holder.pbProfileImage.setVisibility(View.VISIBLE);
    170. Glide.with(context).load(imgUrl).diskCacheStrategy(DiskCacheStrategy.ALL).listener(new RequestListener<String, GlideDrawable>() {
    171. @Override
    172. public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
    173. holder.pbProfileImage.setVisibility(View.GONE);
    174. holder.imgProfilePic.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.profile_pic));
    175. return false;
    176. }
    177.  
    178. @Override
    179. public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
    180. holder.pbProfileImage.setVisibility(View.GONE);
    181. return false;
    182. }
    183. }).into(holder.imgProfilePic);
    184. } else {
    185. holder.pbProfileImage.setVisibility(View.GONE);
    186. holder.imgProfilePic.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.profile_pic));
    187. }
    188.  
    189.  
    190. } else {
    191.  
    192. holder.llreceivedLayout.setVisibility(View.VISIBLE);
    193. holder.llSentLayout.setVisibility(View.GONE);
    194. holder.pbReceivedProfileImage.setVisibility(View.GONE);
    195.  
    196. if (messageHistoryList.get(position).getMessage().getGroup_id() != null) {
    197. if (messageHistoryList.get(position).getMessage().getMessage().equalsIgnoreCase("You have an invitation from a group."))
    198. holder.tvSenderName.setText(name);
    199. else {
    200. holder.tvSenderName.setText(name + "(" + messageHistoryList.get(position).getGroup().getName() + ")");
    201. }
    202. } else
    203. holder.tvSenderName.setText(name);
    204.  
    205.  
    206. String date = parseDateToddMMYYYY(messageHistoryList.get(position).getMessage().getCreated());
    207. holder.tvReceivedDate.setText(date);
    208.  
    209.  
    210. if (messageHistoryList.get(position).getMessage().getAudio() != null) {
    211.  
    212. holder.rlReceivedAudioMessageReceived.setVisibility(View.VISIBLE);
    213.  
    214. holder.llVideoVisibility.setVisibility(View.GONE);
    215. holder.llImageVisibility.setVisibility(View.GONE);
    216. holder.tvReceivedMessage.setVisibility(View.GONE);
    217.  
    218. } else if (messageHistoryList.get(position).getMessage().getVideo() != null) {
    219.  
    220.  
    221. holder.llVideoVisibility.setVisibility(View.VISIBLE);
    222.  
    223. holder.rlReceivedAudioMessageReceived.setVisibility(View.GONE);
    224. holder.llImageVisibility.setVisibility(View.GONE);
    225. holder.tvReceivedMessage.setVisibility(View.GONE);
    226.  
    227. if (messageHistoryList.get(position).getMessage().getMessage() != null) {
    228. holder.tvVideoCaption.setText(messageHistoryList.get(position).getMessage().getMessage());
    229. } else {
    230. holder.tvVideoCaption.setVisibility(View.GONE);
    231. }
    232.  
    233. } else if (messageHistoryList.get(position).getMessage().getImage() != null) {
    234.  
    235.  
    236. holder.llImageVisibility.setVisibility(View.VISIBLE);
    237.  
    238. holder.rlReceivedAudioMessageReceived.setVisibility(View.GONE);
    239. holder.llVideoVisibility.setVisibility(View.GONE);
    240. holder.tvReceivedMessage.setVisibility(View.GONE);
    241.  
    242. loadImageInImageSend(messageHistoryList.get(position).getMessage().getImage(), holder.imgReceivedImage);
    243. if (messageHistoryList.get(position).getMessage().getMessage() != null) {
    244. holder.tvMessageCaption.setText(messageHistoryList.get(position).getMessage().getMessage());
    245. } else {
    246. holder.tvMessageCaption.setVisibility(View.GONE);
    247. }
    248. } else {
    249. holder.tvReceivedMessage.setVisibility(View.VISIBLE);
    250.  
    251. holder.rlReceivedAudioMessageReceived.setVisibility(View.GONE);
    252. holder.llVideoVisibility.setVisibility(View.GONE);
    253. holder.llImageVisibility.setVisibility(View.GONE);
    254.  
    255. holder.tvReceivedMessage.setText(messageHistoryList.get(position).getMessage().getMessage());
    256. }
    257.  
    258.  
    259. String imgUrl = user_Pic;
    260.  
    261. if (!imgUrl.equals("")) {
    262. holder.pbReceivedProfileImage.setVisibility(View.VISIBLE);
    263. Glide.with(context).load(imgUrl).diskCacheStrategy(DiskCacheStrategy.ALL).listener(new RequestListener<String, GlideDrawable>() {
    264. @Override
    265. public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
    266. holder.pbReceivedProfileImage.setVisibility(View.GONE);
    267. holder.imgReceivedProfilePic.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.profile_pic));
    268. return false;
    269. }
    270.  
    271. @Override
    272. public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
    273. holder.pbReceivedProfileImage.setVisibility(View.GONE);
    274. return false;
    275. }
    276. }).into(holder.imgReceivedProfilePic);
    277. } else {
    278. holder.pbReceivedProfileImage.setVisibility(View.GONE);
    279. holder.imgReceivedProfilePic.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.profile_pic));
    280. }
    281.  
    282. }
    283.  
    284. holder.imgSentImage.setOnClickListener(new View.OnClickListener() {
    285. @Override
    286. public void onClick(View v) {
    287.  
    288. if (holder.imgSentImage.getVisibility() == (View.VISIBLE)) {
    289. Intent imageShowingIntent = new Intent(context, ImageShowingActivity.class);
    290.  
    291.  
    292. imageShowingIntent.putExtra("imagePath", messageHistoryList.get(position).getMessage().getImage());
    293.  
    294.  
    295. context.startActivity(imageShowingIntent);
    296. }
    297.  
    298. }
    299. });
    300.  
    301. holder.imgReceivedImage.setOnClickListener(new View.OnClickListener() {
    302. @Override
    303. public void onClick(View v) {
    304. if (holder.imgReceivedImage.getVisibility() == (View.VISIBLE)) {
    305. Intent imageShowingIntent = new Intent(context, ImageShowingActivity.class);
    306. imageShowingIntent.putExtra("imagePath", messageHistoryList.get(position).getMessage().getImage());
    307. context.startActivity(imageShowingIntent);
    308. }
    309. }
    310. });
    311.  
    312. holder.ivReceivedMessageAudioReceived.setOnClickListener(new View.OnClickListener() {
    313. @Override
    314. public void onClick(View v) {
    315. if (holder.rlReceivedAudioMessageReceived.getVisibility() == (View.VISIBLE)) {
    316. Intent audioPlayIntent = new Intent(context, AudioPlayingActivity.class);
    317. audioPlayIntent.putExtra("audioPath", messageHistoryList.get(position).getMessage().getAudio());
    318. context.startActivity(audioPlayIntent);
    319. }
    320. }
    321. });
    322. holder.ivMessageVideoReceived.setOnClickListener(new View.OnClickListener() {
    323. @Override
    324. public void onClick(View v) {
    325.  
    326. if (holder.llVideoVisibility.getVisibility() == (View.VISIBLE)) {
    327. Intent videoPlayingIntent = new Intent(context, VideoPlayingActivity.class);
    328. videoPlayingIntent.putExtra("videoPath", messageHistoryList.get(position).getMessage().getVideo());
    329. context.startActivity(videoPlayingIntent);
    330. }
    331. }
    332. });
    333. holder.ivSentMessageVideoReceived.setOnClickListener(new View.OnClickListener() {
    334. @Override
    335. public void onClick(View v) {
    336.  
    337. if (holder.llSentVideoVisibility.getVisibility() == (View.VISIBLE)) {
    338. Intent videoPlayingIntent = new Intent(context, VideoPlayingActivity.class);
    339.  
    340. videoPlayingIntent.putExtra("videoPath", messageHistoryList.get(position).getMessage().getVideo());
    341.  
    342. context.startActivity(videoPlayingIntent);
    343. }
    344. }
    345. });
    346. holder.ivSentMessageAudioReceived.setOnClickListener(new View.OnClickListener() {
    347. @Override
    348. public void onClick(View v) {
    349.  
    350. if (holder.rlSentAudioMessageReceived.getVisibility() == (View.VISIBLE)) {
    351. Intent audioPlayIntent = new Intent(context, AudioPlayingActivity.class);
    352.  
    353. audioPlayIntent.putExtra("audioPath", messageHistoryList.get(position).getMessage().getAudio());
    354.  
    355. context.startActivity(audioPlayIntent);
    356. }
    357. }
    358. });
    359.  
    360.  
    361. }
    362.  
    363. @Override
    364. public int getItemCount() {
    365. return messageHistoryList.size();
    366. }
    367.  
    368.  
    369. private void loadImageInImageSend(String profile_image, final ImageView imageView) {
    370.  
    371. Glide.with(context).load(profile_image).diskCacheStrategy(DiskCacheStrategy.ALL).listener(new RequestListener<String, GlideDrawable>() {
    372. @Override
    373. public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
    374. Toast.makeText(context, "Failed to get Image From server..Please try again", Toast.LENGTH_SHORT).show();
    375. return false;
    376. }
    377.  
    378. @Override
    379. public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
    380. return false;
    381. }
    382. }).into(imageView);
    383.  
    384. }
    385.  
    386.  
    387. private String parseDateToddMMYYYY(String time) {
    388.  
    389. String inputPattern = "yyyy-MM-dd HH:mm:ss";
    390. String outputPattern = "dd-MMM h:mm a";
    391. SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
    392. SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
    393.  
    394. Date date;
    395. String str = null;
    396.  
    397. try {
    398. date = inputFormat.parse(time);
    399. str = outputFormat.format(date);
    400. } catch (ParseException e) {
    401. e.printStackTrace();
    402. }
    403. return str;
    404. }
    405. }
    406.  

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: