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

Android

Android Problem Overview


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" />

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

I merely "solve" the problem by using

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

200dp is just try-n-error value, to ensure all items in the drop down list are visible.

Solution 3 - Android

In such cases where you want to list all country or have big list
Try: spinnerMode="dialog" as shown below:

<Spinner
    android:id="@+id/spinnerId"
    android:layout_width="wrap_content"
    android:dropDownWidth="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:spinnerMode="dialog"/>

Solution 4 - Android

Put your TextView inside LineanLayout in Spinner Item XML,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<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" />
</LinearLayout>

And Pass your TextView's id to custom adapter constructor

CustomAdapter adapter = new CustomAdapter(this,R.layout.spinner_item,R.id.text_view_0,arraylist);

Constructor

  public CustomAdapter(Context context, int resource, int textViewId, ArrayList<String> resultList) {
                super(context, resource, textViewId, resultList);
        ...
        ...
            }

Solution 5 - Android

Set your width to 0dp and a layout weight of 1. The following will resize it for you.

    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="?android:attr/actionBarSize"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/test label"/>

    <Spinner
        android:id="@+id/test_spinner"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:dropDownWidth="wrap_content"
        android:layout_weight="1"
        android:prompt="@string/test_prompt"
        android:spinnerMode="dropdown" />
</LinearLayout>

Solution 6 - Android

You can try spinnerMode="dialog" but it is just a workaround, not the right answer. Because it will display a new window with a dialog.

So here is the way do to it. It will automatically fit the content.

It strongly depends how you set up your custom layout. May i suggest you to write your own xml file myspinner_custom_dropdown.xml. Recommanded way is now ConstraintLayout from androidX.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckedTextView
        android:id="@android:id/text1"
        style="?android:attr/spinnerDropDownItemStyle"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ellipsize="marquee"
        android:singleLine="true"
        app:layout_constraintStart_toEndOf="@+id/item_image"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/item_image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:src="@drawable/image"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Important : you have yo use your custom layout with your adapter

dataAdapter.setDropDownViewResource(R.layout.myspinner_custom_dropdown);

That's all, you do not need some kinds of hardcored attributes in your spinner declaration.

 <Spinner
            android:id="@+id/my_spinner"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp" />

Solution 7 - Android

Try this: android:layout_width="fill_parent"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCheok Yan ChengView Question on Stackoverflow
Solution 1 - AndroidTed HoppView Answer on Stackoverflow
Solution 2 - AndroidCheok Yan ChengView Answer on Stackoverflow
Solution 3 - AndroidSujay U NView Answer on Stackoverflow
Solution 4 - AndroidVickyView Answer on Stackoverflow
Solution 5 - AndroidCalysmicView Answer on Stackoverflow
Solution 6 - AndroidZharView Answer on Stackoverflow
Solution 7 - AndroidMahmoud AbdulfatahView Answer on Stackoverflow