21

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?

double-beep
  • 5,031
  • 17
  • 33
  • 41
aby
  • 810
  • 6
  • 21
  • 36

2 Answers2

61

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

inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • 1
    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? – aby May 24 '11 at 12:30
  • 5
    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 – AndroidNoob Sep 19 '11 at 14:53
  • 1
    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. – Ted Betz Jan 13 '12 at 21:09
  • 10
    Fails in ICS 4.0.4. Any workarounds ? – Shardul Apr 15 '12 at 09:12
  • 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 – CQM Mar 08 '13 at 19:23
  • @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? – NewK Jul 18 '13 at 15:34
  • Link to "android.R.layout.simple_spinner_dropdown_item" is dead (404). – Pang Oct 02 '13 at 04:39
  • This doesn't work on 4.4.4 . Any other solution? – android developer Jul 04 '14 at 14:33
  • This solution works for 'dialog' type spinner, But how do I achieve this for 'dropdown' type spinner? – Zeba Sep 07 '18 at 05:55
-3

Define a custom layout and use it with the spinner and adapter.

Flo
  • 27,355
  • 15
  • 87
  • 125