Search code examples
androidandroid-spinner

How to set android:popupBackground border size/padding?


I wanted to set the background color of a Spinner in Android Studio and have been able to do so with the following code:

<Spinner
    android:id="@+id/StreamSpinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:background="#D2B450"
    android:popupBackground="#D2B450">
</Spinner>

The issue is that when I added android:popupBackground="#D2B450" it put some borders/padding around the selection box which were not there before I added that line. Before I added that line, the selection box with the content was with a white background and no padding/borders.

I have not been able to find out how to remove this while retaining the background color I need (#D2B450).

This is what it looks like with the line in place to set the popupBackground (see black borders)

enter image description here

and this is what it looks like without that line:

enter image description here

Can anyone point me in the right direction to remove the borders?

Thanks.


Solution

  • You can try to set it programmatically:

    <color name="spinner_background">#D2B450</color>
    
    spinner2.setPopupBackgroundResource(R.color.spinner_background);
    

    Option 2:

    If still not working, there is a workaround to create a custom item layout; set it in android:background; and fix the ripple effect in android:foreground.

    spinner_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        style="?android:attr/spinnerDropDownItemStyle"
        android:layout_width="match_parent"
        android:layout_height="?attr/dropdownListPreferredItemHeight"
        android:foreground="?attr/selectableItemBackground"
        android:ellipsize="marquee"
        android:singleLine="true" />
    

    And set that to the spinner adapter:

    adapter.setDropDownViewResource(R.layout.spinner_item);
    
    • The drawback that the ripple effect will work only on API-23+