11

I can add underline in spinner using style="@style/Base.Widget.AppCompat.Spinner.Underlined". How can I change the color of the underline using style only? I don't want to use any drawable file to change this.

 <item name="colorControlHighlight">@color/colorAccent</item>
 <item name="colorControlNormal">@color/colorAccent</item>

Using above style, Its only highlight underline when user click on it. Its not changing color of underline on normal state.

Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44

3 Answers3

39

By default the Spinner will use the color set via android:textColorSecondary or colorControlNormal in your AppTheme. So either set the appropriate colors there or define a new Theme and apply this one to your Spinner:

Example:

styles.xml

<style name="ThemeSpinner">
    <!-- Color when pressed -->
    <item name="colorAccent">#ffa000</item>
    <!-- Default color for the dropdown arrow and line -->
    <item name="colorControlNormal">#ffc107</item>
</style>

layout.xml

<Spinner
    style="@style/Widget.AppCompat.Spinner.Underlined"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeSpinner" />

Note: The dropdown arrow will also be tinted - I'm not aware of an option to color the arrow separately

Community
  • 1
  • 1
reVerse
  • 35,075
  • 22
  • 89
  • 84
4

Add this to spinner

android:backgroundTint="@color/gray"
Zoe
  • 27,060
  • 21
  • 118
  • 148
Ananta Prasad
  • 3,655
  • 3
  • 23
  • 35
3

Seems like this question has already been answered. But here is a way to resolve that programatically as well. (Tested on API 19 & Above).

Use ViewCompat for this.

ViewCompat.setBackgroundTintList(spinner, ColorStateList.valueOf(your_color));
sud007
  • 5,824
  • 4
  • 56
  • 63
  • it applies color but in an unexpected way, because it applies it on top of the original color – Angelina Oct 10 '19 at 08:52
  • This has worked for me when I last tried, but may need some fiddling with your UI, because android Compat libs and components have changed since I last used. – sud007 Oct 10 '19 at 10:20