user291701
user291701

Reputation: 39731

Use a different drawable for menu item based on sdk level?

I'd like a menu item to use a different drawable depending on api level:

// my_menu.xml
<menu>
  <item android:icon="@style/foo"/>
</menu>

// values/styles.xml
<drawable name="foo">@drawable/apple</drawable>

// values-v11/styles.xml
<drawable name="foo">@drawable/orange</drawable>

Doesn't seem to work though - is there a way to do that?

Thank you

------------- Update ------------------

Ok after some more reading, this is how I did it:

// my_menu.xml
<menu>
  <item style="@style/foo"/>
</menu>

// values/styles.xml
<style name="foo">
    <item name="android:icon">@drawable/apple</item>
</style>

// values-v11/styles.xml
<style name="foo">
    <item name="android:icon">@drawable/orange</item>
</style>

I think this is what lxx is suggesting.

Upvotes: 1

Views: 692

Answers (1)

pandavid87
pandavid87

Reputation: 151

You should be able to do this programatically after you inflate your menu in your code.

        onCreateWhateverMenu(Menu menu){

        ///inflate menu
        ...

        MenuItem item = menu.findItem(itemId)
        if(Build.VERSION.SDK_INT == <insert API level>){
           item.setIcon(your Drawable);
        }
        else{
           item.setIcon(other Drawable);
        }

        ...
    }

Hope this helps.

Upvotes: 2

Related Questions