Android LinearLayout divider, but percentage?

816 views Asked by At

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.

3

There are 3 answers

5
Bryan On BEST ANSWER

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

0
Vincenzo Petronio On

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
Charuක On

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