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.
Related
I have created a layout that consists of imageviews and textviews. When I run the App every thing is fine on portrait screen orientation, but when I rotate the device to be in lanscape the imageviews shrinks (smaller in size).
I want to have (regardless of the screen orientation) the same size of views. I do not want the views to look smaller or bigger, I want the same sizes across different screen orientations.
Note: All dimesnions in the layout are in dp for width and height and the text font it is in sp.
Are your ImageView's being sized by attributes that align to edges, and then margin distances? This would distort your image as the parent boundaries change on device orientation.
Also, please provide your xml code, I don't have the reputation to ask as a comment.
Android tries to help you create responsive layouts (layouts that change the position and size of elements depending on how large the device screen is) through the use of things like layout_weights, settinging the width/height to match_parent, etc. Because of this, if you use these attributes and then rotate the phone screen, the size of your images is going to change because the system will think that you want to text/images to change size depending on orientation or device screen size.
Even if you mix layout weights and hard coded dp pixel sizes this will happen - what happens is that android measures all of your hard coded values and wrap content values, and then for any extra room on the screen, it expands the views that have weight, proportional to the weight number you give them. This is why you'll sometimes see people setting layout_height="0dp" and then setting a weight.
If you want an image that is always the same size, you can hard-code a certain number of dp pixels and remove any mention of layout_weight or match_parent. You can hard-code margins too. Depending on the size of the image, this will mean if you view the image on a phone that's too small, part off it will end up off-screen. There are a variety of ways to deal with this depending on what you want to happen. For example, if you want to elements on the screen to stay the same size but rearrange themselves depending on the device's screen size, you can make multiple layouts for different screen sizes and use resource folder qualifiers.
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.
I always use match_parent or wrap_content in width and/or height and I work with margin and padding (depending I use 0dp on height when I use the weight of Linear Layout) to adjust my layout depending on my needs.
I read that it is the correct way to work if I am developing for any size of android screens.
Is there any situation that is better to use fixed size in the android layout?
When you have a specific hardware target that you'll run on, fixed sizes are easier to get everything to line up perfectly. If you want things to be a certain physical size (such as exactly 2 inches big) dp is the best way of doing things. There's a few use cases, but it should generally be avoided.
I found one example which suggests one such scenario :
The common setting for stretchable layouts is layout_width / layout_height set to match_parent. But when you want it to fit around another container, the Android sdk recommends you use 0dip instead of match_parent. I recommend using 0dip so it’s easier to differentiate partial layouts from full-size layouts.
But, according to android wiki you should avoid using fixed values.
In general, specifying a layout width and height using absolute units such as pixels is not recommended. Instead, using relative measurements such as density-independent pixel units (dp), wrap_content, or match_parent, is a better approach, because it helps ensure that your application will display properly across a variety of device screen sizes.
My question is just in the title.
I try to put an image to my layout, i tried this with a FrameLayout's background with wrap_content width and height and also tried with imageView with all of the possible fitScale properties.
I just cant see why my 720 px width image is HALF on the width of my phone's screen which is a samsung galaxy s3 with 720 px width....
My real question:
What is the best way to ensure that my pics in applicaion wont get any distortion?
First of all, you need to think in screen densities and dip values, not pixels. That means that you are dealing with a "virtual screen" where 1 dip (density independent pixel) represents a different amount of real screen pixels depending on the device.
For drawables you have the following categories for getting crisp results on any screen:
ldpi (Scale factor 0.75)
mdpi (Default / baseline: scale factor 1.0)
hdpi (1.5)
xhdpi (2.0)
Create different sized versions of your bitmaps based on these scale factors to get crisp results on any screen. You should start from xhdpi as highest resolution to avoid quality loss because of upscaling.
Put these different versions into their respective drawable folders (res/drawable-ldpi, res/drawable-mdpi...). These different versions of one and the same bitmap must bear the same file name of course, otherwise that won't work.
Second of all you shouldn't make strong assumptions about the device screen height and width. You need to layout your views and graphics so they dynamically make use of the whole screen not knowing the exact screen resolution. However, you can make weak assumptions about these screen resolutions based on the configuration qualifiers
small
normal
large
xlarge
Try to avoid fixed sizes for your views based on any assumptions. Always avoid "px" values in layout XML files. Use "dip" instead.
Read the documentation Supporting Multiple Screens for more information.
What you want to achieve sounds as if you want your image to take up the full width of the screen.
If you set the image as a background of any view (FrameLayout for example) then the displayed clip of the image depends on the size of that view.
If that view has layout_width / layout_height set to wrap_content then the size of it depends on the dimensions and arrangement of its child view(s).
In case of FrameLayout it depends on the size of that single child of FrameLayout.
If you're using ImageView the image will be scaled to fit into the size of the ImageView by default. The size of the ImageView depends on its layout_width / layout_height values. If you set these values to match_parent and if the ImageView has enough room "to grow" you should be able to see your image stretch all over the screen.
The "room to grow" depends on the ImageView's context in the layout. Does it have neighbors that take up room? Is the parent view too small (because of wrap_content for example)? Look for these possible problems.
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