0

How can I set the size of the icon in the header of a context menu?

enter image description here

The icon is set via ContextMenu.setHeaderIcon(int iconRes).

jenzz
  • 7,239
  • 6
  • 49
  • 69

1 Answers1

1

Just in case someone else faces the same problem, I have eventually implemented it the following way.

I have written a generic method which takes a Drawable and the desired width and height as parameters. It returns a scaled (Bitmap)Drawable based on the given specs.

public Drawable getScaledIcon( Drawable drawable, int dstWidth, int dstHeight ) {

    Bitmap bitmap = ( (BitmapDrawable) drawable ).getBitmap();
    Bitmap bitmapScaled = Bitmap.createScaledBitmap( bitmap, dstWidth, dstHeight, false );

    return new BitmapDrawable( getResources(), bitmapScaled );
}

The method for setting the context menu header icon takes as a parameter either a resource ID via ContextMenu.setHeaderIcon(int iconRes) or a Drawable via ContextMenu.setHeaderIcon(Drawable icon).

Use the latter in conjunction with getScaledIcon(...) and you're done! :)

jenzz
  • 7,239
  • 6
  • 49
  • 69