Android multiline notifications / notifications with longer text-ThrowExceptions

Exception or error:

I need to create a Notification with a longer text, is that possible? By default it is not, but you can use a custom layout, which is what I did. Now I can display multiple lines, but as you can see, the text is still broken / not displayed completely? ): Can someone please tell me what I am doing wrong / if there’s a fixed limit for the size of notifications? If you look at the screenshot you will notice, that there is still a lot of space left… Thanks for any hint!

BTW here’s the XML used for the custom layout, based on http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomNotification

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:padding="3dp"
          >
<ImageView android:id="@+id/image"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_marginRight="10dp"
          />
<TextView android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:textColor="#000"
          />
</LinearLayout>

Longer notification text

How to solve:

For Jelly Bean and higher you can use an expandable notification. The easiest way is to use the NotificationCompat.BigTextStyle for your notification.

Like so:

NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(getString(R.string.title));
bigTextStyle.bigText(getString(R.string.long_explanation));

mBuilder.setStyle(bigTextStyle);

###

NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setContentTitle(title)
                    .setStyle(new NotificationCompat.BigTextStyle()
                                      .bigText(message))
                    .setContentText(message)                                            
                    .setDefaults(NotificationCompat.DEFAULT_SOUND)
                    .setContentIntent(contentIntent)
                    .setAutoCancel(true);

mNotificationManager.notify(requestID, mBuilder.build());

once reffer https://developer.android.com/guide/topics/ui/notifiers/notifications.html

###

Notification view was limited by 65sp in height. This was implementation detail and is not documented and has been changed in Android 4.1 to support expandable notifications. So do not rely on this specific value, but rather rely on the fact that view has limited height.

Here is status_bar_latest_event.xml that was used to inflate views in Notification area:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="65sp"
    android:orientation="vertical"
    >

    <com.android.server.status.LatestItemView android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="64sp"
            android:background="@drawable/status_bar_item_background"
            android:focusable="true"
            android:clickable="true"
            android:paddingRight="6sp"
            >
    </com.android.server.status.LatestItemView>

    <View
        android:layout_width="match_parent"
        android:layout_height="1sp"
        android:background="@drawable/divider_horizontal_bright"
        />

</LinearLayout>

###

My understanding is that Android’s notification system has a limited height per notification in order to avoid a single notification filling up the screen.

From the page you linked:

Caution: When you use a custom notification layout, take special care to ensure that your custom layout works with different device orientations and resolutions. While this advice applies to all View layouts, it’s especially important for notifications because the space in the notification drawer is very restricted. Don’t make your custom layout too complex, and be sure to test it in various configurations.

You can, however, display multiple notifications, ‘sticky’ notifications or perhaps scrolling text inside the notification.

For more information on what you can do with Notifications, see:

Notification and
Notification Builder

###

Android provide a big view expandable notifications they supports 3 styles , bigpicture style,inbox style,big text style (256 dp) , but only from android versions greater then jelly bean. for lower versions we dont have any big text style notification.

###

This is what worked for me on 5.0
It nicely wraps long lines. And it also lets you provide an array of strings that will be displayed separated with a new line.

        String[] multilineDescription = new String[] { "line 1", "another very long line that will get wrapped." };

        NotificationCompat.Builder builder = new NotificationCompat.Builder(appContext)
                .setSmallIcon(smallIcon)
                .setContentTitle(title)
                .setContentText("Pull down for more information");

        String description;
        if (null == multilineDescription || multilineDescription.length == 0) {
            description = "No more information.";
        } else {
            description = multilineDescription[0];

            for (int i=1; i<multilineDescription.length; i++) {
                description += System.getProperty("line.separator");
                description += multilineDescription[i];
            }
        }

        builder.setStyle(new NotificationCompat.BigTextStyle().bigText(description));

Leave a Reply

Your email address will not be published. Required fields are marked *