2

How to create inshorts like push notifications for andriod. The notifications include full image with text at the bottom of the notification.

The small notification is just the text but while u expand it, it shows the image in background with text at bottom.

Can some one please help me with this. Below are the screenshots

enter image description here

Nikhil
  • 167
  • 3
  • 16

2 Answers2

7

You need to inflate a custom remote view as a layout for the notification as follows :

layout_custom_notification.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image_news"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@mipmap/dine_main_item_bg1" />

    <TextView
        android:id="@+id/tv_news_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:layout_toLeftOf="@+id/tv_news_time"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:text="Trump signs order to roll back Obama-era climate policies"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/tv_news_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="20dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:text="9:14 AM"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginTop="20dp"
        android:padding="8dp"
        android:text="Share"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

</RelativeLayout>

Thereafter, set this layout as a contentview in notification as follows :

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.layout_custom_notification);
        contentView.setImageViewResource(R.id.image_news, R.drawable.ic_launcher);
        contentView.setTextViewText(R.id.tv_news_time, "9:14 AM");
        contentView.setTextViewText(R.id.tv_news_title, "Trump signs order to roll back Obama-era climate policies");

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(icon)
            .setContent(contentView)
            .setContentTitle("Custom Notification")
            .setContentIntent(contentIntent)   
            .setWhen(when);

...
mNotificationManager.notify(1, notificationBuilder.build());
Suraj Makhija
  • 1,376
  • 8
  • 16
0

PFB the screenshot of the notification having padding at top and bottom. Test 62 is the notification message, it is coming in the white space. I want it to come on top of image at the bottom.

enter image description here

Nikhil
  • 167
  • 3
  • 16
  • 1
    You can include android:scaleType="centerCrop" in ImageView of layout_custom_notification.xml as shown in my updated answer – Suraj Makhija Mar 29 '17 at 10:12