2

I want to implement interface like iphone sms:

how to do

Now I have chat listview with my custom ChatAdapter, incoming and outcoming messages inflates from different layouts:

outcoming layout:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:background="@drawable/question_bubble"
    android:paddingBottom="10dp"
    android:paddingLeft="20dp"
    android:paddingRight="27dp"
    android:paddingTop="10dp"
    android:textColor="@color/BlackColor"
    android:textSize="18sp" />

incoming layout:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:autoLink="all"
    android:background="@drawable/answer_bubble"
    android:paddingLeft="27dp"
    android:paddingRight="20dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"    
    android:textColor="@color/BlackColor"
    android:textSize="18sp"/>

I use "layout_gravity", but it doesnt work inside list view. Result:

my interface

How can I align question messages to right side?

4 Answers 4

8

Add a wrapper layout to your item layouts (for example a LinearLayout) that will have layout_width="match_parent" and gravitiy="left"/"right", this will adjust the inner Layout/TextView to the right position.

In general it is highly recommended to not use wrap_content for items in a ListView, this makes the ListView's onMeasure method to get repeatedly called and slow your app down.

1
  • 1
    thanks, its a good solution. also thanks for optimization hint.
    – Sash0k
    May 31, 2012 at 11:23
1
You can try this one:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    **android:layout_width="fill_parent"**
    android:layout_height="wrap_content"
    **android:gravity="right"**
1
  • hmm, its a correct way to. and it even more efficient, I suppose. But only for timestamp labels. Bubble images fill all row with this way
    – Sash0k
    May 31, 2012 at 11:39
1

I've been working on this problem also at the moment. Couldn't get it to work with the suggestions mentioned here. For me it worked only by using:linearlayout.setGravity(Gravity.RIGHT); and setGravity(Gravity.LEFT); programmatically after inflating a standard layout in the getView method of the adapter. These linearlayouts have a width of wrap_content and are child of linearlayout that has match_parent for width. Also don't forget to do this every time a view is being requested via getView, not only if convertView==null.

0

if you encounter such problem make sure that your text layout is set in this format

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bubble_layout_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
    android:id="@+id/bubble_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@drawable/chat_bubble_right">

    <TextView
        android:id="@+id/message_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:maxEms="12"
        android:text="Hi! new message"
        android:textColor="@color/colorWhite"
        android:textSize="15sp" />
</LinearLayout>

notice that the parent LinearLayout width is set to match_parent while others are set to wrap_content. this kept me up for hours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.