Koso
Koso

Reputation: 3199

Different dropDownListViewStyles for different spinner styles

In my application theme I have dropDownListViewStyle and spinnerStyle for whole application. But in one fragment I need spinner with custom styled dropDownListViewStyle (I need to change divider). Is it possible to have spinner with other dropDownListViewStyle then is set in theme?

In spinner style or layout is not possible to set dropdown divider. Also is not possible to set dropDownListViewStyle in spinner style or layout.

I am really stucked, hope someone have the answer.

Upvotes: 1

Views: 1582

Answers (1)

Paul Burke
Paul Burke

Reputation: 25584

Unfortunately, dropDownListViewStyle is hardcoded in Spinner. If you look into the source, you'll find the DropdownPopup class, which extends ListPopupWindow. In ListPopupWindow the class of interest is DropDownListView where you'll find the constructor:

public DropDownListView(Context context, boolean hijackFocus) {
    super(context, null, com.android.internal.R.attr.dropDownListViewStyle);
    // ...
}

So the only way to change this is by theme, as you've assessed. Considering that the Spinner in question is being used in an Activity that requires the base theme, I know of only one workaround. Unfortunately, the only way to do it is to change the theme of the Fragment. This means that all Spinners in that Fragment will have the alternative theme. To change the theme of your Fragment at runtime, in your onCreateView do this:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // create ContextThemeWrapper from the original Activity Context with the custom theme
        Context context = new ContextThemeWrapper(getActivity(), R.style.My_Custom_Theme);
        // clone the inflater using the ContextThemeWrapper
        LayoutInflater localInflater = inflater.cloneInContext(context);
        // inflate using the cloned inflater, not the passed in default 
        return localInflater.inflate(R.layout.my_layout, container, false);
    }

Short of this, you're looking at creating a custom Spinner, which isn't too difficult, considering that it's open source.

Upvotes: 3

Related Questions