24

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

erdemlal
  • 491
  • 5
  • 21
stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
  • 1
    Try using `layout_height="wrap_content"` on all your views. – Felix Jun 15 '11 at 12:13
  • thanks, but that doesn't change anything ): – stefan.at.kotlin Jun 15 '11 at 12:17
  • As far as I'm aware you're not able to make notifications any bigger than the default size shown in your screen shot, but I may be wrong. – Tom O Jun 15 '11 at 12:17
  • Just a thought. Does adding a scrollview to the text view work? – Mahadevan Sreenivasan Jun 15 '11 at 12:22
  • at least not using remoteview and xml, because remoteview doesn't support a scrollview ): source: http://stackoverflow.com/questions/4351459/why-my-tablelayout-isnt-allowed-to-be-inflated and the error message I got when I tried it ;-) Just wondering if I can build the layout manually / by code? – stefan.at.kotlin Jun 15 '11 at 12:35

6 Answers6

64
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

Ravi
  • 4,872
  • 8
  • 35
  • 46
sreeniwasa
  • 641
  • 5
  • 2
51

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);
Divisible by Zero
  • 2,616
  • 28
  • 31
5

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>
Community
  • 1
  • 1
inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • Not that I wouldn't believe you, but do you have an official (android documentation) source for that? Just wondering where I could have looked that up. Thanks! – stefan.at.kotlin Jun 15 '11 at 12:24
  • Thanks for the source, great! (the source, not the fact that it is limited :P) – stefan.at.kotlin Jun 15 '11 at 12:34
  • :) Well it kinda makes sense to limit the height. As any third party app can abuse Notification Area by just increasing its height to some arbitrary value. The usability of notifications will drop drastically in such cases, and everybody would blame Android, not the app. – inazaruk Jun 15 '11 at 12:36
  • I agree that it makes sense in general, but I think it should be possible to give more inforamtion / longer text to the user. You could just show a preview and than you have to expand the single notification and can collapse it again or sth. like that. – stefan.at.kotlin Jun 15 '11 at 12:38
  • I think the idea of Notification is two show that more info is available in your application. Why don't you create some custom Dialog/Activity that is launched on click and has your message fully shown in it? I think that was the pattern developers of NotificationManager had in mind. – inazaruk Jun 15 '11 at 12:43
  • 1
    @stefan.at.wfp - you could make the intent associated with your notification display this information with less intrusion than a full screen activity. The simple way to do this would be to use Theme.Translucent so that your activity only shows what you want, leaving full visibility behind it. – mah Jun 15 '11 at 12:44
2

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));
HANiS
  • 530
  • 5
  • 9
1

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.

g4gaj
  • 85
  • 2
  • 11
1

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

erdemlal
  • 491
  • 5
  • 21
Andre
  • 3,150
  • 23
  • 23
  • I assume the same like you, however I am still looking for an "official source" for this behaviour. Hmm scrolling text, that's an idea :-) Will look into this, thanks! – stefan.at.kotlin Jun 15 '11 at 12:18
  • 1
    Beware that `Notification.Builder` is only available in Android 3.0 and above. – Felix Jun 15 '11 at 12:22
  • 1
    hmm well, in my understanding it doesn't really say in which ways (height / size?) it is restricted? the documentation could be more specific... – stefan.at.kotlin Jun 15 '11 at 12:22