In this line:
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, itemList)
Create a new custom xml file in res/layout [say called your_layout_name.xml
] and replace the android.R.layout.simple_list_item_1
in the code above with "R.layout.your_layout_name". In that file, put in the following code:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical|right"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
Explanation
Here is the original android.R.layout.simple_list_item_1
layout which, in that ArrayAdapter code, defines the layout of the row:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
All that needed to be changed was the "gravity" parameter. That defines the alignment of the text, and all you needed to add was "right" to it.
Note: The gravity parameter normally aligns all of its contents in any View. Say, for a LinearLayout, this would try to align all of its child elements. For a TextView, it aligns its text.