Android Notification with Actions and a DropDown Menu

1.8k views Asked by At

Android's Google app has a Notification which shows the local weather for example. If you swipe left on the notification, it shows a "clock icon"-action (e.g. Snooze) as shown below.

Google Notification with Weather Forecast

If you click on the clock, the following dropdown menu opens:

Notification with dropdown menu

This is a feature of the android system, I want to implement in my app. It should be opened via a notification action, I want to set custom options, and I need to get the selected value.

Does anyone know how to implement that?

2

There are 2 answers

1
nkalra0123 On

This is standard, for all the Notification from Android oreo.

For all the notification you get this option for snoozing. If you want to implement this same ui for older Android device, you can directly check the source code of Android oreo System UI, from where we get the snooze option in Android 8 and above.

http://androidxref.com/8.1.0_r33/xref/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/NotificationSnooze.java#274

Check the use of this string

<!-- Notification: Snooze panel: message indicating how long the notification was snoozed for. [CHAR LIMIT=100]-->
1520    <string name="snoozed_for_time">Snoozed for <xliff:g id="time_amount" example="15 minutes">%1$s</xliff:g></string>

http://androidxref.com/8.1.0_r33/xref/frameworks/base/packages/SystemUI/res/values/strings.xml#1519

8
Vishal Arora On

The expand feature is provided by the Android System for different notification styles. You need a view which displays a list of options that the user can pick from. So you need to create a Custom View and populate it with your options.

You can set the custom view using Notification.DecoratedCustomViewStyle() provided by Android Notification System.

If you want different appearances for collapsed and expanded view then you use the following methods to set them -

setStyle(new Notification.DecoratedCustomViewStyle())
   .setCustomContentView(remoteViews)
   .setCustomBigContentView(bigRemoteView);

You will need to add different pending intent for all the options you specify in your layout.

For example -

RemoteViews firstOption = ....;
Intent firstOptionIntent = // add some argument in this intent which depicts this option
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,   flags);
setOnClickPendingIntent(R.id.<option_layout_id>, pendingIntent);

// similary for other options

RemoteViews secondOption = ....;

To add a dropdown list on notification action click, you need to use 2 different layouts, one for the collapsed view and one for the expanded view -

Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.notification_icon)
    .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
    .setCustomContentView(notificationLayout) // collapsed view layout
    .setCustomBigContentView(notificationLayoutExpanded) // expanded view layout
    .build();