Android: Widget takes too much place - android

I faced with a strange problem with my widget size.
I decided to develop one simple widget with supporting of ldpi, mdpi, large-mdpi and hdpi screens.
At first I created xml/widget.xml where I defined the following:
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="146dip"
android:minWidth="220dip"
android:updatePeriodMillis="0"
android:initialLayout="#layout/widget"/>
So I expect that my widget will take 3x2 cells for all supported screens.
Then I designed several layouts, one for each screen type - ldpi, mdpi, large-mdpi and hdpi - where:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Widget"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
...
</RelativeLayout>
So I expect that the widget size will fit 3x2 cells for all screens, not more, not less.
But currently when I run it on emulator (and real device too) I see that my widget takes
more than a half of a screen for all supporting screens - 4x3 cells if
to talk that the max size is 4x4 cells (as it described here:
http://developer.android.com/guide/practices/ui_guidelines/widget_design.html#sizes).
Even if I'll define exact layout_width and layout_height in my widget
layouts in dip it will still take 4x3 cells, just it's visible part
will have another size.

Well, looks like emulator not always refresh my app and that is why I saw my old dimensions instead of new. My friend also said that emulator usually doesn't update apps if using startActivityForResult. So in that case it is better to restart widget manually. Hope that will be helpfull for someone else :)

From http://developer.android.com/guide/topics/appwidgets/index.html
Because the Home screen's layout orientation (and thus, the cell sizes) can change, as a rule of thumb, you should assume the worst-case cell size of 74 pixels for the height and width of a cell. However, you must subtract 2 from the final dimension to account for any integer rounding errors that occur in the pixel count. To find your minimum width and height in density-independent pixels (dp), use this formula:
(number of cells * 74) - 2

Related

UI design implementation for multiple screens

I am in android development for last 6 months, recently got a design from a client. I implemented the design and he said this is not exactly look like what the designer has done. Then again he gave me the design with following specifications
my question is - is this right to add the spacing as he has given will this be proper in all devices else how can I explain him what he says is wrong
Please correct me am I wrong or he is wrong
As other users told you, px is not a relative dimension. As an example, we suppose a mobile phone with 5x10 cm screen dimensions, the model has hdpi resolution (480x854 px). For this device, a horizontal margin of 100px takes up more than 20% of the screen width. But, we also have another phone with the same screen dimensions (5x10 cm), but this one is more expensive and it has higher resolution (xxhdpi -> 1080x1920 px). For this case, a horizontal margin of 100 px will not take up more than 10% of the screen width. Therefore, it is needed a relative dimension as dp.
A possible way to deal with your designer could be decide a device to do the designs (a common one). This is a useful link with some devices and their configurations: https://material.io/tools/devices/ . I could recommend you to design using a 720x1080 px (xhdpi) one as MotoG or Nexus 4. For this case, you only need to divide by 2 the px value that he provides you to get each dp value.
You should take in account 3 Things:
Px - dp proportions:
Read this answer : https://stackoverflow.com/a/2025541/5884956 (actually the whole post is useful)
Percentage doesn't work for you:
What you improve by using dp instead of px is that for screens with same
aspect ratio and different pixel density, the layout looks exactly the same (dp-density independent pixel). It means everything inside the screen will look with the same proportions (buttons, imageviews, margins... etc).
Then, you would imagine all android phones have the same aspect ratio (16:9 for example). Which means that for every 16 dp of high, they have 9 dp of width , or in other words if you divide the height of the phone between the width of the phone will give you the same solution as 16/8.
But this is way far from the reality. The most common one is 16:9 but some phones like "Samsung Galaxy S8" have 18,5:9 and there are a lot more aspect ratios: https://material.io/tools/devices/. If you work with %, the time you see your design in a different aspect ratio, your items will look stretched. And this is not the desired.
Size does matter
By this time you should be convinced that dp is the best solution, if not, someone pease improve my answer. Buuuuuut, as the screen size vary, the number of dp also does. So for a phone of bigger dimensions (even for same aspect ratio) we have more dp. Then you should work knowing that some parts are going to be stretched (it should be the spaces between items and margins).
Ideally (Android Studio should do all this work for you) working with same aspect ratio , % of dp should be the mesure and when changing the aspect ratio
this changes must be reflected in spaces between views. But this is a hard work.
TL;DR
Select a standard device that designers are going to use to design (use a common one).
Tell them the design is not going to look exactly the same for every device (unless you design a different layout for each).
They need to decide which parts of the design are going to be the
stretched (stretch spaces and margins please, not buttons or images).
Let them work with px, they may feel more comfortable (meanwhile you must convert it to dp and work with dp). Also if you choose an 'mdpi' device dp = px.
Look at the layouts that allows you work better with this kind of the
designs ( I feel comfortable with ConstraintLayouts, but by working you will notice that this doesn't fit for everything ), but this is up to you.
Extra Information
https://developer.android.com/training/multiscreen/screensizes

Android UI Design and Layout Adjustment with DP unit

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.

Android: proper way to size UI elements for different size screens - dp vs?

Basic question here - if I define a widget in the following manner:
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#android:color/blue"/>
and I look at the widget on two different devices - one of them with a large screen and one of them with a small screen - which of the following will happen?
a) The widget will appear the same "size" on both screens - i.e. if it takes up one inch of real space on the big screen, it will take up one inch of real space on the small one, even if it that means that it doesn't all fit in the visible screen area.
b) The widget will appear as one inch on the big screen, but as some fraction of an inch on the small screen. However the result will be relative - if the widget took up three quarters of the width of the large screen, it will take up exactly three quarters of the width of the small screen.
Now, assuming that your answer was a - how do I redefine the widget to make it b? Assuming your answer was b, how do I redefine the widget to make it a? Many thanks.
DP is "density independent pixel" which is by definiton equals 1px on a screen with a dpi (dot per inch) of 160. Every device has a different dpi, the cheaper ones usually have less, the more expensive ones more etc.
You can calculate "real" size based on the device's specification but to answer your question, 50dp will look different on different screens, and their "real size" will be also different.
You can read more about this in the Android docs:
http://developer.android.com/guide/practices/screens_support.html
Each widget must define a minWidth and minHeight, indicating the
minimum amount of space it should consume by default....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.
http://developer.android.com/guide/practices/ui_guidelines/widget_design.html

Android layouts size and resize

I am doing a lot of work about android layout I still can't create a layout working in every phone. I am not sure about best the way to create a layout so correct me if I am wrong . There are three things to keep in mind :
Screen px (resolution, for example 1080x1920 px)
Screen dimension "inches"
Screen density dp or also called dpi ( dp is a virtual resolution, it's correct?)
To draw a layout working in every phone (my app will works for phones) do I have to create a directory "layout-kindofdensitydpi for every screen density (layout-ldpi,layout-mdpi,layout-hdpi,layout-xhdpi,layout-xxhdpi,layout-xxxhdpi) and draw "manually" or do I have to do something else?
I did a test, I created these 6 directory and drew manually for every resolution. It requires a lot of time, also device with a resolution of 768x1280 my app doesn't respect what I drew, for example spaces aren't respected, the collocation of elements doesn't result correct and frame layout with ImageView inside isn't scaled.
What I have to do? In some Android books isn't mentioned that elements could not resize and usually them explain how to put some text or image without analyse resize in every device.
Thanks in advice
First you should find the appropriate layout type for your UI (RelativeLayout or LinearLayout). Sometimes using a good layout(or nested layouts) can make the UI look good on every screen. I prefer LinearLayout cuz I can simply set layout_weight for components.
Then set different sizes in dimens.xml file for different densities or screen size buckets. Like this:
And you can also use match_parent or wrap_content
Don't forget to set the screen orientation of your activity if it doesn't need to rotate. Having one orientation makes it much easier to design.
If you couldn't make a good layout using tips above you should create multiple layouts to fit every screen size or density (Screen size and density are two different things).
You should find the best way to determine your screens according to.
Screen size bucket (small, normal, large, and xlarge) picks a layout that fits the screen (or the closest), density bucket (ldpi, mdpi, hdpi, xhdpi, xxhdpi, and xxxhdpi) picks a layout according to the density ,or the smallest width (I think it's almost the same thing as density).
480dp is the sw of these two devices
You can make layouts for different screen orientations too.
I don't like creating layouts for different screens for some reason. One of them and the most important is that sometimes same screen sizes have different densities and it makes it hard and time consuming. To create a layout and you should also provide a lot of pictures.
Use
android:layout_width="match_parent"
android:layout_height="match_parent"
to the outer layout to get access of the full screen of the device.
Also, if there are no changes in your UI then you don't need to create different layout folders.
Refer http://developer.android.com/training/multiscreen/screensizes.html
You should have a look at
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/training/multiscreen/screensizes.html
http://developer.android.com/training/multiscreen/index.html
I would suggest using layout-swXXXdp or layout-large etc instead of layout-KindOfDensitydpi
Create your relative layouts using RelativeLayout and use the weights of LinearLayout. Don't hard code any of the positions in the layout

Home screen widget size for large screen or hdpi?

From Android widget screen guidelines, http://developer.android.com/guide/practices/ui_guidelines/widget_design.html, we know that, home screen has 4*4 cells, and in portrait orientation, each cell is 80 pixels wide by 100 pixels tall.
I think these are for baseline HVGA screen. How about for large screens and hdpi screens, do they still have 4*4 cells for widget and each cell in portrait orientation is still 80 pixels * 100 pixels?
Thanks.
The pixels you are referring to are device independent pixels. As you can see in the documentation here:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:updatePeriodMillis="86400000"
android:initialLayout="#layout/example_appwidget"
android:configure="com.example.android.ExampleAppWidgetConfigure" >
</appwidget-provider>
Note how they are using dp units, not px. This allows the widget to scale correctly to different screen resolutions.
As for the screen being 4x4 cells, this is a property of the default Android homescreen manager. Regardless of screen resolution the home screen will be 4x4 cells when using the default Android home screen.
There are other home screen managers out there that have more than 4x4 cells, such as the custom home screen manager on the Archos tablets. If you want your appwidget to work in some of these other homescreen managers that do special things like increase the number of cells on the screen or allow text input and scrolling in app widgets, then you are probably going to have to write a custom version of your widget for those home screen managers.
So the answer to your question, assuming you are always targeting the default home screen manager, is that the screen is always 4x4 cells and as long as you stick with device independent pixels you have nothing extra you need to do to make your appwidget scale and work correctly on higher resolutions.
When you use images, you have to put them with different dimensions into the related folders in your project-directory.
example:
- you use 100dip width for an imageview
- in folder "drawable-mdpi" you should put .png with 100px width.
- in folder "drawable-hdpi" .png with 150px width
- in folder "drawable-ldpi" .png with 75px width

Categories

Resources