How to create a custom notification on Android

In this article, we will explain how is possible to create a custom notification layout to replace the standard one in the notification bar.
To do it we need to use the Android object RemoteViews, in the library android.widget.RemoteViews. This is available since API level 1 so it will work for every kind of Android versions.
Let’s create our custom notification!


First of all, we just create a custom layout in an xml file. Let’s call it custom_notification.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_notification"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" >

    <ImageView
        android:id="@+id/notifiation_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />

    <TextView
        android:id="@+id/notification_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/notification_image"
        android:layout_alignTop="@+id/notification_image"
        style="@style/NotificationTitle"
    />

    <TextView
        android:id="@+id/notification_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/notification_image"
        android:layout_below="@+id/notification_title"
        style="@style/NotificationText"
    />

</RelativeLayout>

This layout will be the one showed into the notification bar.

IMPORTANT: All the TextView of your custom notification must have a style declared to prevent issues caused by the different background colors of the notification bar for various version of Android.

Our app will read the style from different files depending on the Android version. If the version is 2.2 or lower, the style must be defined in the file res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="NotificationText">
      <item name="android:textColor">?android:attr/textColorPrimaryInverse</item>
    </style>
    <style name="NotificationTitle">
      <item name="android:textColor">?android:attr/textColorPrimaryInverse</item>
      <item name="android:textStyle">bold</item>
    </style>
</resources>

For Android 2.3 and higher versions, we need to define the style in res/values-v9/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />
    <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" />
</resources>

IMPORTANT: There is an error in the attributes provided by Android, the procedure on Android DEV Guide, will lead you to have errors in colors. In order to fix it, we modified the attribute textColorPrimary in textColorPrimaryInverse

From our Java code, we have to get the NotificationManager:

int NOTIFICATION_ID = 1;
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

and create a new Notification:

int icon = R.drawable.icon;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, getString(R.string.text), when);

Than we have to instantiate a RemoteViews object that inflates our custom layout file, fill the View objects and pass the RemoteViews to the contentView field of our Notification.

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setImageViewResource(R.id.notification_image, R.drawable.notification_image);
contentView.setTextViewText(R.id.notification_title, "My custom notification title");
contentView.setTextViewText(R.id.notification_text, "My custom notification text");
notification.contentView = contentView;

We instantiate the intent to call our Activity when the notification is clicked and add it to the contentIntent field of the Notification:

Intent notificationIntent = new Intent(this, MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.contentIntent = contentIntent;

Set some flags for notification behaviour and launch the notification:

notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
notification.defaults |= Notification.DEFAULT_SOUND; // Sound

mNotificationManager.notify(NOTIFICATION_ID, notification);

To cancel the notification we just need to do:

mNotificationManager.cancel(NOTIFICATION_ID);
android-tutorial , , , , ,

4 responses to How to create a custom notification on Android


  1. Juan

    Thank very usefull!

  2. fkohyf

    Very good article. Thanks for sharing!

  3. Andy Res

    +1 for metioning that all TextViews of notification must have a style declared to prevent issues caused by the different background colors.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>