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?
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?
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>
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.
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>
<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 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"/>
drawable
and reference it instead of #123456
. Oh, and android:theme
must be used instead of stlye="@stye/..."
–
Teleost 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.
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>
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;
}
© 2022 - 2024 — McMap. All rights reserved.