How to Align ListView Text to Right in Android?
Asked Answered
I

6

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.

Impostume answered 23/2, 2012 at 19:46 Comment(1)
Thanks to all for your help. I've got it working nowImpostume
C
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"/>
Collogue answered 23/2, 2012 at 19:59 Comment(2)
the above works but how can I handle showing the scroll bar to show on the left as it now shows over the itemsInsolation
@Insolation 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?Collogue
B
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.

Brattice answered 23/2, 2012 at 19:48 Comment(0)
G
2

Add the below to the layout

android:layoutDirection="rtl"
Genvieve answered 5/4, 2015 at 20:57 Comment(0)
T
1

This should suffice

android:layout_gravity="right" 
Tiler answered 23/2, 2012 at 19:48 Comment(2)
Thanks for the reply but I have tried this already. This doesn't change the alignment of the text inside the ListView.Impostume
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?)Glindaglinka
C
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

Corridor answered 14/7, 2015 at 17:38 Comment(0)
L
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.

Larkspur answered 28/3, 2016 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.