androidandroid-layoutandroid-spinner

Custom layout of spinner in Android


I'm developing an Android application, I have to implement a custom layout for a spinner item.

I would realize a spinner like this:

enter image description here

I have a problem to implement the "gold arrow and border".

This is my code:

<Spinner
    android:id="@+id/spinner_language"
    android:layout_width="210dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:gravity="center_horizontal"
    android:entries="@array/spinnerItems"
    android:dropDownVerticalOffset="10dip"
    android:dropDownHorizontalOffset="0dip"/>

This is my spinner layout:

<CheckedTextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/white"
    android:background="@color/custom_black"
    android:textSize="20dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"/>

How i can realize it ?


Solution

  • I hope, this will help you..

    1) In drawable folder--->add spinner_border.xml for border of spinner.

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

    2)In layout design of spinner use this drop down "arrow_icon" for imageview src

         <RelativeLayout
                    android:layout_width="210dp"
                    android:layout_height="wrap_content"         
                    android:background="@drawable/spinner_border"
                    android:layout_gravity="center_horizontal"
                    android:orientation="horizontal">
    
                    <Spinner
                        android:id="@+id/spinner2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_gravity="center"
                        android:background="@android:color/transparent"
                        android:gravity="center_horizontal"
                        android:entries="@array/spinnerItems"
                        android:spinnerMode="dropdown" />
    
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:layout_gravity="center"
                        android:src="@drawable/arrow_icon" />
    
                </RelativeLayout>
    

    If there any further query,please inform, Thanks...