How to place line to split items in a Spinner?

Asked

Viewed 323 times

2

I’m trying to place a line dividing the items I have in a Spinner but I can’t seem to get it out.

spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        style="@style/spinnerItemStyle"
        android:maxLines="1"
        android:textColor="@color/grey_default"
        android:textStyle="bold"
        android:textSize="12dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee" />

spinner_dropbox.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="@style/spinnerDropDownItemStyle"
    android:maxLines="1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee" />

Styles.xml

<style name="spinnerItemStyle" parent="android:Widget.TextView.SpinnerItem"/>

<style name="spinnerDropDownItemStyle" parent="Widget.AppCompat.ListView.DropDown">
    <item name="android:dividerHeight">2dip</item>
    <item name="android:divider">#808080</item>
</style>

Mainactivity (...)

 //adapter creation categorias
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item, list_categorias);
        adapter.setDropDownViewResource(R.layout.spinner_dropbox);
        categoria.setAdapter(adapter);

What I’m getting is this:

inserir a descrição da imagem aqui Can anyone help us? Why not draw the line between the various items?

1 answer

0


What you should do is apply the style

<style name="spinnerDropDownItemStyle" parent="Widget.AppCompat.ListView.DropDown">
    <item name="android:dividerHeight">2dip</item>
    <item name="android:divider">#808080</item>
</style>

at the android:dropDownListViewStyle of Theme of the application instead of assigning it as style of CheckedTextView .

Add this line to the style of Theme of the application:

<item name="android:dropDownListViewStyle">@style/spinnerDropDownItemStyle</item>
  • I can’t understand the answer. In Styles.xml I have set the spinnerDropDownItemStyle which is then associated with spinner_dropbox. This spinner_dropbox is later called in java.

  • The style should not be associated with the spinner_dropbox but in the application’s Theme style.

  • Thank you! It’s worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.