0

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.

Onik
  • 19,396
  • 14
  • 68
  • 91
Amrmsmb
  • 1
  • 27
  • 104
  • 226

2 Answers2

0

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.

Tom James
  • 279
  • 3
  • 16
  • i dont have the xml righr now and anywy it is more or less a complex one...but i would like to tell you that yes there are some margins specified..is it a problem?? – Amrmsmb Dec 27 '16 at 20:36
  • If you have the top and bottom of your image aligned to the parent, and then sized by margins of say, 5dp. In both landscape and portrait mode the top and bottom edges will always end/start 5dp from the edges of the parent/screen. Landscape has a shorter vertical length than portrait does, so if you sized your image in this method - images in landscape would appear squashed, or, stretched in portrait. – Tom James Dec 27 '16 at 20:53
  • You could fix this by aligning the ImageViews position with only one vertical, and/or one horizontal attribute; then size the image by using wrap content or specific dp sizes? – Tom James Dec 27 '16 at 20:56
0

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.

Community
  • 1
  • 1
Lyla
  • 2,767
  • 1
  • 21
  • 23