Question

I have the following Spinner in my ActionBar:

enter image description here

The "MY SHOPPING LIST" item is disabled. As you can see, the dividers are black, even though my themes are as follows:

<?xml version="1.0" encoding="utf-8"?>

<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">

  <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">

    <item name="android:actionBarWidgetTheme">@style/Theme.Apptheme.Widget</item>

  </style>

</resources>

And the styles:

<?xml version="1.0" encoding="utf-8"?>

<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- this style is only referenced in a Light.DarkActionBar based theme -->
    <style name="Theme.Apptheme.Widget" parent="@android:style/Theme.Holo">
        <item name="android:popupMenuStyle">@style/PopupMenu.Apptheme</item>
        <item name="android:dropDownListViewStyle">@style/DropDownNavActionbar.Apptheme</item>
    </style>

    <style name="DropDownNavActionbar.Apptheme" parent="DropDownListView.Apptheme">
        <item name="android:listDivider">@android:color/white</item>
    </style>
</resources>

I was suggested to use actionDropDownStyle, add a listDivider attribute with value @android:color/white, but that doesn't work.

Was it helpful?

Solution

Add these styles

<style name="DropDownListView.ActionBar.Apptheme" parent="@style/DropDownListView.Apptheme">
    <item name="android:dividerHeight">1dp</item>
    <item name="android:divider">@android:color/white</item>
</style>

<style name="Theme.Apptheme.Widget.ActionBarSpinner">
    <item name="android:dropDownListViewStyle">@style/DropDownListView.ActionBar.Apptheme</item>
</style>

And then enable it with:

ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
Context themedContext = new ContextThemeWrapper(this, R.style.Theme_Apptheme_Widget_ActionBarSpinner);
Spinner navSpinner = new Spinner(themedContext);
navSpinner.setAdapter(new ActionBarSpinnerAdapter(themedContext, shoppingList));
navSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {
        Toast.makeText(MainActivity.this, "Item #" + position + " clicked", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent)
    {
        // do nothing
    }
});
actionBar.setCustomView(navSpinner);

Adapter code if you want it:

private class ActionBarSpinnerAdapter extends BaseAdapter
{
    public static final int TYPE_CATEGORY = 0;
    public static final int TYPE_ITEM     = 1;
    public static final int MAX_TYPE      = 2;

    private LayoutInflater mInflater;
    private ArrayList<ShoppingListItem> mData;


    public ActionBarSpinnerAdapter(Context context, ArrayList<ShoppingListItem> data)
    {
        mInflater = LayoutInflater.from(context);
        mData = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        final int type = getItemViewType(position);
        if(convertView == null){
            final int layoutId;
            switch(type){
                case TYPE_CATEGORY: layoutId = android.R.layout.preference_category; break;
                case TYPE_ITEM:     layoutId = android.R.layout.simple_list_item_1;  break;
                default: throw new IllegalArgumentException("Bad type: " + type);
            }
            convertView = mInflater.inflate(layoutId, parent, false);
        }

        TextView textView = (TextView)convertView;
        textView.setText(mData.get(position).getName());

        return convertView;
    }

    @Override
    public ShoppingListItem getItem(int position)
    {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return mData.get(position).getName().hashCode();
    }

    @Override
    public int getItemViewType(int position)
    {
        return mData.get(position).getType();
    }

    @Override
    public int getViewTypeCount()
    {
        return MAX_TYPE;
    }

    @Override
    public boolean areAllItemsEnabled()
    {
        return true;
    }

    @Override
    public boolean isEnabled(int position)
    {
        return (getItemViewType(position) != TYPE_CATEGORY);
    }


    @Override
    public int getCount()
    {
        return mData.size();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top