Skip to content

1.0.1

Latest
Compare
Choose a tag to compare
@consp1racy consp1racy released this 23 Oct 16:37
· 21 commits to master since this release
69293c4

Optical insets

Ever since API 18 the platform has contained a feature called "optical layout". This would, in theory, give your app desired look even when dealing with extra paddings (e.g. for legacy shadows) without resorting to ugly hacks, such as negative margins, to fix alignment.

Too bad it was only supported on Nine-Patch bitmaps since API 18 and InsetDrawable since API 21 and it still produced one undesired effect (read further).

This library opens up optical insets for your apps running on devices from Jellybean MR2 to Pie.

Root layouts

Normally, when dealing with optical layout, a parent view picks the largest optical bounds of its children and sets that as its own optical bounds. You're left with a view hierarchy that looks as the designer intended except for the extra padding on the root layout.

By using one of the layouts listed below as root you'll get rid of the extra padding and finally arrive at the desired look of the whole view hierarchy.

  • FrameLayout -> net.xpece.androidx.optical.RootFrameLayout
  • LinearLayout -> net.xpece.androidx.optical.RootLinearLayout
  • ConstraintLayout -> net.xpece.androidx.optical.RootConstraintLayout
  • CoordinatorLayout -> net.xpece.androidx.optical.RootCoordinatorLayout

The root view group should specify android:layoutMode="opticalBounds" for all its children to be laid out in optical mode (unless they've overridden the value for themselves). You may also want to set android:clipToPadding="false" on the root layout.

Inset widgets

Here's a list of provided optical inset aware widgets:

CardView

Use net.xpece.androidx.optical.InsetCardView which reports its compat shadow area as optical insets. This does nothing when running on Lolliop with useCompatPadding disabled.

Download

Release

Pick the dependencies you need:

dependencies {
    implementation 'com.github.consp1racy.android-support-optical:root-frame-layout:x.y.z'
    implementation 'com.github.consp1racy.android-support-optical:root-linear-layout:x.y.z'
    implementation 'com.github.consp1racy.android-support-optical:root-constraint-layout:x.y.z'
    implementation 'com.github.consp1racy.android-support-optical:root-coordinator-layout:x.y.z'

    implementation 'com.github.consp1racy.android-support-optical:widget-cardview:x.y.z'
}

repositories {
    google()
    jcenter()
    maven { url 'https://jitpack.io' }
}