1

I am building a media player app based on Google's Universal Media Player. Now I'm facing a problem with notification the player creates.

Codes to create notification:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mService);

notificationBuilder
            .setStyle(new NotificationCompat.MediaStyle()
                .setShowActionsInCompactView(
                        new int[]{playPauseButtonPosition})  // show only play/pause in compact view
                .setMediaSession(mSessionToken))
            .setColor(mNotificationColor)
            .setSmallIcon(R.drawable.ic_notification)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setUsesChronometer(true)
            .setContentIntent(createContentIntent(description))
            .setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle())
            .setLargeIcon(art);

This code makes the following notification: enter image description here

After that I remove .setColor(mNotificationColor) portion from the code. I thought it makes the background of the notification white. But now the background turns to grey like this: enter image description here

So now I want to set background color to white, this is easy part. setColor can do the job, but at that time I want the text color black. Cannot figure out how to change the text color. The icons of play/pause is also not changed even I supply black icons.

enter image description here

Ratul
  • 302
  • 6
  • 20

1 Answers1

0

Try some custom notification like this:

When you use Notification and set the text by using built-in means, the following line creates the layout:

  RemoteViews contentView = new RemoteViews(context.getPackageName(),
            com.android.internal.R.layout.status_bar_latest_event_content);

The mentioned layout contains the following View which is responsible for viewing notification text:

<TextView android:id="@+id/text"
    android:textAppearance="@style/TextAppearance.StatusBar.EventContent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:fadingEdge="horizontal"
    android:paddingLeft="4dp" />

So the conclusion is that the needed style is TextAppearance.StatusBar.EventContent, which definition looks like this:

<style name="TextAppearance.StatusBar.EventContent">
    <item name="android:textColor">#000000</item>
</style>

And here are some ref. Hope this will resolve your issue.

Link1

Link2

Community
  • 1
  • 1
  • I add `` as this is the style name I found in the [source code](https://github.com/android/platform_frameworks_support/blob/master/v7/appcompat/res/layout/notification_template_lines_media.xml). But nothing happens. Text color is still white. – Ratul Apr 10 '17 at 13:08
  • the reason for using tools:override="true" is : _overriding @style/TextAppearance_AppCompat_Notification_Title_Media which is marked as private in com.android.support:appcompat-v7. If deliberate, use tools:override="true", otherwise pick a different name. Private resources should not be referenced; the may not be present everywhere, and even where they are they may disappear without notice. To fix this, copy the resource into your own project instead._ this appears when I use without override. – Ratul Apr 10 '17 at 13:26
  • what is your min sdk and which device youhave tested. –  Apr 10 '17 at 13:27
  • Try something like this : contentView.setTextColor(R.id.text2, statColor); –  Apr 10 '17 at 13:31
  • Min SDK version is 17 and I test on Nexus 5 running Android 6.0.1 – Ratul Apr 10 '17 at 13:32
  • how can I set contentView.setTextColor? As the notification style is built in, set by `new NotificationCompat.MediaStyle()` – Ratul Apr 10 '17 at 13:34
  • Actually U need to change this inbild style and add their own –  Apr 10 '17 at 13:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/141363/discussion-between-neo-and-ratul). –  Apr 10 '17 at 13:42