Android - Notifications - 3 (Applying a big view style to a notification)

11:15:00 Dhaval Sodha Parmar 18 Comments

Big picture style



 Bitmap icon1 = BitmapFactory.decodeResource(getResources(),  
                     R.drawable.dhaval1);  
   
           NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(  
                     this).setAutoCancel(true)  
                     .setContentTitle("DJ-Android notification")  
                     .setSmallIcon(R.drawable.ic_launcher).setLargeIcon(icon1)  
                     .setContentText("Hello World!");  
   
           NotificationCompat.BigPictureStyle bigPicStyle = new NotificationCompat.BigPictureStyle();  
           bigPicStyle.bigPicture(icon1);  
           bigPicStyle.setBigContentTitle("Dhaval Sodha Parmar");  
           mBuilder.setStyle(bigPicStyle);  
   
           // Creates an explicit intent for an Activity in your app  
           Intent resultIntent = new Intent(this, testActivity.class);  
             
           // The stack builder object will contain an artificial back stack for  
           // the  
           // started Activity.  
           // This ensures that navigating backward from the Activity leads out of  
           // your application to the Home screen.  
           TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);  
             
           // Adds the back stack for the Intent (but not the Intent itself)  
           stackBuilder.addParentStack(testActivity.class);  
             
           // Adds the Intent that starts the Activity to the top of the stack  
           stackBuilder.addNextIntent(resultIntent);  
           PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,  
                     PendingIntent.FLAG_UPDATE_CURRENT);  
           mBuilder.setContentIntent(resultPendingIntent);  
   
           NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
             
           // mId allows you to update the notification later on.  
           mNotificationManager.notify(100, mBuilder.build());  

Big text style





 Bitmap icon1 = BitmapFactory.decodeResource(getResources(),  
                     R.drawable.dhaval1);  
   
           NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(  
                     this).setAutoCancel(true)  
                     .setContentTitle("DJ-Android notification")  
                     .setSmallIcon(R.drawable.ic_launcher).setLargeIcon(icon1)  
                     .setContentText("Hello World!");  
   
           NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();  
           bigText.bigText("Android is a Linux-based operating system designed primarily for touchscreen mobile devices such as smartphones and tablet computers. Initially developed by Android, Inc., which Google backed financially and later bought in 2005,[12] Android was unveiled in 2007 along with the founding of the Open Handset Alliance: a consortium of hardware, software, and telecommunication companies devoted to advancing open standards for mobile devices.[13] The first Android-powered phone was sold in October 2008");  
           bigText.setBigContentTitle("Android");  
           bigText.setSummaryText("By: Dhaval Sodha Parmar");  
           mBuilder.setStyle(bigText);  
   
           // Creates an explicit intent for an Activity in your app  
           Intent resultIntent = new Intent(this, testActivity.class);  
   
           // The stack builder object will contain an artificial back stack for  
           // the  
           // started Activity.  
           // This ensures that navigating backward from the Activity leads out of  
           // your application to the Home screen.  
           TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);  
   
           // Adds the back stack for the Intent (but not the Intent itself)  
           stackBuilder.addParentStack(testActivity.class);  
   
           // Adds the Intent that starts the Activity to the top of the stack  
           stackBuilder.addNextIntent(resultIntent);  
           PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,  
                     PendingIntent.FLAG_UPDATE_CURRENT);  
           mBuilder.setContentIntent(resultPendingIntent);  
   
           NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
   
           // mId allows you to update the notification later on.  
           mNotificationManager.notify(100, mBuilder.build());  

Inbox style



 String[] events = new String[6];  
             
           events[0] = "Helloo..!";  
           events[1] = "How are you?";  
           events[2] = "HIII !!";  
           events[3] = "i am fine...";  
           events[4] = "what about you? all is well?";  
           events[5] = "Yes, every thing is all right..";  
   
           Bitmap icon1 = BitmapFactory.decodeResource(getResources(),  
                     R.drawable.dhaval1);  
   
           NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(  
                     this).setAutoCancel(true)  
                     .setContentTitle("DJ-Android notification")  
                     .setSmallIcon(R.drawable.ic_launcher).setLargeIcon(icon1)  
                     .setContentText("Hello World!");  
   
           NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();  
             
           // Sets a title for the Inbox style big view  
           inboxStyle.setBigContentTitle("Event Details");  
   
           // Moves events into the big view  
           for (int i = 0; i < events.length; i++) {  
   
                inboxStyle.addLine(events[i]);  
           }  
           // Moves the big view style object into the notification object.  
           mBuilder.setStyle(inboxStyle);  
   
           // Creates an explicit intent for an Activity in your app  
           Intent resultIntent = new Intent(this, testActivity.class);  
   
           // The stack builder object will contain an artificial back stack for  
           // the  
           // started Activity.  
           // This ensures that navigating backward from the Activity leads out of  
           // your application to the Home screen.  
           TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);  
   
           // Adds the back stack for the Intent (but not the Intent itself)  
           stackBuilder.addParentStack(testActivity.class);  
   
           // Adds the Intent that starts the Activity to the top of the stack  
           stackBuilder.addNextIntent(resultIntent);  
           PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,  
                     PendingIntent.FLAG_UPDATE_CURRENT);  
           mBuilder.setContentIntent(resultPendingIntent);  
   
           NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
   
           // mId allows you to update the notification later on.  
           mNotificationManager.notify(100, mBuilder.build());  

18 comments:

  1. Hey ! It doesn't work in my app. No errors in the code but I don't have the extras line. it means no link with bigtext. my mail : mimeticsounds@gmail.com thank you for your reply !

    ReplyDelete
    Replies
    1. because your device android version is less than 4.1. So, it will not be shown.
      Let's check it out.
      http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Compatibility

      Delete
  2. +1
    You did great examples.

    ReplyDelete
  3. Bro Is there any way that the notification will show always in full view no need to expend it to read the full message manually in the notification bar.

    ReplyDelete
    Replies
    1. you can't expand it by default.

      Documentation says : "A notification's big view appears only when the notification is expanded, which happens when the notification is at the top of the notification drawer, or when the user expands the notification with a gesture."

      http://developer.android.com/guide/topics/ui/notifiers/notifications.html#BigNotify

      but, you can try trick becoz first notification in the notification tree is always presented with the expanded view.

      so use,

      mNotification.priority = Notification.PRIORITY_MAX;

      Delete
    2. I am trying to set a BigContentTitle

      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
      .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
      .setSmallIcon(getNotificationIcon())
      .setContentTitle(title)
      .setContentText(messageBody);

      NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle(notificationBuilder);
      style.bigText(messageBody);
      style.setBigContentTitle(title);

      but its still not displaying the complete notification .. what am I doing wrong ?

      Delete
  4. hi, What should be the max size for image in bigpicturestyle?

    ReplyDelete
    Replies

    1. size does't metter we pass bitmap so its automatically resize, check Documentation,

      http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2_r1/android/app/Notification.java#Notification.0bigContentView

      or you can pass bitmap of specific size using

      BitmapFactory.decodeResource

      http://stackoverflow.com/a/8442683

      Delete
  5. can we use both big picture style and big text style in a same notification. I want to show text (line break) along with the image!

    ReplyDelete
    Replies
    1. No, we can't combine both style.. But you can do with custom notification.

      Check my answer on stack: http://stackoverflow.com/a/16168704

      Delete
  6. can we use both big picture style and big image style in the same notification. I want to show text(with line breaks) along with the image!

    ReplyDelete
  7. great example !!
    Do you know how to only add icon(status bar)
    like an alarm app?

    ReplyDelete
  8. how to get gujarati font in android notification

    ReplyDelete
    Replies
    1. Use gujarati ttf file and android Typeface

      Delete
  9. cant get this style when app is not ruining.only when app is ruining ?

    ReplyDelete
  10. Hi Dhaval, I have followed the instruction properly. But the expanded bigTextStyle is not working. I am testing in a lollipop device. What could be the main reason behind this?.
    Thank you.

    ReplyDelete
  11. thanks for your sharing.
    I'm facing the same issue than Varsha Dubey,
    It works great but only when my activity is in foreground. If i'm on android home, application not in foreground, a notification in old style appears.
    Any idea?

    ReplyDelete