How to change a spinner's list background color in Android
Asked Answered
M

4

16

I'm creating a new spinner dynamically, how can I change my list background color?

the current background color is some dark gray:

two muppets When I'm changing the spinner's background attribute to white, i'm getting this unwanted situation:

two muppets I want it to be transparent in the activity, and only when i'm opening the spinner (press on it), I want the background will be white.

Here is the code that I'm creating the spinner with:

I'm creating the adapter with:

    mAdapter = new ArrayAdapter<String>(getApplicationContext(), 
                                     R.layout.spinner, R.id.Language, lang);
    LinearLayout layoutHolder = 
                         (LinearLayout)findViewById(R.id.RegisterFormLayout);
    Spinner spinner = new Spinner(getApplicationContext());
    LayoutParams layParams= new 
                Spinner.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,       
                                         ViewGroup.LayoutParams.WRAP_CONTENT);
    spinner.setLayoutParams(layParams);
    spinner.setAdapter(mAdapter); 
    spinner.setOnItemSelectedListener(new myOnItemSelectedListener());
    if (lang != null)
       spinner.setSelection(lang.intValue());
    spinnerList.add(spinner);
    layoutHolder.addView(spinner);

my spinner.xml layout is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:id="@+id/SpinnerLayout">

<TextView
    android:id="@+id/Language"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#ffffff"
    android:background="#00ffffff"
    android:padding="5dp" />
</LinearLayout>

any suggestion?

Monachism answered 24/4, 2013 at 17:38 Comment(7)
Do you have layout definitions for each of your listview items?Abaddon
no... it's just a list that i attached to ArrayAdapter(mAdapter)Monachism
@NoamMizrachi Check my answer here #15299694Dory
@Dory look at my edit. I have a problem with my spinner.xml file. i made the background of the EditText transparent, because i want it to be transparent with the background image of my activity. now, i don't know from where the dark gray color arrived to my background spinner when i'm pressing on the spinner. I didn't declared this color at nowhere. any idea?Monachism
@NoamMizrachi why did you set this android:background="#00ffffff", black background on black Text?Dory
@Dory it doesn't metter since it is transparent. tnx anyway for the help i managed to solve it.Monachism
check this answer for update.Portmanteau
M
15

The solution for this is to add this code when creating the spinner dynamically:

spinner.setPopupBackgroundResource(R.drawable.spinner);

and to create spinner.xml under Drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#ffffff" />
</shape>

This solution require API level of 16 and above.

the outcome:

two muppets

Monachism answered 25/4, 2013 at 10:14 Comment(1)
Ya u r rt. spinner.setPopupBackgroundResource(R.drawable.spinner); minimum API level is 16. To work on all level set it from XML android:popupBackground="@android:color/redLatanya
M
26

I think this requirement can't possible through theme changes. Because Spinner constructor assigns value on popupBackground attr only if you write in layout xml otherwise it will use default theme value. like below

   <Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:popupBackground="@android:color/holo_green_dark" />

// just try changing popup background

Martinsen answered 25/4, 2013 at 9:2 Comment(1)
It adds a nasty black border around itHormuz
M
15

The solution for this is to add this code when creating the spinner dynamically:

spinner.setPopupBackgroundResource(R.drawable.spinner);

and to create spinner.xml under Drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#ffffff" />
</shape>

This solution require API level of 16 and above.

the outcome:

two muppets

Monachism answered 25/4, 2013 at 10:14 Comment(1)
Ya u r rt. spinner.setPopupBackgroundResource(R.drawable.spinner); minimum API level is 16. To work on all level set it from XML android:popupBackground="@android:color/redLatanya
N
3

To solve your problem,try this.

android:popupBackground="#00826b"
Nikethamide answered 30/8, 2016 at 21:41 Comment(0)
C
1

in my spinner.xml

use this in LinearLayout : android:background="#ffffff"

Chellman answered 25/4, 2013 at 8:30 Comment(1)
i'm getting the result like in my second image of my question. i want it to be transparent in the activity and only when i'm pressing it the background will be white.Monachism

© 2022 - 2024 — McMap. All rights reserved.