Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Align ListView Text to Right in Android?

I have a list view in an Android application used to display an ArrayList containing Strings and I can't find any properties of the ListView that would allow me to align the text of the ListView Items to the right of it instead of the left. Any help would be great, thanks.

like image 854
John Avatar asked Feb 23 '12 19:02

John


4 Answers

The answer depends on how exactly do you build the list. If you use the the standard ArrayAdapter then you could use the constructor:

ArrayAdapter<String>(Context, R.layout.aligned_right, values);

where R.layout.aligned_right is a xml layout like this:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1" 
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"                    
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:paddingLeft="6dip"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="right"/>
like image 165
user Avatar answered Nov 12 '22 07:11

user


This is because these parameters are not defined in the ListView, but in the ListAdapter instead. When you specify an adapter for the list, you should set it to return such a layout which aligns items on the right.

like image 22
Malcolm Avatar answered Nov 12 '22 09:11

Malcolm


Add the below to the layout

android:layoutDirection="rtl"
like image 2
Bahaa Hany Avatar answered Nov 12 '22 09:11

Bahaa Hany


This should suffice

android:layout_gravity="right" 
like image 1
dLobatog Avatar answered Nov 12 '22 09:11

dLobatog