BigPictureStyle Notification in Android Example

In the previous post we have learnt on how to show notifications in Android, now we shall see how to show Large icon in notification bar. This style of notifications are called as BigPicturestyle Notifications.

For example so many appsĀ like, Flipkart, Mobikwik, Paytm etc… they randomly post big picture notifications, the image consists of brief guide about offers etc…

Bigpicturestyle Notification

With help of V4 appcompat library, we can implement this for all devices from Android 2.0 onward.

  • Create Notification object, here set the title, content text, ticker text etc… The content text gets disappear once you expand the notification.
        //Create notification object and set the content.
        NotificationCompat.Builder nb= new NotificationCompat.Builder(this);
        nb.setSmallIcon(R.drawable.ic_small_icon);
        nb.setContentTitle("Set your title");
        nb.setContentText("Set Content text");
        nb.setTicker("Set Ticker text");
  • Create a bitmap of the image which needs to be shown as big picture & set it as shown in below code. Set the summaryText, this will appear on expanding the notification.
        //get the bitmap to show in notification bar
        Bitmap bitmap_image = BitmapFactory.decodeResource(this.getResources(), R.drawable.notification_big_picture);
        NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle().bigPicture(bitmap_image);
        s.setSummaryText("Summary text appears on expanding the notification");
        nb.setStyle(s);

Notification before expansion

Collapsed Notification Android

Notification after expansion

Bigpicturestyle Notification

 

  • Set the intent that should be launched on clicking on the notification.
        Intent resultIntent = new Intent(this, MainActivity.class);
        TaskStackBuilder TSB = TaskStackBuilder.create(this);
        TSB.addParentStack(MainActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        TSB.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                TSB.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        nb.setContentIntent(resultPendingIntent);
        nb.setAutoCancel(true);
        NotificationManager mNotificationManager =
                (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(11221, nb.build());