populating a spinner items with multiple lines
Asked Answered
M

5

7

Some items in my spinner are long and I would like the spinner to wrap the text over multiple lines depending on how long the text is. I looked over serval solutions for this but they are not working for me.

Here showing the text not fitting:

enter image description here

Here is my code in Java:

public void addItemsOnSpinner3() {

        spinner3D = (Spinner) findViewById(R.id.activities1);
        List<String> list = new ArrayList<String>();
        list.add("Academic Year Opening Program");
        list.add("Academic Year Closing Program");
        list.add("LR1: Defining Primary Care, Health Care Disparities & Vulnerable Populations");
        list.add("LR2: Community Resources and the Elderly");
        list.add("LR3: Inter-professional Teams and the Homeless");
        list.add("LR4: Quality Improvement and Veterans");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                R.layout.multiline_spinner_row, list);
        dataAdapter.setDropDownViewResource(R.layout.multiline_spinner_row);
        spinner3D.setAdapter(dataAdapter);
    }

I created a custom row called "multiline_spinner_row.xml" where singleline is changed to false:

<TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:singleLine="false"
    xmlns:android="http://schemas.android.com/apk/res/android" />

All this does is make my selection wrap the text but not the items:

enter image description here

Any help would be greatly appreciated! Thanks in advance!

Mcvay answered 24/3, 2018 at 19:16 Comment(0)
P
5

in spinner set this property:

android:spinnerMode="dialog"

in text view set these properties:

android:layout_weight="1"  
android:ellipsize="none"  
android:maxLines="100"  
android:scrollHorizontally="false"
Preparator answered 24/3, 2018 at 19:29 Comment(1)
We want a dropdown list, not a popup list dialogRumal
L
2

This actually worked for me (in Xamarin Android, but the problem is the same):

Instead of

adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);

write

adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleExpandableListItem1);
Lishalishe answered 12/10, 2020 at 20:20 Comment(0)
O
1

create an xml file like this, your xml will need to have a parent layout to wrap the TextView.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textColor="#333333"
    android:textSize="15sp"
    tools:text="123" />

</RelativeLayout>

make sure your TextView has an id, then create your adapter like this.

private ArrayAdapter<CharSequence> stringAdapter = new ArrayAdapter<>(context, R.layout.spinner_textview, R.id.textView, displayTexts);

use this consturctor

public ArrayAdapter(@NonNull Context context, @LayoutRes int resource,
        @IdRes int textViewResourceId, @NonNull List<T> objects) {
    this(context, resource, textViewResourceId, objects, false);
}

or any constructor that has textViewResourceId as a parameter

that's it. you won't need to override anything else. Check out the below image to view the effect. enter image description here enter image description here

Ornithic answered 3/3, 2021 at 4:29 Comment(0)
L
0

Try this

I solved the problem with simple coding.

Just add Android.Resource.Layout.SimpleExpandableListItem1 when assigning the Adapter for Spinner as displayed below.

please check more on My Blog Post

Lactoprotein answered 14/8, 2020 at 7:33 Comment(1)
Please elaborate a bit and try to avoid just pasting plain URLs in answer.Xiomaraxiong
A
0

Use this

adapter.setDropDownViewResource(android.R.layout.simple_expandable_list_item_1);
Alodie answered 15/3, 2023 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.