Change color of the drop down arrow of Spinner in XML
Solution 1
There are three ways to achieve that.
1. Through code:
In your xml, make sure your spinner has an id. Let's say we have a spinner with id "spinner".
In your code, add the following in your onCreate():
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.getBackground().setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_ATOP);
where red is your defined color in colors.xml in the values folder.
2. Through xml:
For API 21+:
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/red" />
or if you use the support library, you can use:
<android.support.v7.widget.AppCompatSpinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="@color/red" />
3. Through drawables:
You can use this online tool: http://android-holo-colors.com
This will generate custom drawables for any view you want with your preferred color. Make sure you select spinner, then download the resources.
Solution 2
I'm surprised noone had pointed it out, but you can just subclass Widget.AppCompat.Spinner
and change backgroundTint
<style name="Spinner" parent="Widget.AppCompat.Spinner">
<item name="backgroundTint">@color/spinnerTint</item>
</style>
and apply it to the Spinner
<Spinner
style="@style/Spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnerMode="dropdown" />
Solution 3
use backgroundTint attribute
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/white"
/>
if min_SDK < 21(Lollipop) you should use AppCompatSpinner
<android.support.v7.widget.AppCompatSpinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="@color/white"
/>
Solution 4
If (API 21+) {
you can use directly android:backgroundTint="@color/color"
, inside your Spinner:
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="@color/color" />
} else {
create your own style:
<style name="spinner_style" parent="Widget.AppCompat.Spinner">
<item name="backgroundTint">@color/color</item>
</style>
then into Spinner:
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@style/spinner_style"/>
}
Note: you can use the style method in all API's.
Solution 5
try this:
spinner_age.getBackground().setColorFilter(ContextCompat.getColor(this,
R.color.spinner_icon), PorterDuff.Mode.SRC_ATOP);
Francisco Romero
The best way to learn something is trying to help others to solve their issues that you have never tried before. This is the reason why I am in Stackoverflow. Now starting with web developing.
Updated on August 23, 2020Comments
-
Francisco Romero almost 4 years
As I wrote in my question, I want to change the color of the drop down arrow (the default arrow, not a custom arrow or something like that) of a
Spinner
in XML, but the problem is that I couldn't find anything to make reference to it from theXML
.Is it possible? If yes, how can I change the color?
Thanks in advance.
-
derfect almost 9 yearstake a look at the answer here
-
Francisco Romero almost 9 years@Rayes But it is for a custom dropdown, not the default dropdown.
-
Edijae Crusar over 8 years@Error404 3 months later and am faced with the same problem. your accepted answer has helped me solve the problem. 1 vote for you 1 for Hussein :)
-
-
Francisco Romero almost 9 yearsI don't have
colors.xml
file. Or I have to create it? Also I want to point that in my question, when I say by XML, I mean if there is some option likeandroid:colorDropDownArrow
or something like that to set the value of the arrrow directly in the XML. Thanks! -
Hussein El Feky almost 9 yearsUnfortunately, there is no way you can do this from your xml except by using android:background="@drawable/spinnerBackground", but this will change your spinner background completely. So you have to create a new xml in your values folder called colors.xml and add a color there called red. Also check my updated answer, I have put another way instead of creating colors.xml if you want that.
-
Francisco Romero almost 9 yearsThank you for the update! But I still have a doubt. The arrow it's only displayed with the color if I don't have any background for the Spinner, but if I make a background the arrow dissapears. Do you know why it is?
-
Hussein El Feky almost 9 yearsIf you make a background for the spinner, this means you will remove its default background which is an arrow. So if you want to use the background attribute in your xml, then you should go for the second way I posted in my answer. The site will generate a zip file with the files you will put in your project. The site will explain that to you. If you need more help, tell me.
-
Francisco Romero almost 9 yearsI mark it as accepted because you had been very helpful and if I have some doubt I put a new coment ;)
-
Hussein El Feky almost 9 yearsThanks, surely ask me anything if you want. :)
-
niranjan_b about 8 yearsIsn't the backgroundTint property applicable only to API level 21?
-
mewa about 8 years@niranjan_b
android:backgroundTint
is. This is the AppCompat version (notice no android prefix here and style extends an AppCompat derived style) -
Zvi almost 8 yearsWeird behavior: when I added the attribute android:background, no matter what value i set the background, the color of the background is taken from backgroundTint and thus one can not see the arrow.
-
Zvi almost 8 yearsit changed the background color and not the color of the arrow
-
mewa almost 8 years@Zvi When you change
android:background
you're no longer using Android's background drawable, but instead you are defining a new color drawable, that's why you don't see the arrow -
Zvi almost 8 yearsYes, I realized it when reading other posts. I also found a post that solved my problem as follows: to change the background color without destroying the drawable, I have put the spinner in a Relative layout and defined its background color. This, combined with your solution to use backgroundTint to change the arrow color, gives full control on both colors.
-
Pir Fahim Shah almost 4 yearsvery best answer
-
Titus Sutio Fanpula about 3 yearsAwesome. Thanks
-
W.M. over 2 yearsTried to use it, not showing the list at all.