Android Notification with Actions and a DropDown Menu
Asked Answered
D

2

10

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?

Diadromous answered 17/9, 2018 at 20:55 Comment(8)
This guy's answer will save you https://mcmap.net/q/401482/-adding-button-action-in-custom-notificationIodism
Well i do not want do make a notification with a button, I do want to expand my normal android notification after an notification action was clicked!Diadromous
@VIGOPIXelInteractiveInc i do not want to make a notification with a costum content view, the notification should just expand on notification action click (a bit like the reply feature, where a textview appears in the notification...Diadromous
@Diadromous you mean you want notification just like in a musicplayer notification with buttons ,text and images?Demos
@KevinKurien I've a normal notification with some actions.. When the user clicks on the 'Snooze' action button, the notification should expand, showing a RadioButton list with some possible snooze delays... I think I've seen that in a google app but I do not remember which one it was!Diadromous
Interesting question. I'm curious tooFortuna
developer.android.com/training/notify-user/custom-notification use this link what you need is the big notification contentLooming
@pouya But I need to call my custom layout after a Notification action was pressed.. If I use a expandable layout, this is not possible?Diadromous
R
0

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

Rummer answered 16/2, 2019 at 11:10 Comment(1)
Well, this is clear, but is there a way to make a notification(dialog) like shown above accessible via a notification action and retrieve its value?Diadromous
A
0

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();
Abeyant answered 18/2, 2019 at 8:58 Comment(8)
With this solution, I can reach that transformation effect and finally implement a spinner?Diadromous
Yes Sir @DiadromousAbeyant
Is it possible to open the Big ContentView ONLY via notification action?Diadromous
What do you mean by 'notification action' ?Abeyant
Well as described in my question, the "dropdown list" should be shown by clicking on the notification action "snooze".Diadromous
Well but the user will be able to open the expanded view (with the dropdown list) by pulling... This must not be possible.Diadromous
You can update your notification layout with the expanded view on click of your action button in previous notification, if that is what you are asking.Abeyant
can't modify largeIcon with NotificationCompat.DecoratedCustomViewStyle Arredondo

© 2022 - 2024 — McMap. All rights reserved.