Share
Embed
Going edge-to-edge with Gesture Navigation
To aid developers to create immersive experiences, Android Q adds new system navigation models to allow apps to take over more of the user’s screen.
This talk will look at how apps should work with the new gesture navigation, and ways to mitigate gesture conflicts. We’ll look at how to update apps to go edge-to-edge, drawing behind the system bars to create a immersive experience for users. Finally we’ll go through ways to make the WindowInsets APIs easier to use.
Chris Banes
July 02, 2019
Transcript
119 slides
-
Which nav will Android use in the future? 3-button mode
is required for Android devices Gestural nav may be offered as a polished, immersive experience, and can additionally be offered out-of-box -
Why is this so important for apps? Gaining an edge
on user experience given market desire for beautiful UI Handling conflicts with gestures for optimal UX -
Making your app nav-ready 1. Make your UI edge-to-edge 2.
Leverage insets for better UI 3. Override system gestures -
10:00 Drawing behind the navigation bar Strongly recommended on Q
Optional before Q -
System will help From Q onwards, the system is responsible
for protecting system buttons/handles -
System will help From Q onwards, the system is responsible
for protecting system buttons/handles -
System will help From Q onwards, the system is responsible
for protecting system buttons/handles Protection can be in the form of dynamic color adaptation -
System will help From Q onwards, the system is responsible
for protecting system buttons/handles Or the system can draw a translucent scrim Shown in button mode when targetSdkVersion >= 29 -
Going immersive Avoid overlaps with system UI Request to be
laid out fullscreen Change system bar colors 1 2 3 -
<style name="Theme.MyApp" parent="..."> <!-- 70% white --> <item name="android:navigationBarColor"> #B3FFFFFF
</item> </style> values/themes.xml Change system bar colors 1 -
<style name="Theme.MyApp" parent="..."> <!-- 70% black --> <item name="android:navigationBarColor"> #B3000000
</item> </style> values-night/themes.xml Change system bar colors 1 -
view.systemUiVisibility = // Tells the system that you wish to
be laid out // as if the navigation bar was hidden View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or // Tells the system that you wish to be laid out at // the most extreme scenario of any other flags View.SYSTEM_UI_FLAG_LAYOUT_STABLE Request to be laid out fullscreen 2 -
view.systemUiVisibility = // Tells the system that you wish to
be laid out // as if the navigation bar was hidden View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or // Optional, if we want you be laid out fullscreen, // behind the status bar View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or // Tells the system that you wish to be laid out at // the most extreme scenario of any other flags View.SYSTEM_UI_FLAG_LAYOUT_STABLE Request to be laid out fullscreen 2 -
What are insets? A collection of values which tell you
how much to inset (move) content in by Which content to move, depends on the inset type and its purpose -
I N S E T S System window Tells you
where the system- ui is being displayed over your app Use to move clickable views away from edges -
I N S E T S System window Tells you
where the system- ui is being displayed over your app Use to move clickable views away from edges -
System window I N S E T S Tells you
where the system- ui is being displayed over your app Use to move clickable views away from edges -
System window I N S E T S Tells you
where the system- ui is being displayed over your app Use to move clickable views away from edges -
System gesture Represents the areas of the window where system
gestures take priority Includes the vertical edges for swiping back, and bottom edge for home Use to move draggable views away from edges New in Q I N S E T S -
System gesture New in Q I N S E T
S Represents the areas of the window where system gestures take priority Includes the vertical edges for swiping back, and bottom edge for home Use to move draggable views away from edges -
System gesture New in Q I N S E T
S Represents the areas of the window where system gestures take priority Includes the vertical edges for swiping back, and bottom edge for home Use to move draggable views away from edges -
System gesture New in Q I N S E T
S Represents the areas of the window where system gestures take priority Includes the vertical edges for swiping back, and bottom edge for home Use to move draggable views away from edges -
System gesture New in Q I N S E T
S Represents the areas of the window where system gestures take priority Includes the vertical edges for swiping back, and bottom edge for home Use to move draggable views away from edges -
Mandatory system gesture Subset of system gesture insets Defines
areas which cannot be overridden by apps In Q, currently only used by the home gesture zone I N S E T S New in Q -
System window insets Use to move clickable views away from
edges System gesture insets Use to move draggable views away from edges Mandatory system gesture insets Use to check what gesture areas can not be excluded -
ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets -> // Do something with the
insets // return the insets insets } Tip: always use the ViewCompat method -
getSystemWindowInsets(): Insets getSystemGestureInsets(): Insets getMandatorySystemGestureInsets(): Insets getStableInsets(): Insets WindowInsetsCompat data
class Insets( left: Int, top: Int, right: Int, bottom: Int ) -
VC.setOnApplyWindowInsetsListener(bottomBar) { v, insets -> v.updatePadding( bottom = insets.systemWindowInsets.bottom )
insets } <LinearLayout android:padding="32dp"> Overwrites -
VC.setOnApplyWindowInsetsListener(bottomBar) { v, insets -> v.updatePadding( bottom = bottomPad +
insets.systemWindowInsets.bottom ) insets } val bottomPad = bottomBar.bottomPadding // 32dp -
VC.setOnApplyWindowInsetsListener(bottomBar) { v, insets -> v.updatePadding( bottom = bottomPad +
insets.systemWindowInsets.bottom ) insets } val bottomPad = bottomBar.bottomPadding -
data class InitialPadding( val left: Int, val top: Int, val
right: Int, val bottom: Int ) private fun recordInitialPaddingForView(view: View) = ... fun View.doOnApplyWindowInsets(f: (View, WindowInsets, InitialPadding) -> Unit) { // Create a snapshot of the view's padding state val initialPadding = recordInitialPaddingForView(this) // Set an OnApplyWindowInsetsListener which proxies to the given // lambda, passing in the original padding state ViewCompat.setOnApplyWindowInsetsListener(this) { v, insets -> f(v, insets, initialPadding) // Always return the insets, so that children can also use them insets } } -
data class InitialPadding( val left: Int, val top: Int, val
right: Int, val bottom: Int ) private fun recordInitialPaddingForView(view: View) = ... fun View.doOnApplyWindowInsets(f: (View, WindowInsets, InitialPadding) -> Unit) { // Create a snapshot of the view's padding state val initialPadding = recordInitialPaddingForView(this) // Set an OnApplyWindowInsetsListener which proxies to the given // lambda, passing in the original padding state ViewCompat.setOnApplyWindowInsetsListener(this) { v, insets -> f(v, insets, initialPadding) // Always return the insets, so that children can also use them insets } } -
val right: Int, val bottom: Int ) private fun recordInitialPaddingForView(view:
View) = ... fun View.doOnApplyWindowInsets(f: (View, WindowInsets, InitialPadding) -> Unit) { // Create a snapshot of the view's padding state val initialPadding = recordInitialPaddingForView(this) // Set an OnApplyWindowInsetsListener which proxies to the given // lambda, passing in the original padding state ViewCompat.setOnApplyWindowInsetsListener(this) { v, insets -> f(v, insets, initialPadding) // Always return the insets, so that children can also use them insets } } -
bottomBar.doOnApplyWindowInsets { v, insets, initialPadding -> v.updatePadding( bottom = initialPadding.bottom
+ insets.systemWindowInsets.bottom ) insets } -
@BindingAdapter( "paddingLeftSystemWindowInsets", "paddingTopSystemWindowInsets", "paddingRightSystemWindowInsets", "paddingBottomSystemWindowInsets", requireAll = false ) fun
applySystemWindows( view: View, applyLeft: Boolean, applyTop: Boolean, applyRight: Boolean, applyBottom: Boolean ) { view.doOnApplyWindowInsets { view, insets, padding -> val left = if (applyLeft) insets.systemWindowInsetLeft else val top = if (applyTop) insets.systemWindowInsetTop else 0 -
What conflicts will apps face? Swiping in from the edges
will go back or re-activate system gestures when in immersive mode. Any horizontally-draggable content that relies on gestures in the back zone Examples: sliders, seek bars, drag handles, etc. -
What conflicts will apps face? Swiping in from the edges
will go back or re-activate system gestures when in immersive mode. Any horizontally-draggable content that relies on gestures in the back zone Examples: sliders, seek bars, drag handles, etc. -
Overriding gestures New API available to you for opting out
a small part of the edge for app use Should be used extremely sparingly and tied to a draggable handle or visual affordance New in Q -
List<Rect> exclusionRects; public void onLayout(...) { // Update rect bounds
and the exclusionRects list setSystemGestureExclusionRects(exclusionRects); } CustomView.java -
List<Rect> exclusionRects; public void onDraw(Canvas canvas) { // Update rect
bounds and the exclusionRects list setSystemGestureExclusionRects(exclusionRects); } CustomView.java -
Restrictions on overriding gestures Apps can only opt out 200
dp of each edge If apps opt out of more, only the bottom 200 dp will be overridden for the app Requested -
Restrictions on overriding gestures Apps can only opt out 200
dp of each edge If apps opt out of more, only the bottom 200 dp will be overridden for the app Honored Requested -
Non-sticky immersive mode Apps can only opt out 200 dp
of each edge from bringing in system bars and activating gestures -
Non-sticky immersive mode Apps can only opt out 200 dp
of each edge from bringing in system bars and activating gestures -
Non-sticky immersive mode Apps can only opt out 200 dp
of each edge from bringing in system bars and activating gestures -
Non-sticky immersive mode Apps can only opt out 200 dp
of each edge from bringing in system bars and activating gestures -
Non-sticky immersive mode Apps can only opt out 200 dp
of each edge from bringing in system bars and activating gestures -
Sticky immersive mode By updating, apps can opt the entire
edge out of bringing in system bars and reactivating gestures Once the gestures are active, the system will not respect any Back overrides -
Sticky immersive mode By updating, apps can opt the entire
edge out of bringing in system bars and reactivating gestures Once the gestures are active, the system will not respect any Back overrides -
Sticky immersive mode By updating, apps can opt the entire
edge out of bringing in system bars and reactivating gestures Once the gestures are active, the system will not respect any Back overrides -
Sticky immersive mode By updating, apps can opt the entire
edge out of bringing in system bars and reactivating gestures Once the gestures are active, the system will not respect any Back overrides -
Sticky immersive mode By updating, apps can opt the entire
edge out of bringing in system bars and reactivating gestures Once the gestures are active, the system will not respect any Back overrides -
Sticky immersive mode By updating, apps can opt the entire
edge out of bringing in system bars and reactivating gestures Once the gestures are active, the system will not respect any Back overrides -
Sticky immersive mode Useful for single-purpose UIs that want to
keep the user from accidentally going back Examples: games, drawing apps, etc. -
Scrolling views Content should be drawn behind the navigation bar
When the user scrolls to the end, the content is drawn above the navigation bar S C E N A R I O S -
Scrolling views Content should be drawn behind the navigation bar
When the user scrolls to the end, the content is drawn above the navigation bar S C E N A R I O S -
Scrolling views Content should be drawn behind the navigation bar
When the user scrolls to the end, the content is drawn above the navigation bar S C E N A R I O S -
Scrolling views Content should be drawn behind the navigation bar
When the user scrolls to the end, the content is drawn above the navigation bar S C E N A R I O S -
Scrolling views Common way to achieve this is through padding
We’ll pad our scrolling view using the system window insets S C E N A R I O S -
recyclerview.setOnApplyWindowInsetsListener { v, insets -> // Set the bottom padding
so that the content bottom // is above the nav bar (y-axis) v.updatePadding(bottom = insets.systemWindowInsetBottom) // return the insets insets } -
Bottom sheets For bottom sheets which collapse, ensure enough content
is visible above the navigation bar to be dragged S C E N A R I O S -
Bottom sheets For bottom sheets which collapse, ensure enough content
is visible above the navigation bar to be dragged Use the system gesture insets to increase the peeking content height S C E N A R I O S -
Bottom sheets For bottom sheets which collapse, ensure enough content
is visible above the navigation bar to be dragged Use the system gesture insets to increase the peeking content height S C E N A R I O S -
// Record the original peek height val origPeekHeight = behavior.peekHeight
view.setOnApplyWindowInsetsListener { v, insets -> val gestureInsets = insets.getSystemGestureInsets() // Update the peek height so that it is above the navigation bar behavior.peekHeight = gestureInsets.bottom + origPeekHeight // return the insets insets } -
Bottom sheets Remember, users can always swipe to go home
Highlight draggable content to guide user S C E N A R I O S -
Bottom navigation For bottom tabbed navigation, pad the bottom content
in by the system window inset S C E N A R I O S -
Bottom navigation For bottom tabbed navigation, pad the bottom content
in by the system window inset S C E N A R I O S -
Landscape S C E N A R I O S
If you have content that starts near a vertical edge, think about padding all content in horizontally Make sure to test apps with button navigation too -
rootView.setOnApplyWindowInsetsListener { v, insets -> // Set the left/right padding
so that the content // is above the nav bar (y-axis) v.updatePadding( left = insets.systemWindowInsetLeft, right = insets.systemWindowInsetRight ) // return the insets insets } -
Navigation drawers New swipe-when-peeking behavior in Q Beta 5 Works
with all versions of DrawerLayout Update to DrawerLayout 1.1.0-alpha02 for best experience S C E N A R I O S -
Carousels For ViewPagers and horizontal scrolling views, recommend to not
exclude edges S C E N A R I O S -
Carousels For ViewPagers and horizontal scrolling views, recommend to not
exclude edges S C E N A R I O S -
TL;DR G E S T U R E N A
V I G A T I O N Users will expect apps to work with all navigation modes Create immersive UIs by going edge-to-edge Proactively update Jetpack and MDC libraries for automatic support Only use gesture exclusion APIs if you really need them Let us know about troublesome scenarios in your apps