Search code examples
androidandroid-linearlayoutxml-layout

Android LinearLayout divider, but percentage?


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.


Solution

  • 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).