• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Is there a way to determine the actual pixel size of your appwidget?

I'm writing an appwidget and the main part of it will be a bitmap that I create. I've been unable to find a way to programatically find out what the size of the appwidget is.

I know how to request that I get a 4 x 1 size widget, but I don't think I can assume that my widget will be a specific size in pixels across all phones though (or even proportionally the same). Drawing the bitmap myself seems like the best thing to do since it's mostly parts of other graphics and the contents and amount of graphics will change depending on what I need to display. I would have preferred to dynamically build a layout for the RemoteViews, but it's not supported in SDK version 3 which is a requirement for my widget to support.

Anyway, I want to be able to take advantage of the maximum amount of pixels I have and would rather not have my bitmap stretched/shrunk, so knowing how many pixels I have is what I need. Anyone know how to do this?
 
Here's what I am going to use and I think this is correct. I'm doing this in the OnUpdate method of my AppWidgetProvider and will save the information to be used when I periodically update my widget contents.

Code:
Display display;
display = ((WindowManager)
    context.getSystemService(context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
		
// cell size of a widget in portrait mode
int xCellSize = 80;
int yCellSize = 100;

if (metrics.widthPixels > metrics.heightPixels)
{
    // this means we're in landscape mode, so the cell sizes are adjusted
    xCellSize = 106;
    yCellSize = 74;
}
		
int xWidgetSize = 4;
int yWidgetSize = 1;

Log.v("debug", 
    String.format("your %d x %d appwidget is %d x %d physical pixels.",
    xWidgetSize, yWidgetSize,
    (int)(xCellSize * xWidgetSize * metrics.density),
    (int)(yCellSize * yWidgetSize * metrics.density)));

There's only one value for density, so even though aspect ratios can be slightly different on the screen, I'm assuming this will still be correct.

edit: doesn't seem like the INDENT tags are working, so sorry about the formatting.

edit: thanks to KlaymenDK for the code tag tip for formatting.
 
Upvote 0
:( No help from me on this topic, sorry.

edit: doesn't seem like the INDENT tags are working, so sorry about the formatting.
:) But a hint that you can use the "Code" (#) tag to preserve white space:
Code:
if (metrics.widthPixels > metrics.heightPixels)
{
    // this means we're in landscape mode, so the cell sizes are adjusted
    xCellSize = 106;
    yCellSize = 74;
}
Note: I indented the above with TAB characters, they get converted to spaces.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones