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.

greysqrl
  • 937
  • 3
  • 13
  • 31
  • 1
    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 android – MoQ93 Jan 06 '16 at 19:51
  • 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? – greysqrl Jan 06 '16 at 20:00
  • You're welcome. you can mark H. Bhonsle's answer as accepted ;) – MoQ93 Jan 06 '16 at 20:14

3 Answers3

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" />
Sanjeet A
  • 5,171
  • 3
  • 23
  • 40
  • Hi. Thank you. The problem was that I was appending 'android.' and thus accessing the Android rather than my custom spinner. – greysqrl Jan 07 '16 at 11:52
  • 2
    That was not the problem, You have customized the item layout and and dropdown layouts. You can do without touching the layout xmls. – Sanjeet A Jan 07 '16 at 16:49
  • 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. – greysqrl Jan 08 '16 at 17:16
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.

MoQ93
  • 341
  • 2
  • 12
H. Bhonsle
  • 136
  • 5
  • 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. – greysqrl Jan 06 '16 at 20:01
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);
Shivam Agarwal
  • 133
  • 1
  • 6