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?
Step 1. TextView with wrapped text
The first thing to do is to to force simple
TextView
to wrap text. Its easy:Note the
singleLine
attribute here.Step 2. Custom layout
Now we should somehow set
singleLine
attribute tofalse
inTextView
used bySpinner
to show the item in the list.In your code you probably have place where you create adapter to use it with
Spinner
:The idea is to copy the android.R.layout.simple_spinner_dropdown_item layout to your project. Then modify it by setting
singleLine
attribute tofalse
inCheckedTextView
:For this, add file to
res/layout
folder namedmultiline_spinner_dropdown_item.xml
with next code:Note that this file is identical to android.R.layout.simple_spinner_dropdown_item layout, except it has
singleLine
set tofalse
now.Step 3. Creating Adapter with custom layout
Modify your adapter creating code to:
Here is screenshot from modified
SpinnerActivity
example from Android SDK:Define a custom layout and use it with the spinner and adapter.