Question

I want to set a notification for my app in the notifications drawer, but only with a title in it (no body), meaning - only one line of notification, which should be vertically aligned to the app icon.

For example, in the following notification I'd like to keep only the "Ringer shushed 'til 19:16" title, in the same font size, but vertically centered to the app icon on the left.

enter image description here

Here is my code for creating the notification:

NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT);

PendingIntent deleteIntent = PendingIntent.getService(context, 0,
                new Intent(context, GCMIntentService.class)
                        .setAction(IntentConsts.ACTION_CLEAR_MESSAGE_COUNT), PendingIntent.FLAG_CANCEL_CURRENT);

manager.notify(MMConfig.NTF_ID_GCM, new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.push_icon)
                .setContentTitle("My title")
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentIntent(contentIntent)
                .setDeleteIntent(deleteIntent)
                .build());

Right now I succeed creating the notification with only title in it, but I can't seem to make it vertically centered with the app icon. Any ideas?

Was it helpful?

Solution

Like some suggested, I used RemoteViews to customize my notification, as following:

final RemoteViews views = new RemoteViews(getPackageName(), R.layout.notification_new_message);

views.setTextViewText(R.id.title, title);

Notification notification = new Notification();
notification.contentView = views;
notification.icon = R.drawable.push_icon;
notification.contentIntent = contentIntent;

manager.notify(Config.NTF_ID_GCM, notification);

Where I set inside my notification_new_message.xml the desirable look of my notification.

OTHER TIPS

I don't think this is possible. Even I tried some ways of achieving your idea. But nothing does seem to work the way you want. As far as considering the alignment I don't think its practically possible that too in the vertical alignment. Even if you try removing the Title Still the place for the Title is filled with blank space which covers more than half of your notification area, so even with that...still the body of the notification doesn't seem to be proper center aligned.

Also I tried aligning the content of a text view and then using this for the value within the setLatestEventInfo(), still not even the horizontal alignment seems to be working as far as notifications are considered.

I might be wrong, still with the level of research I went through it doesn't seem to be possible. Lemme know if there is any alternative ways found:P

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top