How to wrap lengthy text in a spinner?

29,304

Step 1. TextView with wrapped text

The first thing to do is to to force simple TextView to wrap text. Its easy:

<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:singleLine="false"
    android:text="very long text that will be wrapped to next line" />

Note the singleLine attribute here.

Step 2. Custom layout

Now we should somehow set singleLine attribute to false in TextView used by Spinner to show the item in the list.

In your code you probably have place where you create adapter to use it with Spinner:

this.mAdapter = ArrayAdapter.createFromResource(this, R.array.Planets,
                android.R.layout.simple_spinner_dropdown_item);

The idea is to copy the android.R.layout.simple_spinner_dropdown_item layout to your project. Then modify it by setting singleLine attribute to false in CheckedTextView:

For this, add file to res/layout folder named multiline_spinner_dropdown_item.xml with next code:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="false"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee" />

Note that this file is identical to android.R.layout.simple_spinner_dropdown_item layout, except it has singleLine set to false now.

Step 3. Creating Adapter with custom layout

Modify your adapter creating code to:

this.mAdapter = ArrayAdapter.createFromResource(this, R.array.Planets,
                 R.layout.multiline_spinner_dropdown_item);

Here is screenshot from modified SpinnerActivity example from Android SDK:

enter image description here

Share:
29,304
aby
Author by

aby

Updated on April 24, 2020

Comments

  • aby
    aby about 4 years

    I have two spinner and EditText controls within a table layout view on a separate row. The spinners are populated with data. My problem is the data (texts) that are populated into the spinners are too lengthy to fit the screen size. Therefore, the spinners are forced to stretch unnecessarily stretching other controls on another row.

    It's a must for me to show the texts in the spinner. Hence using ellipses is not an option. If it's possible how can I wrap the lengthy text on the spinners?

  • aby
    aby about 13 years
    Thank you! I just followed your steps and now it works fine. But, after an option is selected from the spinner (and the spinner is collapsed), the text is too lengthy to fit on the screen. How can i shorten it or display it in a multiline?
  • AndroidNoob
    AndroidNoob over 12 years
    Stumbled upon this answer after googling for multiline spinners, works great :) though my spinner items were longer than 2 lines, so in multiline_spinner_dropdown_item.xml I had to set android:layout_height="wrap_content" and android:ellipsize="none" for all my text to appear in the items list
  • Ted Betz
    Ted Betz over 12 years
    Great advice... but watch out for the downloaded source file for API 14. The line android:layout_height="?android:attr/listPreferredItemHeight‌​" was misspelled as android:layout_height="?android:attr/ListPreferredItemHeight‌​" (note the capitalized L) and caused an error.
  • Shardul
    Shardul about 12 years
    Fails in ICS 4.0.4. Any workarounds ?
  • CQM
    CQM over 11 years
    works in 4.2.2 for the most part, but the long text selections bleed off the page instead of wrapping to the next line
  • NewK
    NewK almost 11 years
    @inazaruk I'm feeling dumb, but I have no clue on how to " force simple TextView to wrap text" and how is that related to the spinner it self, can you explain this step?
  • Pang
    Pang over 10 years
    Link to "android.R.layout.simple_spinner_dropdown_item" is dead (404).
  • android developer
    android developer almost 10 years
    This doesn't work on 4.4.4 . Any other solution?
  • Zeba
    Zeba almost 6 years
    This solution works for 'dialog' type spinner, But how do I achieve this for 'dropdown' type spinner?