Question

I have a custom spinner that uses two custom views, one for the drop down and one for the currently selected item.

My problem is, the custom view for the currently selected item is always at least as wide as the longest item in the spinner list and it leaves a sizeable gap on the left hand side of my custom view. If I select the longest item from the spinner, the gap disappears. I have tried android:gravity="left" for my custom view. Also note that this only seems to be an issue with Android 3.0+.

This shows the gap when a short spinner entry is selected

Notice no gap is present between the icon and folder name

Here is the code for my custom spinner view:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/folder_detail" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="wrap_content">

<TextView android:text="Folder Name" 
    android:id="@+id/folder_name" android:textColor="#ffffff"
    android:layout_width="wrap_content" android:textSize="16sp"
    android:textStyle="bold"
    android:layout_height="wrap_content" android:layout_marginTop="0dp">
</TextView>

    <TextView android:id="@+id/folder_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="13sp"  android:text="(0) " android:textColor="@color/light_blue" android:layout_marginLeft="2dp"/>


<ImageView android:id="@+id/icon" android:src="@android:drawable/arrow_down_float" 
    android:layout_marginRight="1dp" android:scaleType="fitXY"
    android:layout_gravity="bottom" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
 </LinearLayout> 
Was it helpful?

Solution

This is intentional. It prevents the spinner from drastically changing size as the selection changes.

You're not meant to include an arrow as part of the item view; this is normally included as part of the background 9-patch of the spinner itself. The gravity setting on the spinner itself controls the alignment of the selected item view within the spinner. The Holo theme aligns it to the left, while the legacy themes horizontally center.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top