If the text of a Spinner
item is too long to fit into a single line, the text is not wrapped but cut off. This is only the case for API level >= 11. Here are screenshots of Android 4.2.2 (left) which shows the wrong behavior and Android 2.3.3 (right) where it looks as expected.
android:singleLine="false"
simply gets ignored here. So as all other tries like android:lines
, android:minLines
, etc. The TextView
somehow seems to be much wider than the window width.
I saw other people having the same problem, but no one could find a solution. So, is this a system bug? I don't think this inconsistency between the OS versions can be intended.
Please note:
There were some answers suggesting relatively simple solutions.
Writing a custom
Adapter
and overridinggetView()
as well asgetDropDownView()
. This is not the solution here, because at this point, there is still the original problem: How does the layout have to look like to handle proper line wrapping?Wrapping the
TextView
of the drop down view into a parentViewGroup
. Does not work withandroid:layout_width="match_parent"
because the width of the parent strangely seems to be unlimited.Giving the drop down view a fixed width. This is not suitable with the different widths the
Spinner
can have.And of course, no solution is to manually insert
\n
s anywhere into the text.
Reproduce with the following code:
UPDATE: I also uploaded this as a sample project on GitHub: Download
/res/values/arrays.xml:
<string-array name="items">
<item>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt.</item>
<item>At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est.</item>
</string-array>
/res/layout/spinner_item.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="none"
android:minHeight="?android:attr/listPreferredItemHeight"
android:singleLine="false" />
Set Adapter
:
spinner.setAdapter(ArrayAdapter.createFromResource(this,
R.array.items,
R.layout.spinner_item));