How to right-align Android spinner
Asked Answered
T

3

7

I'm trying to right-align the text of an Android spinner. I have been through Stack Overflow and tried the recommended solution but it's not working for me so, I'm a little confused as to what I have done wrong. My feeling is that my layout is not being correctly picked up due to an error I have made.

My activity.xml file

<Spinner
android:id="@+id/spinnerStoreType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>

Under res\layout I have created a file called simple_spinner_item.xml

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

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="40sp"
    android:textColor="#FFFFFF"
    android:padding="10dip"
    android:gravity="right"
    />

Finally, in my activity I am using this as follows:

spinnerStoreType = (Spinner) findViewById(R.id.spinnerStoreType);
ArrayAdapter<CharSequence> adapter =    
ArrayAdapter.createFromResource(this,
R.array.transaction_store_array, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinnerStoreType.setAdapter(adapter);

So, I believe that the simple_spinner_item being used is the default one, Android's one. I'm unclear how to get it to use my custom one. I thought that would happen automatically if it has the same name?

Any help as always is very much appreciated.

Thalassa answered 6/1, 2016 at 19:44 Comment(3)
use R.layout.simple_spinner_item.xml instead of android.R.simple_spinner_item.xml to use your own layout. You can also name it something else, it has not to be the same as in androidProgramme
So, using android. was causing it not use my layout basically. Thank you very much for your help. Since you've commented though I'm not sure how to 'accept' this answer?Thalassa
You're welcome. you can mark H. Bhonsle's answer as accepted ;)Programme
T
3

android.R.layout.simple_spinner_item refers to the spinner item from the android library and will not have your custom properties. You should be referring to your own layout by using R.layout.simple_spinner_item.

Trismus answered 6/1, 2016 at 19:59 Comment(1)
Hi. Thank you for taking the time to answer this. The comment above from MoQ helped me resolve this so I'd like to mark that as the answer if possible. I do however appreciate your help. Thank you.Thalassa
H
10

You can achieve this completely using the styles. And I think that's the best way. Here is the code from my styles.xml-

    <style name="AppTheme.Spinner">
        <item name="android:spinnerItemStyle">@style/SpinnerItem</item>
        <item name="android:spinnerDropDownItemStyle">@style/SpinnerDropDownItem</item>
    </style>

    <style name="SpinnerItem">
        <item name="android:gravity">right|center_vertical</item>
        <item name="android:paddingRight">16dp</item>
    </style>

    <style name="SpinnerDropDownItem">
        <item name="android:gravity">right|center_vertical</item>
        <item name="android:paddingRight">16dp</item>
    </style>

and here is the implementation to a spinner-

       <Spinner
            android:id="@+id/type"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_below="@id/title"
            android:entries="@array/data"
            android:theme="@style/AppTheme.Spinner" />
Hafiz answered 6/1, 2016 at 20:35 Comment(3)
Hi. Thank you. The problem was that I was appending 'android.' and thus accessing the Android rather than my custom spinner.Thalassa
That was not the problem, You have customized the item layout and and dropdown layouts. You can do without touching the layout xmls.Hafiz
Actually that WAS the problem, which has now been fixed. What you have proposed is an alternative to my implementation, not a fix to my issue. The previous answer fixed my issue, yours is merely an alternative method, not a solution to the problem I had.Thalassa
T
3

android.R.layout.simple_spinner_item refers to the spinner item from the android library and will not have your custom properties. You should be referring to your own layout by using R.layout.simple_spinner_item.

Trismus answered 6/1, 2016 at 19:59 Comment(1)
Hi. Thank you for taking the time to answer this. The comment above from MoQ helped me resolve this so I'd like to mark that as the answer if possible. I do however appreciate your help. Thank you.Thalassa
R
0

If you want to use android.R.layout.simple_spinner_item as spinner item, add spinnerStoreType.setGravity(Gravity.RIGHT) to your java code for right align. So, your code can go like:

spinnerStoreType = (Spinner) findViewById(R.id.spinnerStoreType);
spinnerStoreType.setGravity(Gravity.RIGHT);   
ArrayAdapter<CharSequence> adapter =    
ArrayAdapter.createFromResource(this,
R.array.transaction_store_array, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinnerStoreType.setAdapter(adapter);
Raymer answered 23/5, 2020 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.