Recently I decided to use custom layout for showing notification. But there was a problem. When I install the app, the notification is displayed correctly. It doesn't matter if the OS theme is light or dark. But after Installing , when I change the theme of the operating system, the colors of the title and text are not changed . Until I reinstall the application or reset the mobile, the colors are displayed correctly and this process continues... I also used "@style/TextAppearance.Compat.Notification"
This happens in both light and dark theme modes for notification custom layout . not happen for non-customized and default notification layout. They work correctly.
For this example I installed the app in dark theme mode and change OS theme later. Tested Device: Redmi Note 3Pro - Custom Rom Based AOSP V9
and vice versa
XML Code for collaps mode:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layoutDirection="rtl"
android:layout_width="match_parent"
android:layout_height="64dp">
<TextView
android:id="@+id/TVNotTitle"
style="@style/TextAppearance.Compat.Notification.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="عنوان پیام" />
<ImageView
android:id="@+id/IVNotif"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
tools:src="@drawable/sibil" />
<TextView
android:id="@+id/TVNotInfo"
style="@style/TextAppearance.Compat.Notification.Info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/TVNotTitle"
android:layout_toStartOf="@id/IVNotif"
android:ellipsize="end"
android:maxLines="1"
tools:text="این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است " />
</RelativeLayout>
Xml expand mode:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layoutDirection="rtl"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="256dp">
<TextView
android:id="@+id/TVNotTitle"
style="@style/TextAppearance.Compat.Notification.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="عنوان پیام" />
<ImageView
android:id="@+id/IVNotif"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentEnd="true"
tools:src="@drawable/sibil" />
<TextView
android:id="@+id/TVNotInfo"
style="@style/TextAppearance.Compat.Notification.Info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/TVNotTitle"
android:layout_toStartOf="@id/IVNotif"
android:ellipsize="end"
android:maxLines="6"
tools:text="این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است این یک پیام جدید است " />
</RelativeLayout>
Notification Code :
val notification = NotificationCompat.Builder(context, myStructure.channel_id)
.setSmallIcon(R.drawable.ic_notification)
.setCustomContentView(collapsView)
.setCustomBigContentView(expandView)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setContentIntent(pendingIntent)
notificationManager.notify(1, notification.build())
I can add theme and night theme if needed.
I solved it by adding style to both theme.xml in day and night file.
night\themes.xml:
<style name="Base.AppTheme.notification.info" parent="TextAppearance.Compat.Notification.Info">
<item name="android:textColor"> @color/white2 </item>
<item name="android:textSize">12sp</item>
</style>