I have a working widget that uses layouts and RemoteViews and scales itself nicely to whatever home screen area assigned to it. However, I need to display custom fonts and thus must re implement using an explicit bitmap and here is where the problems start, computing the bitmap size ended up to be a tough problem.
What is a good formula to compute the widget bitmap size in pixels as a function of these
values (and anything else that is available and is useful):
Number of home screen rows (R) and columns (C) allocated to the widget. (I derive min/max value in the widget_info.xml from these values).
Display metrics (screen size, DPI, density (D), etc)
Current orientation (O)
Android version (V)
The goal is not just to find a safe size but also not wasting screen real estate.
Just call getWidth and getHeight on the parent layout of your Widget. After the widget has been drawn once these values will be populated and you'll know the exact size of the widget.
Related
I am working on a widget, where I need to have a chart displayed in addition to some text fields. I have figured out how to generate a visualization of the graph and then convert it to a bitmap, which gets placed in the ImageView. The problem is the following:
In a normal Android app, I would get the width and height of the ImageView and pass these as parameters for the bitmap generation. This way, the image will be fitting perfectly within its container. Since I am using a home screen widget, this is not possible, as far as I am aware, though. As a result of this, I am trying to derive the size of the ImageView by trying to figure out what size would the text fields have, but I also have to calculate those and so far have had no luck with getting it right. What I end up with is always having excess space above and beneath the image, or on its sides and the size of this is really varying according to the screen the widget is presented on. You can see some examples in the two links below.
Example A This is the default widget size. For this screen, only a small gap can be seen on the sides of the graph.
Example B Resizing the widget and recalculating the size leads to having bigger gaps on the two sides, though. For other screens the issue can be even more jarring.
My question is:
How could I get/calculate the necessary resolution for my bitmap?
This might be a dumb question but I'm using unity to build a double pendulum app and because not all screen sizes are the same the UI buttons get moved around and don't scale to match the phones screen size. I attached a few images to show you what I mean.
Notice how in the third image, the return button overlaps the other UI sliders. I think the issue is that the pixel distance from the top of the screen doesn't change given new orientation. Is there a way to keep the spacing proportional?
On your canvas object, set it to Scale With Screen Size then, set the Match property to 0.5. This way the canvas will scale with both width and screen height.
If you need more info about Designing UI for Multiple Resolutions, check here
Well in your parent cancas object in the inspector,the 2end setting there a a drop down. Click it than a magical setting scale with screen wil be ther.👍
This is on Jelly Bean, and so the widget can be resizable by the user. The only thing in my widget is an Image View. I am creating a bitmap whenever the widget updates which I set into the Image View itself via remote views. The bitmap created and drawn changes depending on the size of the widget, and the space taken up by it.
The problem I have is in determining what size to create the bitmap which I understands must use pixels. Is there a way to find this out from the widget provider? Either the space available in pixels or dips will be fine as I should be able to work it out from one to the other.
The size in dip of a widget can be calculated using the following formula: 70 × n − 30 where n is the number of cells in either width or height.
You can convert dp values to pixels depending on the screen density.
Read more at: http://developer.android.com/guide/practices/ui_guidelines/widget_design.html
I have recently noticed that layers can be resized when needed, so I'm trying to implement a method to resize the main layer to the size of the android screen where it is running.
How do I access to the device screen size from playn?
And there is another way to adjust the size of the game in android devices depending on the size of the screen with playn?
I believe what you are looking for is PlayN.graphics().screenHeight() and PlayN.graphics().screenWidth().
Copied out of the Graphics.java file:
screenWidth() - Gets the width of the available screen real-estate, in pixels.
screenHeight() - Gets the height of the available screen real-estate, in pixels.
width() - Gets the width of the drawable surface, in pixels.
height() - Gets the height of the drawable surface, in pixels.
As the descriptions state, screenHeight() and screenWidth() provide the actual screen height/width. With these values, you can adjust the size or scale of your game to fit the screen.
PlayN.graphics().width() and PlayN.graphics().height() give you the view size that you requested (via PlayN.graphics().setSize()), not the maximum available screen size.
To obtain the maximum available screen size, use PlayN.graphics().availableWidth() and PlayN.graphics().availableHeight().
Have you tried this?
This gives you the available device rendering surface.
PlayN.graphics().width()
PlayN.graphics().height()
Or I missunderstood your problem?
I have built an appwidget with an square layout, and so it doesn't fit exactly in the standard widget sizes as recommended in http://developer.android.com/guide/practices/ui_guidelines/widget_design.html#sizes.
I chose a 3x2 size (android:minWidth="220dip" android:minHeight="146dip") as it is the smalllest that covers the widget's layout.
In http://developer.android.com/guide/practices/ui_guidelines/widget_design.html#design Google recommends:
"All widgets must fit within the bounding box of one of the six supported widget sizes, or better yet, within a pair of portrait and landscape orientation sizes, so your widget looks good when the user switches screen orientations"
My widget looks good in portrait mode. When switched to landscape mode (in the emulator) the layout is clipped. I tried inverting the minWidth and minHeight values in the provider's XML and then it looked perfect in landscape mode but clipped in portrait mode. Setting the size to 3x3 solves the problem, but then the widgets takes a lot of unnecessary space.
I know I can define different layouts in res/layout and res/layout-land, but in this case the layouts are not different at all, in both modes I want the widget to look square.
What I would need is something like 'xml' and 'xml-land', AFAIK this is not supported in Android.
Ideas?
What I would need is something like 'xml' and 'xml-land', AFAIK this is not supported in Android.
It is supported. All resource set qualifiers (e.g., -land) are supported for all resource types.
Whether it will help you is another matter entirely, as I am not aware that you can change actual app widget size on the fly this way.
Setting the size to 3x3 solves the problem, but then the widgets takes a lot of unnecessary space.
You are the one who is trying to force a particular pixel size (or, at least, aspect ratio). This will be fragile, as you are discovering. Furthermore, app widget cells are not guaranteed to be the same size on all devices and home screen implementations.
Hence, you are either going to need to choose an app widget size that gives you tons of extra space (your 3x3 scenario), or design a fluid app widget layout that adapts to the actual size that you are given (and therefore will not be square). Personally, I recommend the latter.