How to set dropdown arrow from drawable in spinner?

Akram Baratov :

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

Ali :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=3675&siteId=1