3

In happy.xml I have a LinearLayout...

<LinearLayout
    android:orientation="vertical"
    android:divider="@drawable/happy_spacer"
    android:showDividers="middle"
    ...>

Here's the file happy_spacer.xml...

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <size android:height="8dp" />
    <solid android:color="#90909090" />
</shape>

Fantastic, the spacer is 8dp high.

But you can't work like that anymore in a responsive world, the height of the spacer has to be a certain percentage of the height of the page it is sitting in.

(Or, it has to - say - relate to the height of something else on happy.xml.)

What's the Android solution?

(Important - as Charuka explains below, you almost certainly want "px" physical pixels in such a situation, not dp or dip.)

For modern Android, apk 21 forward only.

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • 1
    It exist a support library to handle percent but it's only PercentRelativeLayout and PercentFrameLayout... I don't think it's possible to use a percent for a shape. – Jaythaking Dec 13 '16 at 15:55

3 Answers3

4

Usually, at least in my experience, list item dividers are a static height. In any case, I do not think you can make it a percentage of the screen height using XML, but you can programmatically using setDividerHeight(); though you will have to calculate the size based on the percentage yourself.

Otherwise, you can adjust the size based on specific break points by using resource qualifiers; such as h720dp (minimum height of 720dp) or w600dp (minimum width of 600dp).

Bryan
  • 14,756
  • 10
  • 70
  • 125
  • 1
    Wait, `setDividerHeight` does *not* apply to LinearLayout, correct? – Fattie Dec 13 '16 at 16:34
  • @JoeBlow No, but if you really wanted to, you should be able to create a `Drawable` at the size you would like, and then use [`setDividerDrawable()`](https://developer.android.com/reference/android/widget/LinearLayout.html#setDividerDrawable(android.graphics.drawable.Drawable)). – Bryan Dec 13 '16 at 16:38
  • Say, @Bryan I was wondering in Android terminology, what's the difference between "resource qualifiers" and "configuration qualifiers" (if any)? Cheers – Fattie Dec 16 '16 at 11:49
  • @JoeBlow Same thing, I think the more accepted term may be "configuration qualifiers". They qualify specific resources based on device configuration, haha – Bryan Dec 16 '16 at 13:54
  • @JoeBlow Wow, thank you. Completely unnecessary, but I'm glad I could be of help. – Bryan Dec 19 '16 at 13:31
2

Use configuration qualifiers and provide different dimens:

e.g.

// happy_spacer.xml
<size android:height="@dimen/divider" />

// values/dimens.xml
<dimen name="divider">8dp</dimen>

// values-w720dp/dimens.xml
<dimen name="divider">16dp</dimen>

// values-w820dp/dimens.xml
<dimen name="divider">24dp</dimen>
1

agrees with Bryan's answer and if you use your divider inside a list item you can use setDividerHeight() method. But if you directly use your divider inside a LinearLayout you only have linearLayout.setDividerDrawable();

Note :

You should use 8px instead of 8dp or 8dip,if you use dp or dip Android will scale that down depending the device dpi.So it changes a bit depending the device in a responsive world ;) which looks bit ugly. So to have your real question you should use height in px value which is clear and same size for any screen... .

Community
  • 1
  • 1
Charuක
  • 12,953
  • 5
  • 50
  • 88