Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any easy way to change Spinner dropdown color in Android?

enter image description here

I create my theme to use with the app and the parent of the theme is Theme.AppCompat.Light.NoActionBar

by the way, I want white background and black text.

And this is adapter code

     val adapter = ArrayAdapter.createFromResource(activity,
                R.array.email_type_array, android.R.layout.simple_spinner_item)

     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
     child.spinner.adapter = adapter

Is there any easy way to change Spinner dropdown color in Android?

like image 867
theerasan tonthongkam Avatar asked Oct 17 '17 07:10

theerasan tonthongkam


2 Answers

yes. You can use following attribute fro spinner inside your xml

android:popupBackground="YOUR_HEX_COLOR_CODE"

to change textcolor etc Make a custom XML file for your spinner item.

spin_item.xml:

Then provide it your desired color and sizes :

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

<TextView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:textSize="24sp"
    android:textColor="#000000"         
    android:padding="4dp"
    />

And then use it like this:

val adapter = ArrayAdapter.createFromResource(activity,
                R.array.email_type_array, android.R.layout.simple_spinner_item)
adapter.setDropDownViewResource(R.layout.spin_item)
like image 172
Akhilesh Awasthi Avatar answered Sep 23 '22 23:09

Akhilesh Awasthi


To change the dropdown background color use android:popupBackground="@color/aColor" on the xml file for your Spinner widget:

<Spinner
    android:id="@+id/my_spinner"
    android:layout_width="100dp"
    android:layout_height="match_parent"
    android:popupBackground="@color/aColor" />

When playing with a light theme on your styles.xmlfile the spinner dropdown icon color will be black, but pay attention that if you are using <item name="android:textColorSecondary">@color/aColor</item> the dropdown icon will pick that color:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textColorSecondary">@color/aColor</item>

Even your question is about to change the dropdown background color I came here because I was trying to understand why my spinner dropdown icon color was with a different color until I discover that (android:textColorSecondary) - So hope that helps someone else too.

like image 43
Filipe Brito Avatar answered Sep 25 '22 23:09

Filipe Brito