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 - Google MAP V2 PART 3 (add Polyline)

Download Full Code  of  PART-1 , 2 , 3 , 4 Before go ahead Please look in to PART-1 & PART-2 I have just modify Main Activity class: package com.djandroid.mapsv2; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap.OnMapClickListener; import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.maps.model.Polyline; import com.google.android.gms.maps.model.PolylineOptions; public class MainActivity extends FragmentActivity { private GoogleMap MAP; private boolean markClic...

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

Connecting mysql Database in ANDROID using PHP & JSON

 To implement this tutorial you should have basic knowledge of how to run PHP script and start server.  If we talk about client-server architecture, client is Android device and in server side there is a combination of PHP Script and MySQL. In short, PHP Script sits in middle as shown in image. Lets suppose that we have a MySQL database named Employee, and a table int created, with the following SQL: CREATE TABLE `employee` (   `emp_id` int(11) NOT NULL auto_increment,   `emp_name` varchar(100) NOT NULL,   PRIMARY KEY  (`emp_id`) ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; The PHP code will be very simple: Now Create Android Project :  The Android part is only a bit more complicated: -use a HttpPost to get the data -convert response to string -parse JSON data in to List In Your First Activity : O/P :