1

I have coded as below for 3 devices (images below), and log the Density/Resolution/OS (so that you know which device is which). I have 5 buttons and 5 images, each with height from 10dp to 50dp.

You'll notice the images appears on all devices. All the buttons appear on on XXXHDPI device (which is a Samsung S7 device), but for on XXHDPI (Nexus 5) and XHDPI (Samsung S3), the 10dp button disappeared. Why?

The images as below

Layout run on 3 devices

And my layout as below (no other special code, just the default Empty Activity project).

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.elyeproj.phoneinfo.MainActivity">

    <TextView
        android:id="@+id/resolution_width"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/resolution_height"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/resolution_width_dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/resolution_height_dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/resolution_density"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/resolution_density_str"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/phone_os"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="10dp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="20dp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="30dp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="40dp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:layout_marginBottom="5dp"
        android:background="@android:color/holo_blue_dark"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_marginBottom="5dp"
        android:background="@android:color/holo_blue_dark"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_marginBottom="5dp"
        android:background="@android:color/holo_blue_dark"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginBottom="5dp"
        android:background="@android:color/holo_blue_dark"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/holo_blue_dark"/>
</LinearLayout>
Elye
  • 53,639
  • 54
  • 212
  • 474
  • You sure you aren't messing with any of the views dynamically? Try removing your padding just to see if that slides things down, but I wouldn't think it would impact it like this. – zgc7009 May 11 '16 at 13:36
  • I looks like the reason is that the nav buttons are taking away some of your real estate. The XML tries to draw whenever it can. But if it runs out of room, things may be omitted. – durbnpoisn May 11 '16 at 13:37
  • HI @zgc7009. Just try code `` , and you'll see it won't appear on the phone. – Elye May 11 '16 at 13:43
  • Hi @durbnpoisn, that's what I suspect as well. Thanks. So is there a way to make them look consistent across S7 and other devices? It is reliable for me to assume all XXXHDPI will have the 10dp button, but not the others, so that I could use different dimen on XXXHDPI vs the others for my button? – Elye May 11 '16 at 13:45
  • I mean, if you explicitly declare sizes of your buttons but don't know if your screen can fit all of it you can expect some undesired results. Use weight/weightSum here instead of explicit sizes and make sure everything fits. The reason I made my comment is that, according to the way Android lays things out, if it didn't fit it should be because it pushes the bottom view out, not a couple of random views in the middle (note you are missing OS on the Nexus 5 as well). – zgc7009 May 11 '16 at 13:48
  • The Nexus 5 is on SDK22. – Elye May 11 '16 at 13:56
  • @zgc7009, I did have view that have weight/weightSum. And then suddenly I realize that the S7 buttons are thicker than others which is odd. So I shrink down the issue until I realize S7 is displaying at the actual size, while other devices are actually ~10dp thiner. So that is something not expected. How should I ensure my button is consistent across S7 and other devices? Should I use xxxhdpi with a separate dimen to handle that? (is this the xxxhdpi specific, or S7 specific?) – Elye May 11 '16 at 14:01
  • Unfortunately, with all of the resolution/pixel densities of Android, sometimes you have to create compromises. The reason that the S7 looked "wider" is because there is more space available to the weights. So a weight of x will look a bit different per device. In your case, if you scaled images appropriately, I still think that weights are the best option. – zgc7009 May 11 '16 at 14:05
  • @zgc7009, In my actual code, I have use weight for the width (i.e. having two buttons side by side) in a Linear horizontal orientation layout, that is wrap by a Linear vertical orientation layout (and that is scrollable, as the height of the layout is beyond the screen size). Hence the height is not something I could add weight to. It's hard to imagine anytime we use button, we'll have to make weight for both the height and width. – Elye May 11 '16 at 14:13
  • It sounds like you need to rethink the way you are handling this layout. The only way I can think of doing what you want to do and ensuring that everything fits appropriately is using weights on the height. You cannot guarantee that your explicitly declared sizes are going to fit. – zgc7009 May 11 '16 at 14:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111652/discussion-between-elye-and-zgc7009). – Elye May 11 '16 at 14:18

2 Answers2

0

Better try to give separate LinearLayout for text view, button and image. Use android layout weight for that LinearLayout... hope it will work

Suresh
  • 701
  • 1
  • 6
  • 20
  • Sorry, I think you didn't get my question. It's not about the ImageView... That's just to show that 10dp image view still shows. But for 10dp Button View, it disappear totally. From the button, apparently all buttons in XHDPI and XXHDPI is smaller than it should be (I guess by around 10dp). – Elye May 11 '16 at 13:41
  • One more thing..samsung s7 is 4.0 density.. All other's are lower then that.. Can you try with lower then 10db means... – Suresh May 12 '16 at 10:23
  • Ya, that's what I'm referring to s7 is XXXHDPI, while others are lower resolution. – Elye May 12 '16 at 12:45
0

the "problem" is in default button background and its internal paddings. To fix - set some color to buttons background

for example

android:background="@android:color/holo_red_dark"
  • Setting the background will loose the elevation effect for the Button. Refers to http://stackoverflow.com/a/27155606/3286489. Also the question is, why S7 have the button, but not the others? – Elye May 11 '16 at 23:01