Custom Spinner In Android
Posted By : Prince Bhardwaj | 29-Sep-2017
To create a custom spinner and change dropdown icon of your spinner, you have first created an xml spinner_bg.xml in drawable folder to be set as a spinner background. Here is the example of this -
<?xml version="1.0"
encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="8dp">
<solid android:color="@color/colorAccent"/>
<corners
android:radius="4dp" />
<padding android:bottom="3dp"
android:left="3dp"
android:right="5dp"
android:top="3dp" />
</shape>
</item>
<item>
<bitmap android:gravity="center_vertical|right"
android:src="@mipmap/down_icon" />
// you can use any other image here, instead of default_holo_dark_am
</item>
</layer-list>
</item>
</selector>
Now create a style for your spinner in values>styles.xml file -
<style name="SpinnerTheme" parent="android:Widget.DeviceDefault.Spinner">
<item name="android:background">@drawable/spinner_bg</item>
<item name="android:padding">8dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:textSize">18sp</item>
<item name="android:textColor">@android:color/white</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:paddingRight">15dp</item>
</style>
Add this style in your spinner widget as -
<Spinner
style="@style/SpinnerTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:overlapAnchor="false" />
At the end, to change background color of popup, you have to set popup background color in your activity/fragment as-
ArrayAdapteradapter = new ArrayAdapter (context, R.layout.spinner_item, currencies); spinner.setAdapter(adapter); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { spinner.setPopupBackgroundResource(R.color.colorPrimary); }
Now you can run your application and see the customized spinner you have created.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Prince Bhardwaj
Prince Bhardwaj is having Talent as Application Developer, he is an enthusiastic team player in Oodles Technologies.