Insetter main library¶
The main library provides simple APIs for handling WindowInsets.
Builder¶
The Builder
is the main entry point for Insetter. It allows you to build up how you want a view
to react to WindowInsets:
Insetter.builder()
// This will add the navigation bars insets as padding to all sides of the view,
// maintaining the original padding (from the layout XML, style, etc)
.padding(windowInsetTypesOf(navigationBars = true))
// This will add the status bars insets as margin to all sides of the view,
// maintaining the original margins (from the layout XML, etc)`
.margin(windowInsetTypesOf(statusBars = true))
// This is a shortcut for view.setOnApplyWindowInsetsListener(builder.build())
.applyToView(view)
Insetter.builder()
// This will add the navigation bars insets as padding to all sides of the view,
// maintaining the original padding (from the layout XML, style, etc)
.padding(WindowInsetsCompat.Type.navigationBars())
// This will add the status bars insets as margin to all sides of the view,
// maintaining the original margins (from the layout XML, etc)`
.margin(WindowInsetsCompat.Type.statusBars())
// This is a shortcut for view.setOnApplyWindowInsetsListener(builder.build())
.applyToView(view);
Kotlin DSL¶
If you're using Kotlin, we also provided a DSL via the View.applyInsetter()
extension function. To achieve the same result as above:
view.applyInsetter {
// Apply the navigation bar insets...
type(navigationBars = true) {
// Add to padding on all sides
padding()
}
// Apply the status bar insets...
type(statusBars = true) {
// Add to margin on all sides
margin()
}
}
Animated Insets support¶
The library now has support for WindowInsetsAnimations
, allowing your content is react to inset animations, such as the on screen-keyboard (IME) being animated on/off screen.
This functionality works wherever WindowInsetsAnimationCompat works, which at the time or writing is on devices running API 21+.
binding.messageHolder.applyInsetter {
// Apply the navigation bar and ime insets...
type(navigationBars = true, ime = true) {
// ..as padding, enabling the animation support
padding(animated = true)
}
// This is optional, but it's usually necessary to sync the resulting
// translation to other views. You can provide multiple views here.
syncTranslationTo(binding.conversationRecyclerview)
}
See the AnimatedInsetterSample for a working example.
IME animations¶
If you're using the animation insets support for IME/keyboard animations, you also need to ensure that the activity's windowSoftInputMode
is set to adjustResize
:
<activity
android:name=".MyActivity"
android:windowSoftInputMode="adjustResize" />
Download¶
Latest version:
repositories {
mavenCentral()
}
dependencies {
implementation "dev.chrisbanes.insetter:insetter:<latest version>"
}
Snapshots of the current development version are available, which track the latest commit.
The snapshots are deployed to Sonatype's snapshots repository. The latest release is:
repositories {
// Need to add the Sonatype snapshots repo
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
dependencies {
// Check the latest SNAPSHOT version from the image above
implementation "dev.chrisbanes.insetter:insetter:XXX-SNAPSHOT"
}