How to style Android spinner popup?
Asked Answered
R

6

7

I have set

<item name="android:spinnerMode">dialog</item>

and so when I tap the spinner, I get a popup. But that popup is grey with white text and I can't seem to change any of the colors. How do I style this dialog?

I tried the following with some crazy temporary colors to see what changes but nothing changes.

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:dialogTheme">@style/SpinnerDialog</item>
    <item name="android:alertDialogTheme">@style/SpinnerAlertDialog</item>
</style>

<style name="SpinnerDialog" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:popupBackground">#ff00ff</item>
    <item name="colorPrimary">#ff00ff</item>
    <item name="colorPrimaryDark">#ffff00</item>
    <item name="colorAccent">#ff0000</item>
</style>

<style name="SpinnerAlertDialog" parent="Theme.AppCompat.Dialog">
    <item name="colorPrimary">#00ffff</item>
    <item name="colorPrimaryDark">#00ff00</item>
    <item name="colorAccent">#0000ff</item>
</style>

There are a bunch of similar questions, but they all either deal with dropdowns or ancient versions of Android or just don't work.

Rimester answered 19/2, 2017 at 8:55 Comment(7)
Did you try creating a custom layout and a adapter to go with that as mentioned here ?Retrenchment
Possible duplicate of How to customize a Spinner in AndroidRetrenchment
I don't want to do that stuff in code. Are they serious? I just want to change the background of a dialog, not create a fork of Android or whatever they're suggesting.Rimester
There is nothing like forking Android. You just need to specify your custom layout to the adapter.Retrenchment
just add android:popupBackground="#yourcolor" in your xml code of spinner this will change thebackground color of the popup..Nancinancie
No effect. I added it to the AppTheme, to popupTheme, to the spinner theme, it doesn't change anything.Rimester
@Rimester well post some more of your code of spinner..and things you tried..from what you posted things are not clear..Nancinancie
N
9

Instead of using theme or style.xml for changing the popup background color of dialog.

Why not try this?? In your layout xml

 <Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:spinnerMode="dialog"
    android:popupBackground="#yourcolor"/>

Since you tried adding theme it changes nothing.This will be easy to achieve..isn't it??

Hope this helps!!!

Nancinancie answered 19/2, 2017 at 10:33 Comment(1)
This does not work. popupBackgroundColor gets ignored when spinner mode is set to dialog, just like the documentation says.Densify
R
4

You can use a custom layout to achieve this.

Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
        R.id.custom_spinner_item, yourItemList);
adapter.setDropDownViewResource(R.layout.custom_spinner_dropdown_item);
spinner.setAdapter(adapter);

You should have the custom layouts in place:

  • R.id.custom_spinner_item for the item in the spinner.
  • R.layout.custom_spinner_dropdown_item for the spinner drop-down item.
Retrenchment answered 19/2, 2017 at 9:11 Comment(2)
I'm not using dropdowns. I just want to change the background color of the popup window, not change any of the items.Rimester
@Rimester That popup window is called "drop down" hereRetrenchment
B
1

I wanted to change the dialog background and add custom padding here's the style:

 <style name="customSpinnerDialog" >

        <item name="android:background">@android:color/white</item>
        <item name="android:textColor">@color/color_accent</item>
        <item name="android:padding">0dp</item>

 </style>

to apply to you spinner in layout xml :

<android.support.v7.widget.AppCompatSpinner
                android:id="@+id/reason_spinner"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:drawSelectorOnTop="true"
                android:theme="@style/customSpinnerDialog"
                android:spinnerMode="dialog"
                style="@style/SpinnerTheme"
                 />

hope this helps.

Ballard answered 5/7, 2019 at 15:18 Comment(1)
To edit the font or the font size in the prompt, you can create a SpannableStringBuilder from the prompt text, and add necessary spans (such as TypefaceSpan and AbsoluteSizeSpan) with the setSpan method. And call setPrompt method to set the spannable string. For custom font, consider using the CustomTypefaceSpan mentioned in #4819549Subsistence
E
0

Just create theme with "background" item which contains color that you want.

<style name="Spinner.PopUpTheme">
    <item name="android:background">@color/black</item>
</style>

Then set that theme to "popupTheme" attribute for your spinner.

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:spinnerMode="dialog"
    android:popupTheme="Spinner.PopUpTheme"/>
Elizabetelizabeth answered 20/6, 2019 at 12:17 Comment(0)
S
0

in your app theme or activity them :

  <item name="android:dropDownListViewStyle">@style/SpinnerStyle1</item>

declare SpinnerStyle1:

<style name="SpinnerStyle1" parent="Widget.AppCompat.ListView.DropDown">
        <item name="android:divider">@color/blackText</item>
        <item name="android:dividerHeight">1px</item>
        <item name="android:scrollbars">none</item>
</style>
Shiloh answered 12/9, 2020 at 8:6 Comment(0)
L
0

If it is BaseAdapter , you can use parent's rootView to set background.

@SuppressLint("ViewHolder")
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

    val view: View = inflater.inflate(R.layout.unit_spinner_item, parent, false)
    parent?.rootView?.background = AppCompatResources.getDrawable(context,R.drawable.aa_shape )

    ...

    return view
}
Luciferase answered 27/4, 2022 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.