12

I'm trying to color the buttons(Action) in Notification like this. enter image description here

So far this is what i'm achieved so far. enter image description here

Below is the code i'm using

NotificationService.class

private void showCallNotification(Map<String, String> dataMap) {
        notificationId = (int) (System.currentTimeMillis() % 10000);
        Intent intent = new Intent(getApplicationContext(), ReceiveCallActivity.class)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .setAction(Intent.ACTION_ANSWER)
                .putExtra(AppConstants.CALL_STATUS, AppConstants.CALL_ACCEPTED)
                .putExtra("title", dataMap.get("title"))
                .putExtra("action", dataMap.get("action"));

        Intent cancelIntent = new Intent(getApplicationContext(), VideoCallReceiver.class)
                .setAction(AppConstants.INCOMING_CALL_BROADCAST_ACTION)
                .putExtra(AppConstants.CALL_STATUS, AppConstants.CALL_DECLINED)
                .putExtra("notificationId", notificationId);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent cancelPendingIntent = PendingIntent.getBroadcast(this, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Action declineAction = new NotificationCompat.Action(android.R.drawable.ic_menu_call, getString(R.string.decline_call), cancelPendingIntent);
        NotificationCompat.Action acceptAction = new NotificationCompat.Action(android.R.drawable.ic_menu_call, getString(R.string.accept_call), pendingIntent);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, AppConstants.CALL_CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(dataMap.get("sender"))
                .setContentText(getString(R.string.incoming_call))
                .setColor(getResources().getColor(R.color.notification_color))
                .setAutoCancel(true)
                .setTimeoutAfter(CALL_DISMISS_TIME)
                .addAction(declineAction)
                .addAction(acceptAction)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setFullScreenIntent(pendingIntent, true);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createCallNotificationChannel();
        }

        notificationManager.notify(notificationId, builder.build());
    }

I'm out of ideas now. Any help will be appreciated.

Shahal
  • 1,008
  • 1
  • 11
  • 29

4 Answers4

18

Code taken from the Android official Dialer app

when you set the action string getString(R.string.decline_call) change that with a call to the method

  private Spannable getActionText(@StringRes int stringRes, @ColorRes int colorRes) {
    Spannable spannable = new SpannableString(context.getText(stringRes));
    if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
      spannable.setSpan(
          new ForegroundColorSpan(context.getColor(colorRes)), 0, spannable.length(), 0);
    }
    return spannable;
  }
Aenon
  • 346
  • 1
  • 3
  • 7
3

I have managed to apply colors for the notification action buttons. Use spannable just like Aeron suggested (code from the Dialer app) and the very important thing is to set USE_FULL_SCREEN_INTENT permission. Otherwise colors are ignored in the dark mode or look incorrect in the non dark mode.

Alex
  • 66
  • 2
0

You can use custom layout in your notification. There are setCustomContentView() and setCustomBigContentView methods:

// Get the layouts to use in the custom notification
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);

// Apply the layouts to the notification
Notification customNotification = new NotificationCompat.Builder(context, CHANNE L_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
        .setCustomContentView(notificationLayout)
        .setCustomBigContentView(notificationLayoutExpanded)
        .build();

Do have a read here

ravi
  • 899
  • 8
  • 31
  • 1
    After setting `setCustomBigContentView` and `setStyle(new NotificationCompat.DecoratedCustomViewStyle())` , notification is not showing up anymore. Anything else i need to be wary of ? – Shahal Oct 01 '19 at 06:58
-2

Use custom drawable like this

NotificationCompat.Action declineAction = new NotificationCompat.Action(R.drawable.ic_red, getString(R.string.decline_call), cancelPendingIntent);
NotificationCompat.Action acceptAction = new NotificationCompat.Action(R.drawable.ic_green, getString(R.string.accept_call), pendingIntent);
Ankit
  • 1,068
  • 6
  • 10
  • 1
    `new NotificationCompat.Action()` is deprecated. Also even with the new method, icon is not showing. And this is not what i'm trying to implement, I'm trying to colour the buttons not add icons to it. Anyways thanks for your quick reply. – Shahal Oct 01 '19 at 07:00