10

I could not change spinner's textsize or colour with the code below:

<Spinner 
    android:id="@+id/spinner1"
    style="@style/submitspinner"
    android:layout_weight="2"
    android:entries="@array/a_code"
    android:prompt="@string/p_code" />

style:

<style name="submitspinner" parent="@android:TextAppearance.Widget.TextView.SpinnerItem">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_margin">10sp</item> 
    <item name="android:textColor">@android:color/holo_blue_dark</item>
    <item name="android:textSize">@dimen/pt</item> 
</style>

It looks same, how can I increase textsize and change colour of the spinner?

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
John simit
  • 1,305
  • 2
  • 11
  • 14

4 Answers4

41

Via XML Only

Just to help others in case they are statically setting their Spinner entries in XML.

The above answers work if you're creating your Spinner via code but if you're setting your Spinner entries via XML, i.e. using android:entries, then you can adjust the text size and other attributes with the following two theme settings:

In your res/values/styles.xml

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

    <style name="AppBaseTheme" parent="android:Theme.Holo">
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">

        <!-- For the resting Spinner style -->
        <item name="android:spinnerItemStyle">
            @style/spinnerItemStyle
        </item> 

        <!-- For each individual Spinner list item once clicked on -->
        <item name="android:spinnerDropDownItemStyle">
            @style/spinnerDropDownItemStyle
        </item>

    </style>

    <style name="spinnerItemStyle">
        <item name="android:padding">10dp</item>
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">#FFFFFF</item>
    </style>

    <style name="spinnerDropDownItemStyle">
        <item name="android:padding">20dp</item>
        <item name="android:textSize">30sp</item>
        <item name="android:textColor">#FFFFFF</item>
    </style>

</resources>
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
13

What you should do for custom Spinners is create a single XML that will act as your template for the string in the Spinner, like so:

<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/standard_spinner_format"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textSize="@dimen/pt"
   android:textColor="@android:color/holo_blue_dark"/>

Then when you create your Spinner adapter in java do the following:

ArrayAdapter<CharSequence> typeAdapter = ArrayAdapter.createFromResource(getActivity(), 
          R.array.my_spinner_array, R.layout.custom_xml_spinner_layout); //change the last argument here to your xml above.
    typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
TronicZomB
  • 8,667
  • 7
  • 35
  • 50
  • The OP is using static entries via `android:entries` to populate his Spinner. There's a way to style the Spinners without setting the adapter via code. I added an answer to show how. – Joshua Pinter Jan 16 '14 at 22:32
1

customize your spinner

here is a good tutorial i once used

http://stephenpengilley.blogspot.be/2013/01/android-custom-spinner-tutorial.html

it has everything u need.

also in your 'style' u have:

<item name="android:layout_margin">10sp</item>

change it to 10dp

sp = text size

here u can find more about units to measure

What is the difference between "px", "dp", "dip" and "sp" on Android?

Community
  • 1
  • 1
0
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="@style/spinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_5"
android:ellipsize="marquee"
android:layoutDirection="ltr"
android:padding="@dimen/padding_5"
android:singleLine="true"
android:textSize="@dimen/textsize_base" />

 adapter?.setDropDownViewResource(R.layout.spinner_text)
spSourceAccount.adapter = adapter