Search code examples
androidandroid-layoutandroid-fragmentsandroid-linearlayout

How to show assets bigger on high density phones


So If its in DP, It should scale itself according to the different screen sizes, but that's not the case when i define fixed width and height to an image,it opens it slightly smaller on high res phones, and slightly bigger on low res phones.

I don't know that either this is an expected behavior or i am missing something here.

I am testing on Samsung s5 and Samsung S7 Edge

  • Samsung s5 is 432 ppi pixel density
  • Samsung s7 edge is 534 ppi

I have added a single image in my layout for testing. I have added 4 different sizes drawables for it.

<ImageView
    android:layout_width="180dp"
    android:layout_height="200dp"
    android:src="@drawable/testimage"
    />

enter image description here

Now when i open the same project on s5 and s7 edge, the output is little different. It would be understandable if it was appearing larger on high res phone, but on s7 edge, the image is looking smaller than s5.

Kindly guide me that is it a normal behavior or i am missing something here.


Solution

  • Measuring something in dp means it will be the same physical size, not the same percentage of the screen size, so this is entirely expected.

    You can certainly use the Percent Support Library (see the details on this Google+ post to make views a set percentage of the layout's width or height, but you should probably only do that if you use vector drawables that will scale appropriately (otherwise, it will be blurry at some level). Generally you'd want to use the additional space on devices to show more content.

    <android.support.percent.PercentRelativeLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
      <ImageView
         app:layout_widthPercent="50%"
         app:layout_heightPercent="50%"
         android:src="@drawable/testimage" />
    </android.support.percent.PercentRelativeLayout>