Jeremy
Jeremy

Reputation: 1337

XML Spinner attributes textAlignment = "right" API 16

I'm trying to set attributes to a spinner on my android app and one of the attributes in textAlignment = "right" (the problem being I've a spinner that layout_width="match_parent" so there's a lot a space and I'd like to have it to the right) but this is only supported in API 17 and up whereas I want to make an app for API 16 - Is there a work-around?

My attributes are:

        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:textAlignment="gravity"
            android:id="@+id/MainSpinner"
            tools:listitem="@layout/support_simple_spinner_dropdown_item"/>

The current linearLayout looks like this:

enter image description here

And I want it to look like this:

enter image description here

Where:

LinearLayout (horizontal) = enter image description here

RelativeLayout = enter image description here

TextView = enter image description here

LinearLayout (Vertical) = enter image description here

Spinner = enter image description here

Button = enter image description here

Upvotes: 8

Views: 1471

Answers (3)

MrZ
MrZ

Reputation: 560

You can create an adapter like this :

SpinnerAdapter spinnerAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item, new String[]{"tata", "toto", "titi"});
spriner.setAdapter(spinnerAdapter );

and in your layout (res/layout/spinner_item.xml):

<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|end"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />

Upvotes: 2

MashukKhan
MashukKhan

Reputation: 1954

Heyy Jeremy, Try to add android:textDirection="rtl" attribute in your spinner. This attribute will give the text a direction from right to left.

<Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:textAlignment="gravity"
        android:textDirection="rtl"
        android:id="@+id/MainSpinner"
        tools:listitem="@layout/support_simple_spinner_dropdown_item"/> 

Upvotes: 0

Mahesh Kedari
Mahesh Kedari

Reputation: 142

This ans work for me...

<Spinner
android:id="@+id/example_spinner"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:textAlignment="right"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="2dp"
android:paddingBottom="2dp" />

I've followed this : http://nevescheng.blogspot.fr/2013/05/spinner-with-item-text-aligned-to-center.html & this worked fine...

Upvotes: 3

Related Questions