Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Spinner text alignment to the right but not centered

I finished the English version of my application and now I am working on the Arabic localization. Arabic is a right-to-left language, so I need to adjust a lot of things in my layout, including my spinner display.

I used the same approach mentioned here Android - Text is Pushed to the Left in a Spinner but I set the gravity to right.

Here is my spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:layout_width="fill_parent"
    android:gravity="right" />

Changed my code from

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

to this

adapter.setDropDownViewResource(R.layout.spinner_item);

I want my spinner to look like my old English spinner found below,

English spinner

But it currently looks like this,

Arabic spinner

How can I restore the following:

  1. The radio button
  2. The bigger text size
  3. I want it to the right as shown but centered between the two dividers.

NOTE The Arabic webservices aren't finished yet, that's why the data in the image is still in English.

UPDATE

After trying Adil Soomro's suggestion, I get the following,

Spinner semi-desired

There is no radiobutton and there is a considerable space between the border and the first letter.

UPDATE

After Adil Soomro's edit, I now have the following,

Spinner Arabic Fixed

like image 769
Nouran H Avatar asked Jul 05 '12 09:07

Nouran H


People also ask

How do you right align text on android?

To right align text in TextView in Kotlin Android, set android:textAlignment attribute with the value “viewEnd” in layout file, or programmatically set the textAlignment property of the TextView object with View.

How do you align text in layout?

android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered. android:layout_centerInParent="true" if you want TextView in center position of parent view.

How do I align text in text view?

Kotlin Android – Center Align text in TextView To center align text in TextView in Kotlin Android, set android:textAlignment attribute with the value “center” in layout file, or programmatically set the textAlignment property of the TextView object with View. TEXT_ALIGNMENT_CENTER in activity file.

What are android spinners?

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.


2 Answers

You have to use CheckedTextView, it will solve your all 3 problems.

You will place a layout xml something like this:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    android:drawableLeft="?android:attr/listChoiceIndicatorSingle"
    android:gravity="right|center_vertical"
    android:singleLine="true"
    android:layout_width="fill_parent"
    android:textSize="20dp"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee" />
like image 152
Adil Soomro Avatar answered Oct 02 '22 15:10

Adil Soomro


Try this:

android:layout_gravity="right"
like image 38
Ankitkumar Makwana Avatar answered Oct 02 '22 15:10

Ankitkumar Makwana