Skip to main content

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

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());  

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. in which version of android OS?

      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
  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

Post a Comment

Popular posts from this blog

ANDROID - Adding ActionBar Navigation Tabs

Note: if you are develop App < 3.0 Android OS then use  ActionBarSherlock   support library. ActionBarSherlock is an extension of the  support library  designed to facilitate the use of the action bar design pattern across all versions of Android with a single API. Create new Android Project : in Main Activity package com.AlarmManager; import android.os.Bundle; import android.view.View; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.ActionBar.Tab; import com.actionbarsherlock.app.SherlockFragmentActivity; public class AlarmManagerActivity extends SherlockFragmentActivity { public static String ACTIVE_TAB = "activeTab"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { setTheme(R.style.Theme_Sherlock_Light_DarkActionBar); super.onCreate(savedInstanceState); // setContentView(R.lay

Android Material Design (with Design support Library) - 4

Introducing  Design Support Library  , to use Material design component in older version API - android 2.1 and above.  You’ll find a navigation drawer view, floating labels for editing text, a floating action button, snackbar, tabs, and a motion and scroll framework to tie them together. I have used my previous example, so its easy for demonstrate.  Note: Update your Android SDK support  repositories, support library if not updated i  used compileSdkVersion 'android-MNC' for Android M but you can change it to build in older API add  dependencies in build.gradle file compile 'com.android.support:appcompat-v7:22.2.0' compile 'com.android.support:design:22.2.0' compile 'com.android.support:support-v4:22.2.0' start with  navigation drawer , its very easy to use lets, design for drawer <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://

Android show Data from Sqlite DB into Grid View

Shaadi.com Matrimonials Shaadi.com Indian Matrimonials Your Main Activity class package com . Sqlite_grid_view ; import java . util . ArrayList ; import java . util . List ; import android . app . Activity ; import android . os . Bundle ; import android . util . Log ; import android . view . View ; import android . widget . AdapterView ; import android . widget . AdapterView . OnItemClickListener ; import android . widget . ArrayAdapter ; import android . widget . GridView ; import android . widget . TextView ; import android . widget . Toast ; public class AndroidSQLiteTutorialActivity extends Activity { private GridView gridView ; public static ArrayList < String > ArrayofName = new ArrayList < String >(); /** Called when the activity is first created. */ @ Override public void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( R . l