0

I am trying to set drawable as an arrow in Spinner but bitmap is not supported.

I used svg file (ic_arrow.xml) and set it in spinner_arrow.xml file:

<layer-list  xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <padding
                android:left="4dp"
                android:top="4dp"
                android:right="4dp"
                android:bottom="4dp"
                />

            <solid android:color="@color/white" />
            <stroke android:width="2px" android:color="@color/grey" />
            <corners android:radius="5dp" />
        </shape>
    </item>
    <item>
        <bitmap android:gravity="center|right" android:src="@drawable/ic_arrow"/>
    </item>
</layer-list>

So I used it for background in layout file

<Spinner
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/spinner_arrow"/>

But I have an error in displaying Spinner: "Binary XML file line #41 in myapp:layout/activity_registration_driver: Error inflating class Spinner".

I think the problem is in adding drawable for arrow in spinner but i don't know how to solve it

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Akram Baratov
  • 143
  • 1
  • 3
  • 13
  • 1
    Similar question here. With accepted answer. https://stackoverflow.com/questions/20422802/how-to-set-dropdown-arrow-in-spinner – Pranay Bhalerao Feb 24 '20 at 12:15

2 Answers2

2

Please use below code for add arrow with spinner:

<RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="@dimen/btn_height_common"
                android:layout_marginLeft="@dimen/_15sdp"
                android:layout_marginTop="@dimen/_5sdp"
                android:layout_marginRight="@dimen/_15sdp"
                android:background="@drawable/bg_editext">

                <Spinner
                    android:id="@+id/sp_gender"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@null"
                    android:entries="@array/gender"
                    android:fontFamily="@font/gotham_narrow_book"
                    android:overlapAnchor="false"
                    android:paddingLeft="@dimen/_10sdp"
                    android:paddingRight="@dimen/_10sdp"
                    android:textColor="@color/gray_text_color"
                    android:textSize="@dimen/normal_textsize" />

                <ImageView
                    android:layout_width="@dimen/_15sdp"
                    android:layout_height="@dimen/_15sdp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="@dimen/_15sdp"
                    android:padding="@dimen/_2sdp"
                    android:src="@mipmap/ic_down_gray" />

            </RelativeLayout>

Output:

enter image description here

Ali
  • 3,346
  • 4
  • 21
  • 56
1

Basically one needs to create a custom background for a spinner. It should be something like this:spinner_background.xml

<item>

    <layer-list>

        <item>
            <color
                android:color="@android:color/white"/>
        </item>

        <item>
            <bitmap
                android:gravity="center_vertical|right"
                android:src="@drawable/ic_arrow_drop_down_black_24dp"/>
        </item>

    </layer-list>

</item>

Then create a custom style for your spinner, where you specify the above selector as background:

<style name="Widget.App.Spinner" parent="@style/Widget.AppCompat.Spinner">
    <item name="overlapAnchor">true</item>
    <item name="android:background">@drawable/spinner_background</item>
</style>

And finally in your app theme you should override two attributes if you want it to be applied all across your app:

<item name="spinnerStyle">@style/Widget.App.Spinner</item>
<item name="android:spinnerStyle">@style/Widget.App.Spinner</item>
Tushar
  • 54
  • 4