5

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.

John
  • 1,566
  • 6
  • 17
  • 28

6 Answers6

11

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"/>
Malcolm
  • 41,014
  • 11
  • 68
  • 91
user
  • 86,916
  • 18
  • 197
  • 190
  • the above works but how can I handle showing the scroll bar to show on the left as it now shows over the items – spacemonkey Dec 20 '13 at 13:11
  • 1
    @spacemonkey You can't move the scrollbars on the left side. Can't you just set some decent left padding on the root of the row layout so the scrollbars will have some space and will not overlap the content? – user Dec 20 '13 at 13:17
  • Done. Thanks mate appreciated :) – spacemonkey Dec 20 '13 at 13:35
3

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.

Malcolm
  • 41,014
  • 11
  • 68
  • 91
2

Add the below to the layout

android:layoutDirection="rtl"
Bahaa Hany
  • 744
  • 13
  • 22
1

This should suffice

android:layout_gravity="right" 
dLobatog
  • 1,751
  • 16
  • 17
  • Thanks for the reply but I have tried this already. This doesn't change the alignment of the text inside the ListView. – John Feb 23 '12 at 19:53
  • 1
    You need to put it in the layout id you assign to your adapter. You will have to make one, instead of specifying android.R.layout.simple_list_item_1 (probably what you have?) – Tyler Feb 23 '12 at 19:57
0

For Expandable list view we create two row XMLs namely One is group_row.xml and Other is child_row.xml.

make group_row.xml as

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/row_group_name"
    android:layout_width="wrap_content"
    android:textSize="24sp"
    android:layout_alignParentRight="true"
    android:textAllCaps="true"
    android:layout_height="wrap_content" />

That is, a Text view aligned to parentRight inside a Relative Layout (which is match parent).

After 2 hours of struggling, I achieved this a moment ago.

image after enter image description here

sud007
  • 5,824
  • 4
  • 56
  • 63
0

Pay attention to one small thing.

If you use listitem inside the listview, the gravity attribute of the listitem might not work if the listview layout:width is not set to match_parent.

Bandreid
  • 2,727
  • 28
  • 47
Gilad Levinson
  • 234
  • 2
  • 12