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


Last Updated:

  1. tdieckman

    tdieckman Well-Known Member This Topic's Starter

    Joined:
    Sep 30, 2009
    Messages:
    49
    Likes Received:
    5
    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?
     

    Advertisement
  2. tdieckman

    tdieckman Well-Known Member This Topic's Starter

    Joined:
    Sep 30, 2009
    Messages:
    49
    Likes Received:
    5
    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 (Text):
    1.  
    2. Display display;
    3. display = ((WindowManager)
    4.     context.getSystemService(context.WINDOW_SERVICE)).getDefaultDisplay();
    5. DisplayMetrics metrics = new DisplayMetrics();
    6. display.getMetrics(metrics);
    7.        
    8. // cell size of a widget in portrait mode
    9. int xCellSize = 80;
    10. int yCellSize = 100;
    11.  
    12. if (metrics.widthPixels > metrics.heightPixels)
    13. {
    14.     // this means we're in landscape mode, so the cell sizes are adjusted
    15.     xCellSize = 106;
    16.     yCellSize = 74;
    17. }
    18.        
    19. int xWidgetSize = 4;
    20. int yWidgetSize = 1;
    21.  
    22. Log.v("debug",
    23.     String.format("your %d x %d appwidget is %d x %d physical pixels.",
    24.     xWidgetSize, yWidgetSize,
    25.     (int)(xCellSize * xWidgetSize * metrics.density),
    26.     (int)(yCellSize * yWidgetSize * metrics.density)));
    27.  
    28.  
    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.
     
  3. KlaymenDK

    KlaymenDK Well-Known Member

    Joined:
    May 29, 2009
    Messages:
    1,217
    Likes Received:
    130
    :( No help from me on this topic, sorry.

    :) But a hint that you can use the "Code" (#) tag to preserve white space:
    Code (Text):
    1. if (metrics.widthPixels > metrics.heightPixels)
    2. {
    3.     // this means we're in landscape mode, so the cell sizes are adjusted
    4.     xCellSize = 106;
    5.     yCellSize = 74;
    6. }
    Note: I indented the above with TAB characters, they get converted to spaces.
     

Share This Page

Loading...