Search code examples
androidservicenotificationsthemesandroid-remoteview

Changing Notification RemoteViews Background Color


I've a problem changing my Background-Color with the Application-Theme.

NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this);

TypedValue typedValue = new TypedValue();

getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);

int iPrimaryColor = typedValue.data;

getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);

int iPrimaryDarkColor = typedValue.data;

Intent notIntent = new Intent(getApplicationContext(), MainActivity.class);
notIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent notOpenOnClick = PendingIntent.getActivity(getApplicationContext(), 0, notIntent, PendingIntent.FLAG_UPDATE_CURRENT);

RemoteViews smallContentView = new RemoteViews(getPackageName(), R.layout.notification_small);
RemoteViews bigContentView = new RemoteViews(getPackageName(), R.layout.notification_expanded);

nBuilder.setSmallIcon(R.drawable.not_icon)
    .setOngoing(true)
    .setContentTitle(getCurrentSong().getTitle())
    .setContentIntent(notOpenOnClick);

Notification not = nBuilder.build();

smallContentView.setInt(R.id.not_linLayout, "setBackgroundColor", iPrimaryColor);
smallContentView.setInt(R.id.not_imvDivider, "setBackgroundColor", iPrimaryDarkColor);

bigContentView.setInt(R.id.not_linLayout, "setBackgroundColor", iPrimaryColor);
bigContentView.setInt(R.id.not_imvDivider, "setBackgroundColor", iPrimaryDarkColor);

setListeners(smallContentView);
setListeners(bigContentView);

not.contentView = smallContentView;
not.bigContentView = bigContentView;

if (isPlaying()) {
    not.contentView.setImageViewResource(R.id.not_btnPlayPause, R.drawable.ic_pause_48dp);
    not.bigContentView.setImageViewResource(R.id.not_btnPlayPause, R.drawable.ic_pause_48dp);
}
else {
    not.contentView.setImageViewResource(R.id.not_btnPlayPause, R.drawable.ic_play_48dp);
    not.bigContentView.setImageViewResource(R.id.not_btnPlayPause, R.drawable.ic_play_48dp);
}

I already tried this, but the Background of my Notification is still white. The ID's are correct, the View linLayout is a LinearLayout.

Please keep in mind: The whole codes is invoked in a Service!

Thanks!


Solution

  • Much of this can be easier done by taking advantage of NotificationCompat.MediaStyle. It pulls the background color from the setColor() call on pre-API 24 devices (and uses that color as an accent on API 24+ devices). This also means you don't need to write any custom RemoteViews code anymore as it relies solely on the actions you add to your notification for media controls:

    NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this);
    nBuilder.setSmallIcon(R.drawable.not_icon)
      .setContentTitle(getCurrentSong().getTitle())
      .setContentIntent(notOpenOnClick);
    // This is what sets the background color on <N devices
    // It is an accent color on N+ devices
    nBuilder.setColor(getResources().getColor(R.color.colorPrimary));
    // Add actions via nBuilder.addAction()
    // Set the style, setShowActionsInCompactView(0) means the first
    // action you've added will be shown the non-expanded view
    nBuilder.setStyle(new NotificationCompat.MediaStyle()
      .setShowActionsInCompactView(0));
    

    You should definitely read over all of the methods available for MediaStyle and reivew the Best Practices in media playback I/O 2016 talk for example code and best practices around using notifications. Specifically at 30 minutes into the talk