I'm trying to create an appwidget that has a standard size of 4x2 tiles.
Currently, I'm using these values:
android:minHeight="146dp"
android:minWidth="294dp"
On my Desire running Gingerbread, the widget shows as 4x2.
On my Asus TF101 tablet running ICS, the widget gets a size of 4x2.
Finally, in an emulator 720x1280 running ICS, the widget gets a size of 4x3.
So I'm a bit confused here. What are the correct values (perhaps using different resource folders) to achieve a 4x2 widget on all devices?
I've found that working on approx. 72dp x 72dp per 1x1 works best. So for a 2x2 widget I would normally use 144dp x 144dp, however google recommends 110dp x 110dp.
This page from Google gives a better description of what size to use and how to calculate it:
http://developer.android.com/guide/practices/ui_guidelines/widget_design.html
According to google guidelines you should use this formula : 70 × n − 30
For a 4*2 widget that means you should use 250dp * 110dp.
More infos : http://developer.android.com/guide/practices/ui_guidelines/widget_design.html
Related
I'm working for build the UI of my team's App.
In the beginning, my partner designs UI using Sketch 3.
According to the Android API guide for development Supporting Multiple Screens we known that 1dp = 1px in mdpi.
So we plan to design a basic standard UI in mdpi density. Other density would be automatically fit the position or length if we use dp as our length unit.
The basic UI resolution for designing canvas we use in Sketch is 360 x 640. We've read some articles, some articles said the mdpi resolution for designing standard is 360 x 640, and others said is 480 x 320.
It's really confused. We finally choose 360 x 640, because 480 x 320 is 4:3 aspect ratio not so many Android devices use it.
After we've done our UI, I'm trying to apply it to our app. But I've met some problems with positioning element. For example, our splash page.
Here is our splash page, I can hold Option/Alt to get the distance between edge to out element in Sketch 3. Because the UI sample file was made in mdpi resolution, I can use the length directly.(1px = 1dp in mdpi)
Splash Sample Image
CoolLogo is a ImageView. the whole layout is an RelativeLayout.
I'm using android:layout_centerHorizontal="true" to make ImageView in the center of Layout.
and android:layout_marginTop="268dp" to make it right position according to our UI sample file.
Here is whole layout xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#style/LaunchPageLayout">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/coollogo"
android:layout_marginTop="268dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
Preview on the Nexus 5(5", xxdpi, 1080 x 1920) looks fine. But the ImageView position seems to be incorrect a little bit.
When switch to smaller device such as Nexus One(3.7", hdpi, 480 x 800), the position is totally wrong. it should be in more upside position.
Nexus One Preview Sample Image in Android Studio
I'm confused. How can we get the right position values when we put our element into layout? Doesn't it means we just only to design a UI in mdpi it should fit all density? but if that is ture, why the layout on small devices is wrong? because our sample UI is design in mdpi resolution. I'm totally muddled.
might somebody give me some advices or some best practice to bulid UI?
Thank you.
According to guideline,
Google is not recommend to use absolutely pixel unit to design UI.
We use the method below to design our application in the end.
Share to everybody who started to design Android UI.
Basically, we need an mdpi size canvas, the canvas size is 320px * 480px
We use this resolution as our standard resolution,
including measuring length and size.
And if you ever heard about the design of iOS UI (or macOS).
You'll know that iOS often use 320px * 480px as standard x1 resolution.
When export resources, there will be x1 for standard resolution and x2 for Retina resolution.
Because the Retina resolution is two times of standard resolution.
Just like that, if we use standard resolution, Android will help us to fit on machines with different resolutions.
so we got:
mdpi => x1 (the standard)
hdpi => x1.5
xhdpi => x2
xxhdpi => x3
Theoretically, if we follow standard resolution, we can build layout for any other resolution.
By the way, We can set different dimen values as well.
it will help the layout to be more accurate in visually.
Maybe the best way to build one layout to fit all kind of resolution currently is to use ConstraintLayout.
but this kind of approach sometimes doesn't make sense.
Anyway, designing UI for phone and tablet (or even phablet) always be a good user experience.
I am trying to emulate a Huawei Y530. According to this site, it has the following specifications:
Screen: 480 x 854 pixels, 4.5 inches (~218 ppi pixel density)
This is what my WEB APP looks like on the actual device:
And this is what it looks like on a emulator, with the following specification:
Target: Android 4.3 - API Level 18
Device: 4" WVGA (Nexus S) (480x800 hdpi)
I know there is a difference in the height of the two devices, but still I believe that the emulator should show something similar to what the actual device is showing.
I tried to find the innerWidth and innerHeight of the real device with the following code:
var viewportwidth;
var viewportheight;
// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
if (typeof window.innerWidth != 'undefined')
{
viewportwidth = window.innerWidth,
viewportheight = window.innerHeight
}
alert (viewportwidth);
alert(viewportheight);
The result is width: 320px and height: 544px
The innerWidth on the emualtor is: 480px and the innerHeight: 756px
Since the real device had a width of 320px, i tried using an emulator with the following specs:
Screen: 3.2" QVGA (ADP2) (320 x 480 mdpi)
With the following result:
You can always create a new emulator if the app will be developed only for this particular phone. The image attached emulates the exact properties of the Huawei Ascend Y530 phone.
Alternatively, if the app is being developed for different phone having different screen sizes, I would take a look at the xml file for the layout. Take note of the following attributes, like placing form widgets in relation to other widgets. For example: android:layout_toRightOf="#+id/ukFlag" for the Danish flag. There are actually several layout parameters you can paly around with until you get the right layout:
Layout_toLeftOf
Layout_toRightOf
Layout_above
Layout_below
You can also setup the positioning of the widget, to be either left_based, right_based or centred.
Hope this would help you better: Android Layouts
Without knowing too much in how you have done your application, it seems you are using fixed units like pixels to specify size. As this project is a web app, maybe you should consider to use what is called a Responsive Design approach. In that way your content can adjust to whatever size the screen is.
I'll let you a couple of useful articles to learn more about this:
Responsive Web Design in Wikipedia
Responsive Design Presentation
Responsive Design Examples
I've written a widget which is supposed to be 4x2 homescreen blocks. It's minheight and minwidth, with accordance to the current "App Widget Design Guidelines" page are
android:minWidth="250dp"
android:minHeight="110dp"
And a padding is applied from dimens.xml if the system version is below Ice Cream Sandwich
On my Nexus 4 (xhdpi, Jellybean 4.2.1), this widget is 4x2. On an emulator (hdpi, Gingerbread 2.3.3), this widget is 4x2. On my Nexus 7 however (hdpi, Jellybean 4.2.1), the widget is 3x2. I can't for the life of me figure out why or how to fix it. Any thoughts?
Lars Vogel writes in this Home Screen Widget tutorial that:
As a calculation rule you should define the size of the widget with the formula: ((Number of columns / rows)* 74) - 2. These are device independent pixels and the -2 is used to avoid rounding issues.
Edit: apparently Vogel got it from the old Widget API documentation (scroll down past mid page to see reference).
Based on this measurement, for a 4 cells width and 2 cells height, use 294 x 146 dip:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dip" android:minHeight="146dip"
android:updatePeriodMillis="0" android:initialLayout="#layout/some_layout" />
Users of my Android app report that the included app widget is too big.
I've used this formula:
width/height = (number-of-cells * 74) - 2
So I got 294dp width and 72dp height for a 4x1 widget. But, as I've said, users report that the widget takes more place than 4x1.
How can this happen? Does it differ with different Android versions?
Edit: I've set this as the minimum width/height and the resize width/height. My layout file for this appwidget uses fill_parent so it should not extend that size.
This documentation page (relevant for Android 4.x, I suppose) shows that the formula is:
width/height = `70 * n - 30`
... where n is the number of cells taken. It also mentions that...
When your widget is added, it will be stretched to occupy the minimum
number of cells, horizontally and vertically, required to satisfy its
minWidth and minHeight constraints
Based on the dimensions given in the question, your application would stretch to 5 cells (320dp; 250dp is not enough) width-wise and 2 cells (110dp; 40dp is not enough) height-wise.
To support different widgets for different Android versions is quite simple.
For supporting Android version >= 4.0, you have to put your widgets stuff in the right folder.
/res/xml-v14
/res/layout-v14 [optional]
-v14 means the API-Level 14 (Android 4.0). Android version higher or equals 4.0 will use the files in these folders. Don't forget to calculate android:minWidth and android:minHeight using the correct formula:
cell_size in dp = 70 × n − 30
You can leave your working widget in the existing folders:
/res/xml
/res/layout
Using the correct forumla:
cell_size in dp = 74 × n - 2
To keep it simple and reuse the calculated dimensions you can also create the correct dimensions in the correct folder and reference to the size.
Create a file /res/values/widget_size.xml containing the dimensions for Android version < 4.0
<resources>
<dimen name="cell_size_1">72dp</dimen>
<dimen name="cell_size_2">146dp</dimen>
</resources>
Create a file /res/values-v14/widget_size.xml containing the dimensions for Android version >= 4.0
<resources>
<dimen name="cell_size_1">40dp</dimen>
<dimen name="cell_size_2">110dp</dimen>
</resources>
Reference the size of the widget to the new created dimensions.
android:minWidth="#dimen/cell_size_2"
android:minHeight="#dimen/cell_size_1"
Unfortunately this does not work on my Galaxy Tab 7.7 running Android 4.04. A 3x1 widget is 2x1, although it shows the correct size on the emulator. I assume it is because of
While the width and height of a cell—as well as the amount of
automatic margins applied to widgets—may vary across devices, you can
use the table below to roughly estimate your widget's minimum
dimensions, given the desired number of occupied grid cells:
stated at App Widget Design Guidelines
I create an 4x4 appwidget with minWidth and minHeight set to "250dp" as it is suggested in
App Widget Design Guidelines:
http://developer.android.com/guide/practices/ui_guidelines/widget_design.html
It is ok on every phones and phone-sized emulator I have tried but if I test it on a tablet (Xoom or Nexus 7) or on an emulator (resolution: 1280x752 with dpi 160)
then the launcher detects it as 3x3 widget.
What am I doing wrong?
Thanks,
Tamas
Try grid size = (74*n) - 2 for older api level. This value come from old document.