Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make the spinner width same as largest available item width

Tags:

android

I realize even I use wrap_content for my spinner (I avoid using match_parent as I do not want the spinner overly long), and using match_parent for spinner's item view and drop down item view,

I still find the spinner's width is not same as largest item's width

enter image description here

As you can see, the UnitedKingdom is not fit.

So, if I select the UnitedKingdom, only the spinner's will resize

enter image description here

How can I make the spinner resize to the largest available item width, even without being explicitly selecting the largest item?

My spinner XML is

<Spinner     android:id="@+id/spinner1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_marginBottom="10dp" />  

and my spinner's views XML are

<TextView xmlns:android="http://schemas.android.com/apk/res/android"      android:id="@+id/text_view_0"     style="?android:attr/spinnerItemStyle"     android:singleLine="true"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:drawablePadding="10dp"     android:gravity="center_vertical" /> 

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"      android:id="@+id/checked_text_view_0"     style="?android:attr/spinnerDropDownItemStyle"     android:singleLine="true"     android:layout_width="match_parent"     android:layout_height="48dp"     android:minHeight="48dp"     android:drawablePadding="10dp" /> 
like image 888
Cheok Yan Cheng Avatar asked May 19 '13 10:05

Cheok Yan Cheng


1 Answers

You should be able to get the effect you want by specifying the width of the dropdown as WRAP_CONTENT:

<Spinner     android:id="@+id/spinner1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_marginBottom="10dp"     android:dropDownWidth="wrap_content"     android:spinnerMode="dropdown" /> 

This will measure up to 15 items in the spinner's adapter to determine the width it should use.

like image 168
Ted Hopp Avatar answered Sep 22 '22 02:09

Ted Hopp