Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 795
Noam Mizrachi Avatar asked Apr 24 '13 17:04

Noam Mizrachi


People also ask

How do I change the default background color in Android Studio?

Create background color. By default each activity in Android has a white background. To change the background color, first add a new color definition to the colors. xml file in the values resource folder like the following.


1 Answers

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

like image 157
Rahul Patil Avatar answered Sep 30 '22 14:09

Rahul Patil