Set Border/Divider line between Spinner items in android

How to show divider in middle of two or multiple spinner items to separate them with each other.

In this tutorial we are creating spinner with divider lines because its make our spinner look different than other normal style spinners. So here is the complete step by step tutorial for Set Border/Divider line between Spinner items in android.

android-project-download-code-button

How to Set Border/Divider line between Spinner items in android.

Code for MainActivity.java file.

 package com.adddividerbetweenspinner_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

 public class MainActivity extends Activity {
 Spinner spinnerWithBorder;
 
 String[] spinnerItems = new String[]{
 "APPLE",
 "MANGO",
 "BANANA",
 "PAPAYA",
 "WATER MELON",
 
 };
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 spinnerWithBorder = (Spinner)findViewById(R.id.spinner1);
 
 ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
 this,R.layout.spinner_textview,spinnerItems );
 
 spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_textview);
 
 spinnerWithBorder.setAdapter(spinnerArrayAdapter);
 }
}

Code for activity_main.xml layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.adddividerbetweenspinner_android_examples.com.MainActivity" >

 <Spinner
 android:id="@+id/spinner1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true" />

</RelativeLayout>

Code for spinner_textview.xml file.

 <?xml version="1.0" encoding="utf-8"?>
 <TextView
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/textView1"
 android:layout_width="250dp"
 android:layout_height="wrap_content"
 android:textColor="#fefcfe"
 android:padding="10dp"
 android:textSize="18dp"
 android:background="@layout/spinner_item_divider" />

Code for spinner_item_divider.xml layout file.

 <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
 <item>
 <shape android:shape="rectangle">
 <solid android:color="#fde125"/>
 </shape>
 </item>
 <item android:bottom="1.5dp">
 <shape android:shape="rectangle">
 <solid android:color="#02bd8a"/>
 </shape>
 </item>
</layer-list>

Screenshot:

Set Border/Divider line between Spinner items in android

Click here to download Set Border/Divider line between Spinner items in android project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *