Android custom layout for push notification view

notification_layout.xml, the layout to be used for the push notification view.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_notification"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp" >
    <ImageView
        android:id="@+id/iv_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp"
        android:src="@drawable/ic_launcher" />
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_image"
        android:textStyle="bold"
        android:textColor="#000"
        android:text="Custom Notification" />
    <TextView
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_image"
        android:layout_below="@id/tv_title"
        android:textColor="#716f6f"
        android:text="To have a notification appear in an expanded view, first create a NotificationCompat.Builder object with the normal view options you want. Next, call Builder.setStyle() with an expanded layout object as its argument." />
</RelativeLayout>

Inflate the push notification layout using RemoteViews and build the notification.

val contentView = RemoteViews(packageName, R.layout.notification_layout)

val mBuilder = NotificationCompat.Builder(this)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle("collapsed title")
        .setContentText("collapsed text")
        .setCustomBigContentView(contentView)

val notification = mBuilder.build()
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(1, notification)

Complete example in Github

Search within Codexpedia

Custom Search

Search the entire web

Custom Search