Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 316
Jeremy Avatar asked Sep 28 '16 21:09

Jeremy


2 Answers

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...

like image 189
Mahesh Kedari Avatar answered Nov 07 '22 11:11

Mahesh Kedari


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" />
like image 2
MrZ Avatar answered Nov 07 '22 12:11

MrZ