Question

Hello stackoverflow community. I'm working on a notification function using NotificationCompat. I would like the notification icon to be dark for Android < v11 and light for Android >=11. I'm followed the design guidelines and created the icons, but I've had no luck making the right one display automatically.

All of the tutorials I've found set the icon using setIcon(R.drawable.icon_name_here). But hard coding the drawable resource only allows you to set one icon. So if it's a dark icon it looks great in Android < v11, but looks ridiculous in Android >= v11.

I've tried adding the following style in res/values/styles.xml:

<resources>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
    <item name="icon_notification">@drawable/ic_notification_dark</item>
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

And here is my res/values-v11/styles.xml:

<resources>

<!--
    Base application theme for API 11+. This theme completely replaces
    AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
    <!-- API 11 theme customizations can go here. -->
    <item name="icon_notification">@drawable/ic_notification_light</item>
</style>

Here is my res/values/attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="AppBaseTheme">
    <attr name="icon_notification" format="reference" />
  </declare-styleable>
</resources>

And finally, here is where the icon is set in the NotificationCompat class:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.attr.icon_notification)
                .setContentTitle("Notification Title")
                .setContentText("notification message")
                            .setNumber(++num)
                .setAutoCancel(true).setOnlyAlertOnce(false)
                .setTicker("Ding Dong!").setSound(ringtonePrefUri)
                .setWhen(System.currentTimeMillis())
                .setDefaults(Notification.DEFAULT_VIBRATE);

That's all the code. I just want to set the right notification icon depending on the Android version. It seems simple but I have not found a simple and clear explanation. Please help!

Was it helpful?

Solution

Just use something like this:

int iconRes = (Build.VERSION.SDK_INT < 11) ? R.drawable.icon_dark : R.drawable.icon_light

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(iconRes)
            .setContentTitle("Notification Title")
            .setContentText("notification message")
                        .setNumber(++num)
            .setAutoCancel(true).setOnlyAlertOnce(false)
            .setTicker("Ding Dong!").setSound(ringtonePrefUri)
            .setWhen(System.currentTimeMillis())
            .setDefaults(Notification.DEFAULT_VIBRATE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top