1

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.

3
  • 1
    Is this Spinner in an Activity with other spinners?
    – Paul Burke
    Aug 22, 2013 at 13:35
  • Unfortunately yes. Activity contains more fragments (some of them contains spinners) and one fragment contains this special styled spinner.
    – Koso
    Aug 22, 2013 at 15:01
  • Will you be awarding this bounty?
    – Paul Burke
    Aug 26, 2013 at 13:55

1 Answer 1

3
+50

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.

2
  • Thanks a lot man! You just confirmed what I thought. I just hoped I'm still overlooking something because it's weird, that it's not possible to achieve this using styles/themes.
    – Koso
    Aug 23, 2013 at 15:19
  • 1
    Glad to help. Now about that bounty... ;-)
    – Paul Burke
    Aug 23, 2013 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.