Android Spinner Underline color
Asked Answered
H

3

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.

Hatcher answered 9/5, 2016 at 6:21 Comment(0)
L
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

Lakisha answered 9/5, 2016 at 10:34 Comment(6)
This won't work if the Spinner is inside a ScrollView.Arillode
what about the red color for the error? Is there a way to get it?Perceive
@Perceive I've never seen a Spinner with an error state honestlyLakisha
Just try to add a new account in any of the Google app. It will ask you for the date of birth. Enter an invalid date and then press next...Perceive
I asked a question about this: #39793221Perceive
Works PERFECTLY with this implementation of style: #9477165Titanium
O
4

Add this to spinner

android:backgroundTint="@color/gray"
Ore answered 25/3, 2019 at 7:14 Comment(0)
M
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));
Martinmas answered 25/10, 2017 at 8:40 Comment(2)
it applies color but in an unexpected way, because it applies it on top of the original colorInextinguishable
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.Martinmas

© 2022 - 2024 — McMap. All rights reserved.