Custom Notification Layouts and Text Colors

Custom notification layouts and text colors

Solution by Malcolm works fine with API>=9. Here's the solution for older API:

The trick is to create the standard notification object and then traverse the default contentView created by Notification.setLatestEventInfo(...). When you find the right TextView, just get the tv.getTextColors().getDefaultColor().

Here's the code that extracts the default text color and text size (in scaled density pixels - sp).

private Integer notification_text_color = null;
private float notification_text_size = 11;
private final String COLOR_SEARCH_RECURSE_TIP = "SOME_SAMPLE_TEXT";

private boolean recurseGroup(ViewGroup gp)
{
final int count = gp.getChildCount();
for (int i = 0; i < count; ++i)
{
if (gp.getChildAt(i) instanceof TextView)
{
final TextView text = (TextView) gp.getChildAt(i);
final String szText = text.getText().toString();
if (COLOR_SEARCH_RECURSE_TIP.equals(szText))
{
notification_text_color = text.getTextColors().getDefaultColor();
notification_text_size = text.getTextSize();
DisplayMetrics metrics = new DisplayMetrics();
WindowManager systemWM = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
systemWM.getDefaultDisplay().getMetrics(metrics);
notification_text_size /= metrics.scaledDensity;
return true;
}
}
else if (gp.getChildAt(i) instanceof ViewGroup)
return recurseGroup((ViewGroup) gp.getChildAt(i));
}
return false;
}

private void extractColors()
{
if (notification_text_color != null)
return;

try
{
Notification ntf = new Notification();
ntf.setLatestEventInfo(this, COLOR_SEARCH_RECURSE_TIP, "Utest", null);
LinearLayout group = new LinearLayout(this);
ViewGroup event = (ViewGroup) ntf.contentView.apply(this, group);
recurseGroup(event);
group.removeAllViews();
}
catch (Exception e)
{
notification_text_color = android.R.color.black;
}
}

Call extractColors ie. in onCreate() of your service. Then when you're creating the custom notification, the color and text size you want are in notification_text_color and notification_text_size:

Notification notification = new Notification();
RemoteViews notification_view = new RemoteViews(getPackageName(), R.layout.notification);
notification_view.setTextColor(R.id.label, notification_text_color);
notification_view.setFloat(R.id.label, "setTextSize", notification_text_size);

How to create a Custom Notification Layout in android?

I used BitTextStyle() to add highlighted text in notification.

return new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_mono)
.setContentTitle(title)
.setContentText(message)
.setLargeIcon(icon)
.setColor(ContextCompat.getColor(context, R.color.notification_color))
.setStyle(new NotificationCompat.BigTextStyle().bigText(title))
.setStyle(new NotificationCompat.BigTextStyle().bigText(message).setSummaryText("#hashtag"))
.setShowWhen(true)
.setAutoCancel(true);

Get notification texts colors and background

Maybe you'll find solution in these posts?

  • Custom notification layouts and text colors
  • How to match text color with notification color bar?

Colored text and icons in notification bar in Android Nougat

You can use a custom contentView on your Notification Builder

To define a custom notification layout, start by instantiating a
RemoteViews object that inflates an XML layout file. Then, instead of
calling methods such as setContentTitle(), call setContent(). To set
content details in the custom notification, use the methods in
RemoteViews to set the values of the view's children:

Create an XML layout for the notification in a separate file. You can
use any file name you wish, but you must use the extension .xml In
your app, use RemoteViews methods to define your notification's icons
and text. Put this RemoteViews object into your
NotificationCompat.Builder by calling setContent(). Avoid setting a
background Drawable on your RemoteViews object, because your text
color may become unreadable.

and the code is as follow:

RemoteViews mycontentView = new RemoteViews(getPackageName(), R.layout.notification);
mycontentView.setImageViewResource(R.id.myimage, R.mipmap.ic_launcher);
mycontentView.setTextViewText(R.id.mytitle, "Custom Notification");

NotificationCompat.Builder myBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContent(mycontentView);

Notification myNotification = myBuilder.build();
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.defaults |= Notification.DEFAULT_VIBRATE;
myNotificationManager.notify(1, myNotification);

where R.layout.notification is your custom layout file

and the layout file is below

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="64dp"
android:padding="12dp" >
<ImageView
android:src="@mipmap/ic_launcher"
android:id="@+id/myimage"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp" />
<TextView
android:textSize="12dp"
android:textColor="#000"
android:text="Testing"
android:id="@+id/mytitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/myimage"
/>

</RelativeLayout>

I hope it helps.



Related Topics



Leave a reply



Submit