Search code examples
androidactionbarsherlockspinner

Sherlock actionbar spinner looks 'old' in lower Android versions


I've found this question (Android 2.2 Spinner looks old in Actionbar) and I tried it, but it isn't working for me! I want to create a Spinner in my Sherlock Actionbar, but it looks old in lower android versions. This is my code:

Oncreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
}

Oncreateoptionsmenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {


    MenuInflater mi = getSupportMenuInflater();
    mi.inflate(R.menu.list, menu);

    final Spinner spinner = new Spinner(getSupportActionBar()
    .getThemedContext());


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getSupportActionBar().getThemedContext(),
    R.layout.sherlock_spinner_item, new String[]{"Level 1", "Level 2", "Level 3"});
    adapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
        int arg2, long arg3) {
            // clicked

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

    menu.add("Level").setActionView(spinner)
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    return true;
}

Solution

  • That's because Spinner is old, and ActionBarSherlock does not affect the behavior of Spinner.

    If, instead of your code, you use setNavigationMode(ActionBar.NAVIGATION_MODE_LIST), you will get a drop-down list that probably looks the way you want... but that is not a Spinner. Rather, that is a backport called IcsSpinner that is part of ActionBarSherlock's internal implementation. IcsSpinner is not part of the ActionBarSherlock public API AFAIK, though if you poke around you will find some people treating it as such anyway (running the risk of their apps breaking if Jake Wharton changes IcsSpinner).

    Either:

    • Use setNavigationMode(ActionBar.NAVIGATION_MODE_LIST), or

    • Try a supported backport of holo-themed widgets, such as HoloEverywhere, or

    • Pull out ActionBarSherlock's IcsSpinner into your own code base, so that you can maintain your copy independently of changes made to ActionBarSherlock, or

    • Use ActionBarSherlock's own IcsSpinner directly, despite the risks, or

    • Live with the older look and feel on older devices, or

    • Don't use a drop-down list in your action bar on older devices