4

In my project have custom view has fixed width.The width is specified in values->dimen

<dimen name="layout_width">75dp</dimen>

The issue is that in some device(eg:Pixel) with resolution 1080 X 1920 this view is Clipped. However some other devices(eg:Pixel2) with same resolution everything works fine.

Have tried using different dimension files as in this post .Both these device are picking from same dimen files

Code

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="21dp"
    android:gravity="center"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="55dp"
        android:layout_height="55dp"
       >

        <ImageView
            android:id="@+id/icon"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_centerInParent="true"
            tools:background="@drawable/icon" />
    </RelativeLayout>

    <TextView
        android:id="@+id/title"
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_marginTop="20dp"
        android:gravity="center"
        />
</LinearLayout>
Nikhil
  • 650
  • 1
  • 11
  • 28

2 Answers2

1

Inside your imageView, experiment with different scaleTypes to see if any fit your use case.

    <ImageView
        android:id="@+id/icon"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_centerInParent="true"
        android:scaleType="fitCenter"
        tools:background="@drawable/icon" />

Other acceptable scaleType values are FIT_XY, FIT_START, FIT_CENTER, FIT_END, CENTER, CENTER_CROP

sindhu_sp
  • 749
  • 5
  • 12
  • It works only for ImageView. I think @Nikhil was asking for a solution which works for different types of Views. – Sudip Feb 12 '18 at 19:41
1

This happens due to different screen dpi of devices. like normal 1080*1960 devices v/s nexus 5x (which has higher dpi with same resolution)

Another example can be of Samsung galaxy edge S6 (it has much higher dpi as 577dpi with xxxhdpi resolutions).

The most consistent and reliable solution would be not to use static height width, instead arrange layouts in such a way using wrap_content and match_parent properties and relative layouts

Dhaval Patel
  • 300
  • 1
  • 11
  • As mentioned in question, phones were Pixel and Pixel2: [compare phones](https://www.gsmarena.com/compare.php3?idPhone1=8346&idPhone2=8733). Both have the same ppi and resolution: 1080 x 1920 pixels, 16:9 ratio (~441 ppi density) – repitch Feb 14 '18 at 11:40
  • can you post required layout screen shot here – Dhaval Patel Feb 14 '18 at 13:02