1

I want to change popupBackground color of my dialog spinner.

In my activity.xml :

<Spinner
                    android:id="@+id/mCategorySpinner"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@id/textView7"
                    android:entries="@array/recipeCategory"
                    android:spinnerMode="dialog"
                    android:popupBackground="@color/colorPrimary"
                    android:textAlignment="center" />

In my activity.java :

categorySpinner=(Spinner) findViewById(R.id.mCategorySpinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.recipeCategory, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        categorySpinner.setPrompt("Choose category");
        categorySpinner.setAdapter(new NothingSelectedSpinnerAdapter(
                adapter,
                R.layout.category_spinner_row_nothing_selected,             
                this));

Nothing happens if I change android:popupBackground in XML,it remains the default white.
But if I change background it works, but it's not for dialog's background.

Ghulam Moinul Quadir
  • 1,638
  • 1
  • 12
  • 17
Chris
  • 73
  • 1
  • 10

2 Answers2

1

You can change background color and drop down icon like doing this way

Step 1: In drawable folder make a file called background.xml for the background of the spinner.

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/transparent" />
    <corners android:radius="5dp" />
    <stroke
        android:width="1dp"
        android:color="@color/darkGray" />
</shape>

Step 2: Now apply this background on your spinner in xml file

 android:background="@drawable/background"
Zoe
  • 27,060
  • 21
  • 118
  • 148
Ritwik
  • 45
  • 8
  • 1
    XML resources in the drawable folder are accessed just like images, there's no need to add `.xml` after the name. That in fact causes it to not be found. – Zoe Oct 28 '17 at 09:04
1

1.Use spinner_selector.xml

To show the color your changed

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/holo_red_light"
          android:state_pressed="true"/>
    <item android:color="@android:color/white"
          android:state_pressed="false"/>
</selector>

2.Add style

Add it to the style ,and you can use it in other place .

<style name="spinner_style">
   <item name="android:background">@drawable/spinner_selector</item>
</style>

3.Add it to the xml code

Use it as background of spinner .

<Spinner
    android:id="@+id/mCategorySpinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/textView7"
    android:entries="@array/recipeCategory"
    android:spinnerMode="dialog"
    style="@style/spinner_style"
    android:textAlignment="center" />
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
  • No description. Now that there's some description I removed the downvote. Remember that not everyone who reads this in the future knows exactly what everything does, explaining it is important. It still needs a better explanation, but now it is good enough for it to not be downvoted – Zoe Oct 28 '17 at 09:13
  • What about `@drawable/spinner_press` and `@drawable/spinner`? – Chris Oct 28 '17 at 09:20
  • I change it to color .If you pressed ,the color will change .@Chris – KeLiuyue Oct 28 '17 at 09:24
  • Could you check my answer please? – KeLiuyue Oct 30 '17 at 01:45