Spinner drop down menu items color (Android)
Asked Answered
N

2

6

So I have a spinner and I was successful in changing the color of the selected item but I am not able to change the color of the items in the drop down menu

This is my spinner_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView  
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content"
   android:textSize="13sp" 
   android:textColor="#33CCFF"         
/>

and this is my styles.xml

<resources>
  <style name="AppTheme" parent="android:Theme.Light" >
     <item name="android:spinnerDropDownItemStyle">@style/SpinnerItem.DropDownItem.Color</item>
     <item name="android:spinnerItemStyle">@style/SpinnerItem</item>
  </style>
  <style name="SpinnerItem.DropDownItem.Color" parent="@android:style/Widget.DropDownItem.Spinner">
      <item name="android:textColor">#4FBDE8</item>
  </style>

  <style name="SpinnerItem" parent="@android:style/Widget.TextView.SpinnerItem">
      <item name="android:textColor">#4FBDE8</item>
  </style>
</resources>

is there an XML way I can do it in ?

Novia answered 1/10, 2012 at 20:32 Comment(0)
A
3

Here is the solution I found on another stackoverflow thread

<style name="Theme.NoTitleBar.WithColoredSpinners" parent="@android:style/Theme.NoTitleBar">
    <item name="android:spinnerItemStyle">@style/SpinnerItem</item>
    <item name="android:spinnerDropDownItemStyle">@style/SpinnerItem.DropDownItem</item>
</style>

<style name="SpinnerItem" parent="@android:style/Widget.TextView.SpinnerItem">
    <item name="android:textColor">#00FF00</item>
</style>

<style name="SpinnerItem.DropDownItem" parent="@android:style/Widget.DropDownItem.Spinner">
    <item name="android:textColor">#FF0000</item>
</style>

Aesthetic answered 29/10, 2012 at 2:36 Comment(0)
C
1

While specifying a layout resource file for the spinner, you have to set it at two places.

  1. While declaring a new ArrayAdapter

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item, categories);
    
  2. While setting the dropDownViewResource on the array adapter.

    dataAdapter.setDropDownViewResource(R.layout.spinner_item_dropdown);
    

Notice that two different layout files have been used. You can customize your views in the following manner, by defining the styles and using these styles as theme in the respective layouts.

<style name="SpinnerItem" parent="@android:style/Widget.TextView.SpinnerItem">
    <item name="android:textColor">@color/white</item>
    <item name="android:background">@color/black</item>
</style>

<style name="SpinnerItem.DropDownItem" parent="@android:style/Widget.DropDownItem.Spinner">
    <item name="android:textColor">@color/black</item>
</style>

Hope this helps.

Cuddle answered 14/4, 2016 at 6:54 Comment(4)
It worked for me. Could you tell the exact problem you are facing?Cuddle
Nothing really happens, the text color not the back ground changesClient
Post your code here. Most probably you are not using the styles correctly.Cuddle
So, I made all according to your answer , but I don't understand exactly, where I need to set this custom style... In TextView from R.layout.spinner_item_dropdown ?Simasimah

© 2022 - 2024 — McMap. All rights reserved.