Leandros Leandros - 4 months ago 28
Android Question

Change ActionBar Menu Icons depending on Style

I want to use different

ActionBar
Icons depending on which style I use (Dark or Light).
I couldn't figure it out how, heres what I tried:

<style name="AppThemeDark" parent="Theme.Sherlock.ForceOverflow">
<item name="android:actionButtonStyle">@style/customActionButtonStyleDark</item>
<item name="actionButtonStyle">@style/customActionButtonStyleDark</item>
</style>

<style name="AppThemeLight" parent="Theme.Sherlock.Light.ForceOverflow">
<item name="android:actionButtonStyle">@style/customActionButtonStyleLight</item>
<item name="actionButtonStyle">@style/customActionButtonStyleLight</item>
</style>

<style name="customActionButtonStyleDark" >
<item name="@drawable/action_search">@drawable/action_search</item>
</style>

<style name="customActionButtonStyleLight" >
<item name="@drawable/action_search">@drawable/action_search_light</item>
</style>


I also tried to insert
<item name="@drawable/action_search">@drawable/action_search</item>
directly into the theme style, but nothing worked.

Any ideas how?

Answer

Solved it, but gave it up to try it with XML, I did it now programmatically:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        SharedPreferences prefs = getSharedPreferences("app", 0);
        boolean isDark = "Dark".equals(prefs.getString("theme", "Dark"));

        com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.main, menu);

        // set Icons
        menu.findItem(R.id.menuitem_search).setIcon(isDark ? R.drawable.action_search : R.drawable.action_search_light);
        menu.findItem(R.id.menuitem_add).setIcon(isDark ? R.drawable.content_new : R.drawable.content_new_light);
        menu.findItem(R.id.menuitem_share).setIcon(isDark ? R.drawable.social_share : R.drawable.social_share_light);
        return true;
    }