androidxmlspinner

Spinner arrow disappears when background is set to a custom spinner xml file?


I would like to add a border and rounded corners to my spinner, which I understand can be done by creating a separate custom spinner xml file. However when I do so, the spinner arrow disappears. This problem continues even after I add the bitmap into the custom spinner xml file.

image

This is my code:

activity_main.xml

<Spinner
        android:id="@+id/postOptions"
        android:layout_width="150dp"
        android:layout_height="48dp"
        android:entries="@array/postOptions"
        android:spinnerMode="dropdown"
        android:background="@drawable/spinner_layout"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.061"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/header"
        app:layout_constraintVertical_bias="0.026" />

spinner_layout.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF"/>
    <corners android:radius="4dp"/>
    <stroke android:color="@color/theme" android:width="1dp"/>
    <item>
        <bitmap
            android:gravity="center_vertical|right"
            android:src="@drawable/baseline_arrow_drop_down_circle_24"/>
    </item>
</shape>

Solution

  • When you set the spinner background color using android:background="@color/your_color" your spinner default arrow will disappear

    Write your spinner code inside a frame layout, here you don't need to use a separate image view to show the drop-down icon.

    <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/spinner_layout">
    
                <Spinner
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:dropDownSelector="@color/colorAccent"
                    android:dropDownWidth="@dimen/dp_70"
                    android:spinnerMode="dropdown"
                    android:tooltipText="Select floor" />
    </FrameLayout>
    

    spinner_layout.xml

    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="#FFFFFF"/>
        <corners android:radius="4dp"/>
        <stroke android:color="@color/theme" android:width="1dp"/>
    </shape>