BJ Dela Cruz
BJ Dela Cruz

Reputation: 5354

How to Properly Set the Icon in Notification Area?

I am trying to set an icon in the Notification Area for my Android application.

I noticed that the icon in the ticker appears to be cut off. When the ticker goes away, the icon that is in the Notification Area looks good. Here is my code where I set the icon:

String msg = "Calculation completed. Tap to view result.";
final Intent restartActivityIntent = new Intent(context, NumberOfPrimesActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, restartActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews mContentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);
mContentView.setImageViewResource(R.id.image, R.drawable.two);
mContentView.setTextViewText(R.id.text, msg);
Notification.Builder notificationBuilder = new Notification.Builder(context).setSmallIcon(R.drawable.two).setContentTitle("Sum of Primes").setContentText(msg).setAutoCancel(true).setContentIntent(pendingIntent).setTicker(msg);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, notificationBuilder.build());

Here is my custom layout for my notification:

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/toast_layout_root"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#7777"
      android:padding="3dp">

      <ImageView android:id="@+id/image"
         android:layout_width="44dp"
         android:layout_height="44dp"
         android:layout_marginRight="10dp"
         android:contentDescription="alert"
         android:src="@drawable/two" />

      <TextView
         android:id="@+id/text"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:textColor="#FFF"
         android:textSize="24sp" />

   </LinearLayout>

Here is how my res directory looks like:

enter image description here

The two.png file in the ldpi folder is 22x22.

Finally, here are two screenshots. The first shows the ticker with the icon cropped off. The second shows the icon after the ticker goes away. The icon looks good in the second screenshot.

enter image description here enter image description here

Updates

The two.png file in the mdpi folder is 48x48.

The two.png file in the hdpi folder is 72x72.

The two.png file in the xhdpi folder is 92x92.

The two.png file in the xxhdpi folder is 144x144.

I am using simulators to test my program. I have two simulators. One is a Nexus 7 (800 x 1280), and another is a Nexus 4 (768 x 1280).

On both simulators, the icon in the ticker is cut off.

Upvotes: 1

Views: 1550

Answers (1)

TactMayers
TactMayers

Reputation: 884

It looks like for whatever reasons, the icon in ticker view will not be automatically resized, but in the notification view it would.

According to Android Iconography, "Notification icons must be 24x24 dp.", which translates to the following dimensions in different screen densities:

ldpi: 18 x 18px
mdpi: 24 x 24px
hdpi: 36 x 36px
xhdpi: 48 x 48px

So if you resize the two.png icons in the res folder accordingly, the icon would not be cut off in both ticker and notification views.

Upvotes: 4

Related Questions