Getting my head around Android, I've sort of trouble understanding support for multiple
devices. I've gone through the dev-docu, now for the clear-up:
The idea behind using dp is, that e.g. a button has the same 'real-world'-size, independent of the actual screen-size, right?
If so, that means, I provide the images in ldpi, hdpi and so on. That results in a button, that has always the same size.
If still correct, I understand that now, that my buttons always has the same size, I should provide different layouts, because on a small device, there might only be space for two next to one another, where on a larger device there's room for three.
If all of the above is correct, my question would be, how I deal graphics-tool-wise with it. Say I have a button 100px x 100px at 300dpi, what sizes and dpi's would that be for the new documents in e.g. Gimp for the different screensizes?
Thanx in advance for any smoke-lifting!
Marcus
What it means when you can use your custom assets for different screen densities is that when you put those respective assets, they will not scale again and use them as they are. Now it is true that the size of the screen also varies. That is when you can consider tweaking your layout.
For example in a large screen you may be able to fit two components beside each other and use a linear layout with horizontal alignment. But when it comes to a small screen, that might not be possible and one option is to define a new layout for small screens and say pt those two components in a vertical layout.
For more information regarding how to implement this and best practices, check these Android Docs
After some more study, it seems that the graphocs-tools DPI are actually the one referred to in e.g. the baseline. So to create a baseline-doc, you indeed create a document 470 x 320 pixel in size and a resolution of 160 dpi. The rest goes from there.
Related
I have been searching for information to define dimens.xml for every different screen size so that I can have different values for different screen sizes. I have seen that is really difficult because I would have to make a different dimens file for every different screen size. While searching for a possible solution to this, I have found this library: https://github.com/intuit/sdp
Is that library the best solution nowadays to support all the different android devices according to the screen size? I would appreciate other alternatives if using "scalable dp" library is not a good option.
In my case, the purpose of having different values according to the screen size is because I have a ConstraintLayout with some items that have Top and Bottom margin. To let you understand, imagine that both Top and Bottom margin is 10dp. I can see so much difference of margin between device 1: 480x800px (4" and screen density= hdpi) and device 2: 480x800px(3.7" and screen density= hdpi). As you can see both devices have same screen density and same px, so I have to use some other attribute or tool to distinguish that different screen size...
In my activity, I use DisplayMetrics to dynamically get the pixel height and width of the screen, and then I assign each of the components in the Activity sizes based on those dimensions. I wanted to know how this could be affected by screens that have different densities? Is it a good idea to use pixels?
Edit:
The purpose of using pixels dynamically is so that my layout scales based on the given screen. I just want to know how density will play into this. For example, if I have two screens with a height of 1024px and width of 800px, but one is twice as dense as the other, and I want to use 40% of the height and 40% of the width (this is just hypothetical) for a button, why should the density matter? This will just mean that the size of the button will have more pixels in the higher density screen, but the physical size of the button will be the same as DisplayMetrics will always give me the absolute size in pixels. Or am I wrong about this?
See this question and its answers. You will get the answer to your question.
Edit:
From one of the answers on the mentioned question
If you are any serious about developing an Android app for more than one type of device, you should have read the screens support development document at least once. In addition to that it is always a good thing to know the actual number of active devices that have a particular screen configuration.
Screen Sizes and Densities
To help in your case it is proposed to use dp units instead of pixels, but still there will be differences from one device to another.
On a tablet screen with a high pixel density, the elements probably will occupy less relative space.
If you want to improve it more then you will have to do the dimensions calculation by your own.
Or use a layout that auto distributes the space, for example the LinearLayout
Also you have to take into account that it is the system that decides the size of some widgets, for example the standard buttons
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
Android developers site,and most of you,Push everybody to use dp units on their layouts,
I can understand this approach if you using different layout for different densities,but when my requires is one layout.xml ,and one drawable folder,it is make no sense to me to use it,
because it is NOT preserve proportion.
It seems weird to me that there is no simple way to preserve proportion for all devices.
Just help you to understand my point.
1.make any layout with view inside it
2.set the sizes on dp as Android advise you to do
3.and then check it on the eclipse graphical view of your XML.
It looks way way different between different size devices.
Button with width of 15 dp look like 10 times bigger in 640*480 devices then Tablets.
So what's the point?
I understand all the math of conversion between dp and pixel with density,
but i looking for simple way to preserver the proportion on my layout,without define others for others densities.
The only way that i find to help is using android:weightsum,
But it can not use for margins and other settings.
Any idea?
You can always use your values folder and implements the exact dp size for each of different device densities, so you wont create different layout on different density.
Tablet and handheld devices will never have the same sizes that is where the different layout folders are created to let the developers design on different devices.
I would recommend a different layout design on tablet and handheld devices which most apps applies.
Think of dp units as referring to actual size (like cm and inches). So something that is 250dp will look (for example) about 1 inch on a phone, but also roughly 1 inch on a tablet. No matter the size and resolution of the device, it will always be about 1 inch.
Why is it done this way? Well basically it is the Android creative vision that you won't simply scale your app from a handheld screen to a tablet sized screen. This is why they don't allow you to specify heights and widths as % of screen size. The one small exception is layout_weight in LinearLayout.
You may agree or disagree with their vision, but that's the reality.
My layout as shown below looks very different depending on what screen size it is projected on to. I'm aware I can improve this somewhat using different layouts for each screen size but considering I have followed the best practises described in the android multiple screen support documentation (using dps, no absolute layouts etc) I wasn't expecting the results to look this bad so I fear there is a further underlying problem.
Code:
http://pastebin.com/D96ue9sc
Your layout is fine and completely as I would expect it. You shouldn't mix up density independent pixels with fully dynamically layouts.
DP just means that the value is calculated according the density types. The density itself has nothing to do with screen resolutions. 60dp are 60px on a mdpi device, it doesn't matter if the screen is full HD or just 480x360. But the result is, of course, very different because the calculated 60px are nothing on a HD screen but a lot on a small one.
You have not other possibility but to provide different layouts according to the screen size/resolution.