Maintain the same view size on different screen orientation - android

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.

Related

Android: text cropped in some devices

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.

button position multi screen

My problem is very simple.
I have a button that have 100dp marginRight and 100dp marginTop in a xxhdpi density screen.
The problem is, when I change to another density screen or size, the button its not in position I want. It remains the 100dp margin top and right but I don’t won’t this.
I thought that if I use dp, when I change screen it’s was going to be in the position I want, calculate de position in smaller or bigger screens.
I hope you understand, I don’t want to create a layout for every density or size screen.
How can position a button that be in the same position in every screen?
on a different screen density that 100dp will change position.
what you can do is support multiple size and create different layout for each.
read http://developer.android.com/guide/practices/screens_support.html.
Using the new size qualifiers is a solution.

Wrap content vs setting dp

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.

How can I increase the size of the images within my ImageButtons?

In a nutshell I'm looking for my dotted box image to fill the view. I had 60x60 image Buttons in my medium screen layout. In my large screen layout my buttons have appeared tiny so I've increased them to 100x100. The images within these buttons will not stretch. Is it possible to stretch these images or replace them with bigger ones?
I know people don't like this because it renders slower, but I think LinearLayout is going to be the way to go. When you use RelativeLayout, two devices with XHDPI or LDPI may still render images differently due to difference in screen size/resolution.
Using LinearLayout will force the images to be the same size relative to the screen. The one caveat is screens will vary from 1.5:1 to 1.77:1 (personal experience, might be a little off), stretching some images about 10%.
This combined with specifiying your own dimensions rather than wrap_content does the job.
android:scaleType="fitXY"

XML for all screens

I have a layout with 8 buttons 2 image views and 2 text views. On my device (Galaxy S) it fits but on devices with smaller screen the bottom side is not appearing.How could I adapt my layout for all screens?
if you want your app to support all kind of screen sizes,
you need to create different layout xml file for each kind of the screen types.
place each one of the above on resource folder:
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
on run time - android will automatically choose the right resource according to the device's screen type
you can read more about it on -
http://developer.android.com/guide/practices/screens_support.html
use RelativeLayouts or LinearLayouts with layout_weight property. If you can post some xml, i would give you more suggestions.
The problem occurs generally with the ImageViews mostly, as they occupy more space on smaller screen, since smaller screen has lesser no.of pixels per inch. It's a general practice to restrict the sizes in dp.
You can define more layouts with different width and height.Then check the height and width and density of device and use that specific layout for that device.
Use screen dependent pixels dp and sp. Design a different layout for different screen sizes. http://developer.android.com/guide/practices/screens_support.html <-- refer here
For most width and sometimes height problems you can use layout_weight to assign the exact ratio catered for each View.
If there is no other choice and that your layout have to be that way you might want to consider using a ScrollView to wrap your parent layout so that they can be scrollable. http://developer.android.com/reference/android/widget/ScrollView.html <-- refer here.
Good luck.

Categories

Resources