In my android app, there is a certain layout with a fixed height. In this layout, there are some TextView s arranged vertically. I have set the font size of text fields to be somewhat larger (18sp).
In almost all the devices I checked, The text fits inside the fixed sized layout. But in few devices, The bottom TextView gets cropped out of the layout.
If the proportion between text unit size (sp) and the layout unit height (dp) is the same in all devices, I wouldn't expect this to happen.
So, is this due to something wrong with the devices? Is there any way I can fix this?
Screen density is different than screen dimensions. dp normalizes densities across devices, but they can still have varying screen heights and widths. The obvious example is tablet vs phones, tablets have the same screen densities as phones but much larger screen dimensions. Just like that, some phones have smaller screens than others, so you need to take that into account when using fixed height layouts.
To get around this problem, you can take advantage of the width and smallest-width resource buckets and provide different layouts for smaller devices.
As it seems, the proportion between a unit sp and a unit dp depends on the font size setting (Settings->Display->Font size).
I could recreate the issue on other devices when I set the font size to be Huge.
So, it is not a good idea to set fixed sizes to layouts containing text.
Related
I have read Android guidelines regarding different screen sizes, but still I have some considerations.
Client has given me an image from PSD file which has certain resolution that fits
1080 X 1920. I just use wrap_content, and it perfectly fits the part
of screen.
I am not using DP to define its width-height, If i was using DP it would have
adjusted image according to screen sizes.
My questions are,
Does wrap_content works the same way as Density Pixels?
Is it also responsive, and changes the image width-height according
to different screens?
If not, then Is it necessary to use DP to support different screen
sizes ?
Thanks
The setting wrap_content tells your view to size itself to the dimensions required by its content. In the case of your test, your image is 1080x1920 and your device's screen resolution is likely 1080x1920 as well, hence the perfect fit. Since you set the width and height to wrap_content, Android is simply trying to use as much screen space as it needs to correctly display the amount of content it was supplied. In this case, since the available screen space matches the size of the content, it just fits perfectly.
But what if the device screen isn't 1080x1920? In that case, Android will only use as much space as it can, but still attempt to fit the image inside the bounds of the available screen space. In other words, the system will appropriately scale the image down to get it in the container you have provided for it. But this can lead to awkward fits if the aspect ratio isn't the same as the image. For instance, see this screenshot below:
This image is 1920x1080, but notice that it doesn't quite fit. That's because this nexus 7 screen is 1824x1200 when viewed in landscape. Additionally, the toolbar at the top of the screen is eating up available screenspace, making my viewable area even smaller and more awkwardly shaped. So while the system would love this image to extend all the way to the left and right borders, it can't, because then that would mean the height would be bigger than the viewable space. Since I used wrap_content to display this image, the system is using as much vertical space as it can, and the result is that the image doesn't quite fit the horizontal space.
So to more directly address your questions, yes wrap_content is a relative size setting that will make it easier to get a consistent look across multiple screen sizes, similar to using dp. But realize that there are hundreds, if not thousands of available Android devices on the market, and they all have varying screen sizes and densities. So your drawables may not always appear the way you want them on every device.
The way to overcome this is to supply multiple versions of your assets and provide alternate layout files for different screen sizes and densities. Once you do that, all you can do is test, test, and test some more. Use emulators for weird screen densities or devices you don't own, just to make sure you're getting the look you want. In the case of your 1920x1080 image, it looks great on that one device, but how will it fit a large tablet or a tiny handset that is smaller than the resolution of the image? These are situations you must account for in your design.
I suggest you read these resources, as they are hugely helpful in learning how to deal with issues resulting from varying screen sizes and densities:
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/training/multiscreen/screensizes.html
This DP measure is pretty confusing, I'm trying to learn when should I use wrap_content and when should I set the height, when using ImageView.
My current issue is that I'm using wrap content on 3 images (and yes I have different resources for each screen size: mdpi,hdpi,xhdpi) BUT in some devices (both hdpi) since they have different width pixel size (480px for one and 590px for the other), in the 480px one, one of the images looks smaller cause their size is calculated cause of the wrap_content.
I could make my designer re-make all the images for the hdpi size, but I want to know when to use wrap_content and when to set DP size myself.
DP is just a unit of measure that normalizes for different screen pixel densities, which means a value like 50dp always has the same physical size no matter what device you run your app on.
As far as actually designing your layouts, you should almost always use either wrap_content or match_parent instead of setting hard numbers for width and height. Exceptions usually come about when you use layout_weight for children of a LinearLayout for doing proportional sizes, or when using the various layout anchors for children of a RelativeLayout.
If I have an image that I want it to be medium on android medium screen and large on large screens. I know I have to put layouts in 2 different folders: medium and large layout.
But my question is, should I use one image (say the medium size) and stretch it for the alrge screen by putting min width, min hight and fitscaleXY.
OR
Should I create 2 images, one for medium sized screen and larger version for large screen?
Thanks
EDIT:
Really no answer yet to this? I am sure this is something people would do all the time. You want the image to be larger on large screens, how do I go about it? I was thinking of specifying width and hight for each screen layout size instead of wrap content.. You agree?
You dont have to make different layout xml files for each screen size, unless they really need a different layout, like how things are organised.
In Android you must specify the sizes in dp, and textsizes in sp, which is an equivalent to pixels, only they will be scaled automatically based on the screen density.
Measurement Units.
If you specify some size in dp, it may show up on a large device at for example 300 pixels, and on a smaller device 200 pixels (just an example).
I would Definatedly not hardcode the sizes per screen, since there are a lot of different screens around.
What is the best way to set the text size so it looks the same on all devices? The biggest problem I am having is setting the text size on the buttons so everything fits or isn't too small. I have tried setting the size in the xml, and I have just tried setting it dynamically by getting the screen size and messing with the screen width and height to set the size. I had tested different things on different devices and thought all was well, until I just tried my app on the Galaxy Nexus and half of my words were getting chopped off inside of the button. I made some adjustments and the font is now way too small on tablets.
Using the xml has worked fine for me before, but most of these new 7" tablets use the large layout, so my images and text are really small if I set the height, width, and text size with the xml. Setting the widths and hights dynamically have helped with the buttons and image sizes, but the font still doesn't look quite right.
Is my best bet just to find a happy medium via xml(large, x-large, etc.) or are there other ways?
Have you seen this article: Supporting Multiple Screens ?
It says:
you should prefer the sp (scale-independent pixel) to define text
sizes. The sp scale factor depends on a user setting and the system
scales the size the same as it does for dp.
The same issue is explained here, Different text size for different hardware
How wrap_content, fill_parent or the dp will guarantee that the view is given an appropriate size on the current device screen in android platform.
Only wrap_content and fill_parent will adapt to the size of the screen.
Sizes declared in dip will be adapted to the resolution of the screen, but not to the size (100dip will be the same physical length (for example 1 inch), so it won't take the same proportion of the screen on a small device and a large one.
Sizes declared in px will not be adapted at all.
So if you want to develop an application for different devices sizes and resolutions, i suggest you create layouts and drawable directories, as described in the framework.
wrap_content takes the actual size of the child....
fill_parent takes the size of the parent....
dp takes the size of what you given...
For the the difference between fill_parent and wrap_content, see the answers for this question
And about dp, see this documentation on how android support a range of screen sizes and resolutions
For the better understanding of layout objects, please have a look on below documentation pages
User Interface
Common Layout Objects