1

I am trying to set the temperature to fill the whole square of the notification icon. I have tried various settings and tweaks with the code and this is the best I can get it. I started off by creating some notification icons at the Android Assets Studio website and refer to them as ic_notification in their corresponding drawable folders. The PNG I created is a blank PNG where the 3.4 sits in it as seen in the picture, it is the black box in the picture.

I have run this on a Table (10.1" sized) and a small phone like the Samsung Galaxy S and the it stays the same size as in the picture below. I would like to fill the whole box not just the black box, covering the light blue part too. Can someone explain why it doesn't fill the whole screen, or how to get it to fill the whole screen?

Notification Icon

The code I use is the following, parts[4] is the temperature taken from an array and degrees is the Fahrenheit or Celsius symbol depending on what the users selected in the settings. The icon gets set when the notification is build .setLargeIcon(drawableToBitmap(iconDrawable)) after the drawable is created.

        BitmapDrawable iconDrawable = writeOnDrawable(R.drawable.ic_notification, parts[4] + degrees);
        public BitmapDrawable writeOnDrawable(int drawableId, String text) {

        Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(),
                drawableId).copy(Bitmap.Config.ARGB_8888, true);

        Paint paint = new Paint();
        paint.setStyle(Style.FILL);
        paint.setColor(Color.WHITE);
        paint.setTextSize(bm.getWidth() / 3);
        paint.setTextAlign(Align.CENTER);
        Canvas canvas = new Canvas(bm);
        //Log.e("log_etag", "Drawable Text " + text);
        //Log.e("log_etag", "getWidth bitMap " + bm.getWidth());
        //Log.e("log_etag", "Text Width " + text.length());

        Rect bounds = new Rect();
        float x = bm.getHeight() / 2, y = bm.getWidth() / 2;
        // get text lenght
        paint.getTextBounds(text, 0, text.length(), bounds); 
        // canvas.drawLine(0, y, canvas.getWidth(), y, paint); // Included
        // to show vertical alignment
        // canvas.drawLine(x, 0, x, canvas.getHeight(), paint); // Included
        // to show horizontal alignment
        // draw text
        canvas.drawText(text, x, y + bounds.height() * 0.5f, paint); 

        return new BitmapDrawable(mContext.getResources(), bm);

    }

And I use the following to convert to bitmap

    public Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }
Dino
  • 561
  • 3
  • 10
  • 21
  • Some time ago I had problems with small and big icons in Android being messed up. I am not certain this is the case with you too, but you can consider this post: http://stackoverflow.com/a/19375412/1108032 Note that the icon screw up is mentioned in the PS, not the main topic of the post – Boris Strandjev Jan 29 '15 at 16:24
  • Interesting, but it doesn't seem to help my cause. I suppose I have to fiddle around with the drawable settings. – Dino Jan 29 '15 at 17:06

1 Answers1

0

If I'm not wrong, the problem comes when converting the drawable to bitmap. The call to createBitmap is defining the size of the bitmap equal to the current size of the drawable, but that's not correct. If the drawable is too small or too large it won't fit exactly in the view.

There are specific dimensions in the sdk that define exactly how large a notification bitmap icon should be. So try it this way:

int width = (int) getResources().getDimension(android.R.dimen.notification_large_icon_width);
int height = (int) getResources().getDimension(android.R.dimen.notification_large_icon_height);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
jmart
  • 2,769
  • 21
  • 36
  • I have tried that and it still does the same thing, however, it only does it for the Samsung Galaxy S4, I don't have another one, but on my friends Motorola G it covers the whole icon. So I'm wondering if it has something to do with Samsung's Touch Wiz branded phones. – Dino Mar 21 '15 at 09:30
  • 1
    Unfortunately Samsung modifies too many things in the Android system they shoudn't modify at all. I had many problems on Samsung devices as well in other areas. – jmart Mar 21 '15 at 12:59