0

I'm trying to style a custom spinner in dialog mode, but whatever I do I can't get rid of the white background.

This is my spinner.

    <androidx.appcompat.widget.AppCompatSpinner
        android:id="@+id/mySpinner"
        android:layout_width="match_parent"
        android:layout_height="?attr/dropdownListPreferredItemHeight"
        android:layout_margin="16dp"
        android:background="@drawable/background_dropdown"
        android:spinnerMode="dialog"
    />

The items in my spinner all have a transparent background, as I don't want a background for each item, but one for the whole dialog. I found out I can set a background for all items, by overriding android:listViewStyle in my theme:

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:listViewStyle">@style/myListView</item>
</style>

<style name="myListView" parent="@android:style/Widget.ListView">
    <item name="android:background">@drawable/background_dropdown</item>
</style>

how to remove the background in the corners?

But even after that, there is still another background behind the list. I guessed it's from the dialog the spinner uses, so I added android:alertDialogStyle to my theme.

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:listViewStyle">@style/myListView</item>
    <item name="android:alertDialogStyle">@style/myDialog</item>
</style>

<style name="myDialog">
    <item name="android:background">@android:color/transparent</item>
</style>

but it made no difference. How do I get rid of this white background in the corners?

ElDuderino
  • 3,253
  • 2
  • 21
  • 38

1 Answers1

0

You can work around this issue by using a cardview. This is the code:

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        app:cardBackgroundColor="@color/colorWhite"
        app:cardCornerRadius="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="20dp">

            <androidx.appcompat.widget.AppCompatSpinner
                 android:id="@+id/mySpinner"
                 android:layout_width="match_parent"
                 android:layout_height="?attr/dropdownListPreferredItemHeight"
                 android:layout_margin="16dp"
                 android:background="@drawable/background_dropdown"
                 android:spinnerMode="dialog"
                 app:layout_constraintEnd_toEndOf="parent"
                 app:layout_constraintStart_toStartOf="parent"
                 app:layout_constraintBottom_toBottomOf="parent"
                 app:layout_constraintTop_toTopOf="parent"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>
Hascher
  • 486
  • 1
  • 3
  • 12