Veronika Gilbert
Veronika Gilbert

Reputation: 471

Change background color of Spinner popup

I need to change the color of the spinner popup. Here is what I have attempted till now: java code:

ArrayAdapter<Integer> adapter_year = new ArrayAdapter<Integer>(this, R.drawable.custom_spinner_holidays, year);
adapter_year.setDropDownViewResource(R.layout.customize_spinner);

custom_spinner_holidays.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="@drawable/custom_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:textColor="@android:color/white" />

customize_spinner.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/spinnertext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#0193DE"
        android:textColor="#FFFFFF"
        android:textSize="18sp" />

</LinearLayout>

But it doesn't work. I get error when I click on the spinner.

Upvotes: 10

Views: 13781

Answers (1)

Anjali
Anjali

Reputation: 598

You are making things complicated! Just add this to your main spinner xml

android:popupBackground="#yourcolor"

and change your java code as:

adapter_year.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Upvotes: 21

Related Questions