Change colour of small triangle on spinner in android
Asked Answered
L

9

60

enter image description here

How can I change the colour of small triangle at the bottom right corner of spinner like shown in the image? It is showing default grey colour right now. like this

enter image description here

Lobotomy answered 18/9, 2014 at 11:19 Comment(4)
Put custom background to it as per requirement.Gloucestershire
Just a small triangle in image? There must be an image with grey colour in sdk. Can you provide its link so that I can copy paste it and change its colour only.Lobotomy
Hope, it helps you... gersic.com/blog.php?id=57Gloucestershire
@Ricky that is a good article that explains the process very clearly. I have included it at the bottom of my answer, with credit to you.Luxate
L
19

To get the correct images, you can go to Android Asset Studio site and choose the Android Holo Colors Generator. This will create all the assets you might need, including the "triangle". It also generates the XML files that implement the changes.


Here are the example XML files:

Custom Color:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="apptheme_color">#33b5e5</color>
</resources>

Custom Spinner Style:

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

<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">

  <style name="SpinnerAppTheme" parent="android:Widget.Spinner">
      <item name="android:background">@drawable/apptheme_spinner_background_holo_light</item>
      <item name="android:dropDownSelector">@drawable/apptheme_list_selector_holo_light</item>
  </style>

  <style name="SpinnerDropDownItemAppTheme" parent="android:Widget.DropDownItem.Spinner">
      <item name="android:checkMark">@drawable/apptheme_btn_radio_holo_light</item>
  </style>
</resources>

Custom Application Theme:

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

<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">

  <style name="AppTheme" parent="@style/_AppTheme"/>

  <style name="_AppTheme" parent="Theme.AppCompat.Light">

    <item name="android:spinnerStyle">@style/SpinnerAppTheme</item>

    <item name="android:spinnerDropDownItemStyle">@style/SpinnerDropDownItemAppTheme</item>

  </style>

</resources>

As you can see, there are also many drawables that are referenced by these styles.

Here is the one specific to the "triangle" you want to change:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false"
          android:drawable="@drawable/apptheme_spinner_disabled_holo_light" />
    <item android:state_pressed="true"
          android:drawable="@drawable/apptheme_spinner_pressed_holo_light" />
    <item android:state_pressed="false" android:state_focused="true"
          android:drawable="@drawable/apptheme_spinner_focused_holo_light" />
    <item android:drawable="@drawable/apptheme_spinner_default_holo_light" />
</selector>

I don't think this is the right place to list every file completely, since you can get them all directly from the referenced tool.


Note: this is the way to theme your whole app so that all spinners are updated to the custom style.

If you are looking for a quick and dirty way to change just one spinner, then check out that article referenced by Ricky in his comment:

I recommend reading it anyway, because it explains the process very well, and will help you to understand the rest of my answer.

Luxate answered 18/9, 2014 at 11:35 Comment(2)
The link to the article by Tom Gersic is dead. I think I found a copy of it here: synchronoussoft.net/blog/tom-gersicBullring
What is apptheme_spinner_background_holo_light?Quimper
G
89

The best and easiest solution:

spinner.getBackground().setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_ATOP);

Other solution (Thanks to Simon) if you don't want change all Spinners:

Drawable spinnerDrawable = spinner.getBackground().getConstantState().newDrawable();

spinnerDrawable.setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_ATOP);

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    spinner.setBackground(spinnerDrawable);
}else{
    spinner.setBackgroundDrawable(spinnerDrawable);
}
Gregorio answered 12/2, 2015 at 3:37 Comment(4)
Not great, that changes the color on ALL spinners.Frauenfeld
To apply it only on a specific spinner, you have to duplicate the drawable : Drawable spinnerDrawable = spinner.getBackground().getConstantState().newDrawable(); spinnerDrawable.setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP); if (Build.VERSION.SDK_INT >= 16) spinner.setBackground(spinnerDrawable); https://mcmap.net/q/174876/-android-cloning-a-drawable-in-order-to-make-a-statelistdrawable-with-filtersFrauenfeld
For me it is not true that it changes color on all spinnersBogey
Same here @Bogey . Perhaps it has to do with what SDK level you run it on. I am testing on 6.0.1.Rhino
A
43

From Lollipop on, you can set the background tint on xml,

android:backgroundTint="@color/my_color"
Antic answered 11/5, 2016 at 20:23 Comment(1)
Kindly not that will work only if you are not setting a background to your spinnerExtortion
L
19

To get the correct images, you can go to Android Asset Studio site and choose the Android Holo Colors Generator. This will create all the assets you might need, including the "triangle". It also generates the XML files that implement the changes.


Here are the example XML files:

Custom Color:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="apptheme_color">#33b5e5</color>
</resources>

Custom Spinner Style:

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

<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">

  <style name="SpinnerAppTheme" parent="android:Widget.Spinner">
      <item name="android:background">@drawable/apptheme_spinner_background_holo_light</item>
      <item name="android:dropDownSelector">@drawable/apptheme_list_selector_holo_light</item>
  </style>

  <style name="SpinnerDropDownItemAppTheme" parent="android:Widget.DropDownItem.Spinner">
      <item name="android:checkMark">@drawable/apptheme_btn_radio_holo_light</item>
  </style>
</resources>

Custom Application Theme:

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

<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">

  <style name="AppTheme" parent="@style/_AppTheme"/>

  <style name="_AppTheme" parent="Theme.AppCompat.Light">

    <item name="android:spinnerStyle">@style/SpinnerAppTheme</item>

    <item name="android:spinnerDropDownItemStyle">@style/SpinnerDropDownItemAppTheme</item>

  </style>

</resources>

As you can see, there are also many drawables that are referenced by these styles.

Here is the one specific to the "triangle" you want to change:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false"
          android:drawable="@drawable/apptheme_spinner_disabled_holo_light" />
    <item android:state_pressed="true"
          android:drawable="@drawable/apptheme_spinner_pressed_holo_light" />
    <item android:state_pressed="false" android:state_focused="true"
          android:drawable="@drawable/apptheme_spinner_focused_holo_light" />
    <item android:drawable="@drawable/apptheme_spinner_default_holo_light" />
</selector>

I don't think this is the right place to list every file completely, since you can get them all directly from the referenced tool.


Note: this is the way to theme your whole app so that all spinners are updated to the custom style.

If you are looking for a quick and dirty way to change just one spinner, then check out that article referenced by Ricky in his comment:

I recommend reading it anyway, because it explains the process very well, and will help you to understand the rest of my answer.

Luxate answered 18/9, 2014 at 11:35 Comment(2)
The link to the article by Tom Gersic is dead. I think I found a copy of it here: synchronoussoft.net/blog/tom-gersicBullring
What is apptheme_spinner_background_holo_light?Quimper
C
15

I'm still very new at Android development, so perhaps take my advice with a grain of salt, but none of the answers here seemed to relate to the spinner implementation I was using.

What I was looking for

android:backgroundTint="@color/colorPrimary"

Here's the entire <Spinner> tag:

        <Spinner
            android:id="@+id/coin_selector"
            android:layout_width="wrap_content"
            android:layout_height="21dp"
            android:layout_margin="26dp"
            android:layout_weight="1"
            android:textColor="#FFFFFF"
            android:dropDownSelector="@color/colorAccent"
            android:backgroundTint="@color/colorPrimary"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            tools:layout_editor_absoluteY="89dp"/>

Blue triangle

Coan answered 12/5, 2018 at 15:12 Comment(1)
android:backgroundTint requires API level 21Dorkus
C
7

Here is other way to resolve that programatically as well. (Tested on API 19 & Above).

Use ViewCompat for this.

ViewCompat.setBackgroundTintList(spinner, ColorStateList.valueOf(your_color));

Usage of spinner.setSupportBackgroundTintList throws an error

Castellan answered 25/10, 2017 at 8:43 Comment(1)
Excellent - this works for me with regular Spinner, no need for AppCompatSpinner. Good answerBethelbethena
S
5

Just do as like this. This will helps you to solve your problem.

<RelativeLayout 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:background="spinner background image">

        <Spinner       
        android:layout_width="match_parent"
        android:layout_height="match_parent"        
        android:background="@null"/>

        <ImageView 
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="arrow image" />

    </RelativeLayout>

or

Just had a look on the answer which I given for another query: Custom Spinner

Steamboat answered 18/9, 2014 at 11:35 Comment(0)
S
5

If you have minSdkVersion 21, it is very simple:

<Spinner
style="@style/Widget.AppCompat.DropDownItem.Spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/triangleColor" />

where triangleColor is your color.

Sceptic answered 10/4, 2018 at 15:0 Comment(0)
C
4

The solution for minimum SDK version 14. You can use AppCompatSpinner from Support Library:

AppCompatSpinner spinner = (AppCompatSpinner) view.findViewById(R.id.my_spinner);
spinner.setSupportBackgroundTintList(ContextCompat.getColorStateList(context, R.color.my_color));
Charyl answered 12/5, 2017 at 8:51 Comment(0)
C
0

I tried styling but found this line works to show the down caret for a Spinner in Kotlin:

ViewCompat.setBackgroundTintList(binding.ddnSpots, ColorStateList.valueOf(Color.parseColor("#4a3728")))

Note that 'ddnSpots' is bound as an element to a Fragment as compared to traditional view binding.

Colleen answered 15/8, 2022 at 20:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.