Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom expandable notifications in Jelly Bean (4.1)

Jelly Bean added support for expandable status notification. According to http://developer.android.com/about/versions/jelly-bean.html:

In addition to the templated styles, you can create your own notification styles using any remote View.

How do do this? I believe to do this you need to create a custom Notification.Style. It's an abstract class so I it needs to be extended. I haven't been able to find any documentation on which parts need to be extended.

This SO question gives a good example of how to use the notificaiton.builder for basic notificaitons, I'm using this as a starting point. Adding

.setContent(new RemoteViews(getPackageName(), R.layout.notification)) 

adds a custom view for basic notifications, but it's not expandable.

like image 573
RobB Avatar asked Sep 12 '12 04:09

RobB


2 Answers

You need to create your own RemoteViews, then indicate that you want the expanded content to inherit your custom RemoteViews.

 RemoteViews expandedView = new RemoteViews(YOUR CONTEXT.getPackageName(), YOUR CUSTOM LAYOUT);
 Notification notification = mBuilder.build();
 notification.bigContentView = expandedView;

Note that bigContentView is what you're looking for. mBuilder is a Notification.Builder object.

like image 177
adneal Avatar answered Sep 28 '22 13:09

adneal


There's a good tutorial here on how to do it and how to create your own layout too.

enter image description here

Basically you need to create a remoteView with your layout and then set it as bigContentView in the Notification object. Btw make sure you also add the reguler contentView because the OS will use the smaller contentView in some cases.

like image 25
Jimmy Avatar answered Sep 28 '22 13:09

Jimmy