2

I am trying to create a chat application with RecyclerView for displaying the list of messages in chatBubble form.

In recyclerView, in each row layout I have two text views. One for displaying the message and other for displaying the timestamp. For short messages, it works. However, for long messages, the chat bubble grows too big and the corresponding TextView for displaying timestamp can't be seen in this case.

  1. Why is this happening and how to correct this,
  2. Also, the space between each item in RecyclerView needs to be increased, I tried using android:dividerHeight="12dp" but it didn't work.
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Prateek Ratnaker
  • 817
  • 11
  • 35
  • `android:layout_toRightOf="@+id/message` is the issue. when textview occupies whole parent, ur time is going towards right that is out of screen – Mohammed Atif Jan 17 '17 at 17:13

1 Answers1

1

As @Mohammed Atif commented, instead of using

android:layout_toRightOf="@+id/message

use this

android:layout_alignParentRight="true"

Now, for adding space between recyclerview's items, you need to add itemDecorator

public class VerticalSpaceItemDecoration extends RecyclerView.ItemDecoration {

private final int mVerticalSpaceHeight;

public VerticalSpaceItemDecoration(int mVerticalSpaceHeight) {
    this.mVerticalSpaceHeight = mVerticalSpaceHeight;
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                           RecyclerView.State state) {
    if (parent.getChildAdapterPosition(view) != parent.getAdapter().getItemCount() - 1) {
        outRect.bottom = mVerticalSpaceHeight;
    }
}

}

Then add this item decorator to recyclerview like this

recyclerview.addItemDecoration(new VerticalSpaceItemDecoration(2));

Here, 2 is the space between the recyclerview list items.

Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103
  • android:layout_alignParentRight="true" on doing this,the whole relative layout android:id="@+id/messageContainer" expands to full width of the screen....the balloon for chat message occupies the whole width in such case – Prateek Ratnaker Jan 18 '17 at 02:02
  • Yes, that's true. You have to fix the width of the chat balloon. I think this is what you will need as in most of the chat apps the chat balloon does not occupy the full width – Malwinder Singh Jan 18 '17 at 02:19
  • there is another way, instead of using relative layout, use linear layout and set time textview gravity to right. that will solve your problem – Malwinder Singh Jan 18 '17 at 02:23
  • i fixed the maxwidth of textview android:id="@+id/message".....however now i was working on spacing between two items in a recycler view ...i tried using android:divider="@android:color/transparent" android:dividerHeight="12dp" and recyclerview.addItemDecoration(new VerticalSpaceItemDecoration(12)); suggested by you but both of them didn't work .....why is this happening? and how to do this? – Prateek Ratnaker Jan 18 '17 at 03:02
  • dividerHeight or divider won't work in recyclerview, they work in listview only. Are you sure the given itemDecoratorm code is not working? Its a working code in my project. Try to increase the height from 12. Also note that this height is in pixel and not dp. – Malwinder Singh Jan 18 '17 at 03:10
  • i made it to 120 but still it didn't work....please check my updated code and snapshot – Prateek Ratnaker Jan 18 '17 at 03:32
  • Your code looks fine to me, try setting itemDecorator before setting the adaper – Malwinder Singh Jan 18 '17 at 03:39
  • I set the itemdecorator before setting adapter and after setting layout manager but unfortunately that didn't work – Prateek Ratnaker Jan 18 '17 at 15:24
  • I am sorry for wasting so much precious time of yours. I have updated my answer. Please check again the ItemDecorator class. We have got the space to add to the child views but haven't added it to the views. – Malwinder Singh Jan 18 '17 at 16:42