aragaer
aragaer

Reputation: 17848

Android notification width

I am writing an application that builds a notification from several "symbols". Sometimes these won't fit and I'll just stop and add a number of symbols that did not fit.

Currently I'm looking at device's screen width to determine the number of symbols that do fit. Like this:

DisplayMetrics dm = resources.getDisplayMetrics();
int num = dm.widthPixels / height;

where height is "hardcoded" value of 64dp.

This does not work on tablets though. As result I'm losing rightmost symbols. Is there any way to determine the actual amount of space in my notification?

On this picture I'm using another hardcoded value of no more than 7 symbols total. enter image description here

Upvotes: 5

Views: 1500

Answers (3)

Rohan Lodhi
Rohan Lodhi

Reputation: 1395

Follow below code:

Notification noti = new Notification.Builder(context)
        .setContentTitle(context.getString(R.string.app_name))
        .setContentText(notification_description).setDefaults(Notification.DEFAULT_ALL)
        .setStyle(new NotificationCompat.BigTextStyle() .bigText(msg).
                setSmallIcon(R.drawable.application_icon_notification)
        .setContentIntent(contentIntent)
        .setAutoCancel(true)
        .build();

Upvotes: -1

aragaer
aragaer

Reputation: 17848

Here's the answer I've been waiting for: 478dp

Source

Which results in about 7.5 symbols per notification and having a value of 7 hardcoded is correct.

Upvotes: 1

rgrocha
rgrocha

Reputation: 1461

Are using height=64 or the equivalent of 64dp in pixels?

Why not get the width and height of your top layer to get the usable screen size instead of full screen size (including system and ActionBars)?

Upvotes: 3

Related Questions