Vipul Behl
Vipul Behl

Reputation: 634

Notification icon appearing small in Android

see this imageI'm trying to add icon to the notification, but the notification icon comes very small and doesn't cover up the entire space.

I've right image sizes in each resource folder.

The code that I've for notification:

Intent myIntent = new Intent(getApplicationContext(), Lock.class);
         PendingIntent pendingIntent = PendingIntent.getActivity(
                getApplicationContext(), 
                0, 
                myIntent, 
                Intent.FLAG_ACTIVITY_NEW_TASK);

         myNotification = new NotificationCompat.Builder(getApplicationContext())
           .setContentTitle("Invisible Screen Lock")
           .setContentText("Lock Your Screen")
           .setContentIntent(pendingIntent)
           .setDefaults(Notification.DEFAULT_SOUND)
           .setSmallIcon(R.drawable.launcher)
           .setOngoing(true)
           .build();

         notificationManager = 
           (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
         notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

How can I make the icon appear correctly?

Upvotes: 0

Views: 829

Answers (2)

Logic
Logic

Reputation: 2258

Try giving a Large Icon

    Notification.Builder nb = new Notification.Builder(context)
    .setContentTitle("title")
    .setContentText("content")
    .setAutoCancel(true)
    .setLargeIcon(drawableToBitmap(YOUR_DRAWABLE))
    .setSmallIcon(R.drawable.small_icon)
    .setTicker(s.getText());
   NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
   nm.notify(100, nb.build());

add this method

public static Bitmap drawableToBitmap (Drawable drawable) {
    Bitmap bitmap = null;

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if(bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }

    if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

Upvotes: 1

Audumbar
Audumbar

Reputation: 404

Use setLargeIcon(icon) as follow:

myNotification = new NotificationCompat.Builder(getApplicationContext())
       .setContentTitle("Invisible Screen Lock")
       .setContentText("Lock Your Screen")
       .setContentIntent(pendingIntent)
       .setDefaults(Notification.DEFAULT_SOUND)
       .setLargeIcon(icon)
       .setOngoing(true)
       .build();

Upvotes: 1

Related Questions