Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

left and right alignment rows inside Listview

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?

like image 548
Sash0k Avatar asked Aug 10 '26 05:08

Sash0k


2 Answers

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.

like image 104
marmor Avatar answered Aug 12 '26 12:08

marmor


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"**
like image 24
Amit Singh Avatar answered Aug 12 '26 12:08

Amit Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!