1

I have this code of Spinner in XML:

<Spinner
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@style/ThemeOverlay.AppCompat.Light"
    android:dropDownWidth="match_parent"
    android:spinnerMode="dropdown" />

But the dropdown width is cropped because of the icon shown. How to make it full width like the parent?

enter image description here

2
  • this answer suggests a workaround if you want to keep the arrow and still have the dropdown menu take up the full width: stackoverflow.com/a/47548396/10300673 Commented Jul 11, 2019 at 7:52
  • Hi @NikosHidalgo I managed to solve it by programmatically, going to post it shortly Commented Jul 11, 2019 at 7:57

2 Answers 2

2

I am able to solve it do it programmatically:

parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        if (Build.VERSION.SDK_INT > 16) {
            parentLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        } else {
            parentLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }

        spinnerDropdown.setDropDownWidth(parentLayout.getWidth());
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for posting this. Any idea how the listener affected your desired outcome?
As in my case, I need the spinnerDropdown to be full width of the parentLayout, so I only need to set the dropdownWidth to be equal parentLayout's width, thus, need to be done in onGlobalLayout
match parent wasn't working for me, but his worked thanks
0

Based on randy's solution

Targeted device on and above 21

Reusable extension

fun ViewGroup.addViewObserver(function: (View) -> Unit) {
val view = this
view.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
    override fun onGlobalLayout() {
        view.viewTreeObserver.removeOnGlobalLayoutListener(this)
        function.invoke(view)
    }
})

}

In Activity

parentViewGroup.addViewObserver {
    spinnerView.dropDownWidth = it.width
}

Note: parentViewGroup is the view containing the spinner and spinnerView is the spinner itself

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.