Question

I have icons for my Android menu. On Android 3+ I'm using a black ActionBar so the icons are white. However, on Android 2.x the menu is inherently white which means the icons are nearly invisible. How can I use different menu icons for different versions? I'm assuming I can do it using different drawable directories like res/drawable-mdpi-v11, but I'm wondering if there is another way so I don't have to create a bunch of different directories as I add versions or pixel densities.

EDIT: I put dark versions in res/drawable-mdpi and res/drawable-hdpi for use with Android 2.x and I put light versions in res/drawable-mdpi-v11 and res/drawable-hdpi-v11 for use with Android 3.x and higher, but my Android 2.1 (sdk 7) emulator is still showing the light version.

Any idea why?

Was it helpful?

Solution

You can Select a theme based on platform version, as outlined in the Styles and Themes dev guide. Define a style in your res/values/styles.xml like this:

<style name="ThemeSelector" parent="android:Theme.Light">
    ...
</style>

Then in a res/values-v11/ folder, select your theme (probably Holo, if you're dark)

<style name="ThemeSelector" parent="android:Theme.Holo">
    ...
</style>

Then add icons to that style. For instance, here's a snippet from the styles.xml file from the HoneycombGallery sample application.

<style name="AppTheme.Dark" parent="@android:style/Theme.Holo">
    ...
    <item name="menuIconCamera">@drawable/ic_menu_camera_holo_dark</item>
    <item name="menuIconToggle">@drawable/ic_menu_toggle_holo_dark</item>
    <item name="menuIconShare">@drawable/ic_menu_share_holo_dark</item>
</style>

The bottom 3 elements are all icons in the drawable directories. You'll still need at least one folder per resolution-specific set of icons, but you can combine the light & dark icons into the same folder, but you won't have to have different folders of icons for each platform version. Also, you'll need to list them as references in the values/attrs.xml file, like this:

<resources>
  <declare-styleable name="AppTheme">
    <attr name="listDragShadowBackground" format="reference" />
    <attr name="menuIconCamera" format="reference" />
    <attr name="menuIconToggle" format="reference" />
    <attr name="menuIconShare" format="reference" />
  </declare-styleable>
</resources>

At which point you'll be able to refer to them within your layout XML using the "?attr/NameOfYourDrawable" dereference, like this:

<item android:id="@+id/menu_camera"
      android:title="@string/camera"
      android:icon="?attr/menuIconCamera"
      android:showAsAction="ifRoom" />

OTHER TIPS

Found on the android dev site: http://developer.android.com/guide/practices/ui_guidelines/icon_design_menu.html

Warning: Because these resources can change between platform versions, you should not reference these icons using the Android platform resource IDs (i.e. menu icons under android.R.drawable). If you want to use any icons or other internal drawable resources, you should store a local copy of those icons or drawables in your application resources, then reference the local copy from your application code. In that way, you can maintain control over the appearance of your icons, even if the system's copy changes. Note that the grid below is not intended to be complete.

/res/drawable-hdpi (for Android 2.2 and below)

/res/drawable-hdpi-v# (for Android 2.3 and above)

Have you also tried testing this on a 2.1+ phone and not an emulator? If you don't have a phone, try creating another AVD? I'm afraid that you're going to need the separate folders.

Hopefully this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top