Ubarjohade
Ubarjohade

Reputation: 471

Android Spinner Color

I am attempting to make the background color of my spinner white. That's the only change I want to make to the spinner but every solution I see either requires making a custom spinner, changing the styles, or some other long and overly complicated process to just change the color. Is there a way to change the color easily. I enjoyed the method of creating a drawable and setting that as a background. Solution I found below works for api >= 23 but I need something with a min of 19. Is there an easy way or do I have to create a custom spinner just to change the color? This is what I am trying to make it look like.

Using drawable listed below for >= 23 api

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <color android:color="@color/splashColor" />
        </item>

        <item android:gravity="center_vertical|right" android:right="8dp">
            <layer-list>
                <item android:width="12dp" android:height="12dp" android:gravity="center" android:bottom="11dp">
                    <rotate android:fromDegrees="45">
                        <shape android:shape="rectangle">
                            <solid android:color="#000000" />
                            <stroke android:color="#aaaaaa" android:width="1dp"/>
                        </shape>
                    </rotate>
                </item>

                <item android:width="30dp" android:height="10dp" android:bottom="21dp" android:gravity="center">
                    <shape android:shape="rectangle">
                        <solid android:color="@color/splashColor"/>
                    </shape>
                </item>
            </layer-list>
        </item>
    </layer-list>

Using a simplified version without setting width/height (requires >= 23) looks like this

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape>
            <solid android:color="@android:color/white" />
        </shape>
    </item>
    <item>
        <bitmap
            android:gravity="end"
            android:src="@drawable/arrow_dropdown" />
    </item>
</layer-list> 

but it makes the height too large, making the whole screen seem off. I am unaware of a way to change the height pre 23

Upvotes: 0

Views: 446

Answers (2)

Ubarjohade
Ubarjohade

Reputation: 471

In the end I used the second method and just manually set the height of the spinner. Here is what the spinner code looks like in the end.

<Spinner
      android:layout_width="match_parent"
      android:layout_height="35dp"
      android:background="@drawable/custom_spinner"
      android:id="@+id/type" />

custom_spinner

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape>
            <solid android:color="@android:color/white" />
        </shape>
    </item>
    <item>
        <bitmap
            android:gravity="end"
            android:src="@drawable/arrow_dropdown" />
    </item>
</layer-list>

Upvotes: 0

MadScientist
MadScientist

Reputation: 2164

You can set a theme attribute to it.

use a custom namespace and set it.

Ex.

<ProgressBar
....
 app:theme="@style/custom_style"
/>

where custom_style is defined in your style directory as:

 <style name="custom_style" parent="Theme.AppCompat">
        <item name="colorControlNormal"><!--this color is not of importance --></item>
        <item name="colorControlActivated">#FFFFFF</item>
    </style>

added white to the progressbar color. Works for a ratingbar. assuming should work for progressbars too.

Upvotes: 1

Related Questions