How to show divider between spinner items?
Asked Answered
P

6

30

I using listviews and expandedviews that has dividers and I can set them but on spinner its looks like it is no divider between items.

Someone that has a idea of how to fix this?

Pyo answered 31/12, 2010 at 11:17 Comment(0)
P
2

For people with same problem i after almost gived up i got an idea of how to get the divider.

I added the divider line at the bottom of my custom layout for each item

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout android:id="@+id/RelativeLayout01"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" style="@style/ListItem2">

    <TextView android:id="@+id/Text" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_alignParentLeft="true"
        style="@style/SpinnerView_Text" android:paddingLeft="10dip" />

    <ImageView android:id="@+id/icon" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:src="@drawable/arrowright"
        android:layout_alignParentRight="true" android:layout_centerInParent="true"
        android:layout_marginRight="20dip" />

</RelativeLayout>

<ImageView android:id="@+id/Divider1" android:layout_width="fill_parent"
    android:layout_height="1dip" style="@style/Divider"></ImageView>

Pyo answered 6/1, 2011 at 10:12 Comment(1)
can you post more xml? for example the outermost layout and style/Divider.thank youCutright
C
28

This worked for me:

<style name="SpinnerStyle" parent="Widget.AppCompat.ListView.DropDown">
        <item name="android:divider">#d1d1d1</item>
        <item name="android:dividerHeight">0.5dp</item>
    </style>

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:dropDownListViewStyle">@style/SpinnerStyle</item>

The advantage of using this is that it doesn't remove the ripple effect on hover.

Camphene answered 25/7, 2016 at 22:23 Comment(2)
This is not working for me even after adding it to main apptheme.Sundown
Thanks This is working from me.you must add this in andoid:Theme for spinnerToughminded
B
17

I managed to find a more proper solution for this issue (without including the divider in the single item layout).

What you have to do is define in your activity's theme

        <item name="android:dropDownListViewStyle">@style/App.Style.Spinner</item>

and then create the proper style with

   <style name="App.Style.Spinner" parent="@style/Widget.Sherlock.Light.ListView.DropDown">
           <item name="android:dividerHeight">10dip</item>
           <item name="android:divider">@drawable/mydivider</item>
   </style>
Bonni answered 16/4, 2012 at 15:24 Comment(4)
This is great unless you want to apply this style to only 1 Spinner in the Activity.Branscum
Where do we define the item: <item name="android:dropDownListViewStyle">@style/App.Style.Spinner</item> What do you mean by "in your activity's theme"? Am a little bit confused about this.Desperado
@ojonugwaochalifu You have to define this item as part of your activity's theme...Bonni
This is more appropriate as when you implement something in UI it should be consistent throughout the app and this solution does that.Aqueous
D
12

Based on @Talihawk answer, I made it work for specific spinner only. Instead of setting your activity theme, set the theme directly for the spinner view:

<style name="MatchSpinnerStyle" parent="android:style/Widget.ListView.DropDown">
    <item name="android:divider">#123456</item>
    <item name="android:dividerHeight">1dp</item>
</style>

<style name="MatchSpinnerTheme" parent="AppTheme">
    <item name="android:dropDownListViewStyle">@style/MatchSpinnerStyle</item>
</style>

and

<android.support.v7.widget.AppCompatSpinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"        
    android:theme="@style/MatchSpinnerTheme"/>
Dhammapada answered 13/11, 2017 at 14:14 Comment(1)
+1. Also, I must add, you can define your divider in a drawable and reference it instead of #123456. Oh, and android:theme must be used instead of stlye="@stye/..."Teleost
F
4

Sorry, I am answering years after the question was asked but the solution is very simple, you just have to do a simple thing. Go to your style.xml file and add this item in your active theme

 <item name="android:dropDownListViewStyle">@style/MySpinner</item> 

after this add another theme with the name MySpinner and same parent of your active theme

 <style name="MySpinner" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:dividerHeight">2dp</item>
    <item name="android:divider">#000</item>
</style>

this will separate your single item and will not show the separator while we hover on a single item

but be sure while doing this, we are applying this theme on all the spinner in the activity. Now after every spinner is will work on this same spinner theme.

Fossilize answered 30/12, 2019 at 11:17 Comment(0)
P
2

For people with same problem i after almost gived up i got an idea of how to get the divider.

I added the divider line at the bottom of my custom layout for each item

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout android:id="@+id/RelativeLayout01"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" style="@style/ListItem2">

    <TextView android:id="@+id/Text" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_alignParentLeft="true"
        style="@style/SpinnerView_Text" android:paddingLeft="10dip" />

    <ImageView android:id="@+id/icon" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:src="@drawable/arrowright"
        android:layout_alignParentRight="true" android:layout_centerInParent="true"
        android:layout_marginRight="20dip" />

</RelativeLayout>

<ImageView android:id="@+id/Divider1" android:layout_width="fill_parent"
    android:layout_height="1dip" style="@style/Divider"></ImageView>

Pyo answered 6/1, 2011 at 10:12 Comment(1)
can you post more xml? for example the outermost layout and style/Divider.thank youCutright
I
2

None of the other solutions worked for me, so I used a drawable as the background of the Spinner items to produce the desired effect.

I have created a new Drawable dropdown_divider.xml and a custom SpinnerAdapter class where I adapted the getDropDownView() method to set the background to the Spinner items.

The android:bottom="-56dp" in the drawable is what centers the line perfectly between two items for me, but that depends on the exact margins and paddings that you've applied in your layout.

dropdown_divider.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:left="16dp"
        android:right="16dp"
        android:bottom="-56dp">
        <shape android:shape="line" >
            <stroke
                android:width=".2dp"
                android:color="#FF666666" />
        </shape>
    </item>
</layer-list>

SpinnerAdapter:

@Override
public View getDropDownView(int position, View convertView,
                            @NonNull ViewGroup parent) {
    TextView text = (TextView) super.getDropDownView(position, convertView, parent);
    text.setBackground(context.getDrawable(R.drawable.dropdown_divider));
    label.setTextColor(Color.BLACK);
    label.setText(lists.get(position).getTitle());

    return text;
}

The result looks like this: example picture of result

Incase answered 2/12, 2020 at 20:6 Comment(1)
This means the reason why so many people are having this divider issue is because the SpinnerAdapter styling is overriding the divider set to the Spinner on themes... again one of my many issues with the value of Adapters in Android....Everick

© 2022 - 2024 — McMap. All rights reserved.