Slide 1

Slide 1 text

Android UI Patterns, Practices, Pitfalls @chris_h_codes

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

https:/ /github.com/aosp-mirror/.../View.java

Slide 5

Slide 5 text

CSS IS AWESOME

Slide 6

Slide 6 text

ViewGroup View

Slide 7

Slide 7 text

override fun onLayout( changed: Boolean, left: Int, top: Int, right: Int, bottom: Int ) ViewGroup override fun onMeasure( widthSpec: Int, heightSpec: Int )\

Slide 8

Slide 8 text

FrameLayout AbsoluteLayout LinearLayout RelativeLayout ListView

Slide 9

Slide 9 text

GridLayout ConstraintLayout CoordinatorLayout NestedScrollView RecyclerView

Slide 10

Slide 10 text

ConstraintLayout

Slide 11

Slide 11 text

Slide 12

Slide 12 text

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Slide 15

Slide 15 text

Slide 16

Slide 16 text

Text label This tag and its children can be replaced by one and a compound drawable

Slide 17

Slide 17 text

override fun onMeasure( widthSpec: Int, heightSpec: Int )/ 1x 1x 1x 1x 1x 1x 1x 1x

Slide 18

Slide 18 text

override fun onMeasure( widthSpec: Int, heightSpec: Int )/ 1x 2x 1x 2x 2x 1x 1x 1x

Slide 19

Slide 19 text

override fun onMeasure( widthSpec: Int, heightSpec: Int )/ 2x 4x 2x 4x 4x 2x 2x 2x

Slide 20

Slide 20 text

Slide 21

Slide 21 text

Slide 22

Slide 22 text

ConstraintLayout

Slide 23

Slide 23 text

ConstraintLayout ConstraintLayout

Slide 24

Slide 24 text

ConstraintLayout ConstraintLayout

Slide 25

Slide 25 text

Main container Screen container Screen root Recycler List children

Slide 26

Slide 26 text

Be mindful of your ConstraintLayouts

Slide 27

Slide 27 text

Be mindful of your MotionLayouts

Slide 28

Slide 28 text

Animation Complexity

Slide 29

Slide 29 text

Animation Complexity view.animate() .translationX(100f) .alpha(0.5f) .rotation(45f) Easy Pretty tricky

Slide 30

Slide 30 text

Animation Complexity view.animate() .translationX(100f) .alpha(0.5f) .rotation(45f) Easy Pretty tricky android:animateLayoutChanges="true"

Slide 31

Slide 31 text

Animation Complexity view.animate() .translationX(100f) .alpha(0.5f) .rotation(45f) Easy Pretty tricky android:animateLayoutChanges="true" viewGroup .layoutTransition .enableTransitionType(LayoutTransition.CHANGING)

Slide 32

Slide 32 text

Animation Complexity view.animate() .translationX(100f) .alpha(0.5f) .rotation(45f) Easy Pretty tricky ObjectAnimator.ofFloat(0f, 1f)\ android:animateLayoutChanges="true"

Slide 33

Slide 33 text

Animation Complexity view.animate() .translationX(100f) .alpha(0.5f) .rotation(45f) Easy Pretty tricky ObjectAnimator.ofFloat(object : Property ...)\ android:animateLayoutChanges="true"

Slide 34

Slide 34 text

Animation Complexity view.animate() .translationX(100f) .alpha(0.5f) .rotation(45f) Easy Pretty tricky android:animateLayoutChanges="true" ObjectAnimator.ofFloat(...)\

Slide 35

Slide 35 text

Animation Complexity view.animate() .translationX(100f) .alpha(0.5f) .rotation(45f) Easy Pretty tricky android:animateLayoutChanges="true" ObjectAnimator.ofFloat(...)\ TransitionManager .beginDelayedTransition(viewGroup)

Slide 36

Slide 36 text

Animation Complexity view.animate() .translationX(100f) .alpha(0.5f) .rotation(45f) Easy Pretty tricky android:animateLayoutChanges="true" ObjectAnimator.ofFloat(...)\ TransitionManager .beginDelayedTransition(vie

Slide 37

Slide 37 text

MotionLayout Easy to use, declarative syntax Seekable Can be driven by touch

Slide 38

Slide 38 text

MotionLayout Easy to use, declarative syntax Seekable Can be driven by touch All animated views have to be direct children Can’t easily perform shared element transitions

Slide 39

Slide 39 text

Transition Tips

Slide 40

Slide 40 text

Transition Tips 1. Make them quick, debug them slow

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

Transition Tips 1. Make them quick, debug them slow 2. Choreograph customisations with Kotlin

Slide 45

Slide 45 text

class SomeFragment : Fragment() { sharedElementEnterTransition = TransitionInflater .from(context) .inflateTransition(android.R.transition.move) }

Slide 46

Slide 46 text

sharedElementEnterTransition = TransitionInflater .from(context) .inflateTransition(android.R.transition.move)

Slide 47

Slide 47 text

Transitions can… Target specific views Exclude specific views Have their duration, interpolation, and pathing modified

Slide 48

Slide 48 text

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

inline fun transitionSet( block: TransitionSet.() -> Unit ): TransitionSet { return TransitionSet().apply(block) } inline fun TransitionSet.addSet( block: TransitionSet.() -> Unit ) { addTransition(TransitionSet().apply(block)) } inline fun TransitionSet.addTransition( transition: Transition, block: Transition.() -> Unit ) { transition.apply(block) addTransition(transition) }

Slide 51

Slide 51 text

val transition = transitionSet { duration = 250 addSet { addTransition(ChangeBounds()) addTransition(ChangeTransform()) addTarget(currentFeelingTransitionName) interpolator = FastOutSlowInInterpolator() } addSet { addTransition(ChangeBounds()) { pathMotion = ArcMotion() } addTransition(ChangeTransform()) addTransition(TextResize()) addTarget(titleTransitionName) interpolator = FastOutLinearInInterpolator() } }

Slide 52

Slide 52 text

Transition Tips 1. Make them quick, debug them slow 2. Choreograph customisations with Kotlin 3. Respect your parents

Slide 53

Slide 53 text

val transition = transitionSet { duration = 250 addSet { addTransition(ChangeBounds()) addTarget(viewA) interpolator = FastOutSlowInInterpolator() }/ addSet { addTransition(ChangeBounds()) { pathMotion = ArcMotion() } addTransition(TextResize()) addTarget(viewB) interpolator = FastOutLinearInInterpolator() }/ }/

Slide 54

Slide 54 text

val transition = transitionSet { duration = 250 addSet { addTransition(ChangeBounds()) addTransition(ChangeTransform()) addTarget(viewA) interpolator = FastOutSlowInInterpolator() }/ addSet { addTransition(ChangeBounds()) { pathMotion = ArcMotion() } addTransition(ChangeTransform()) addTransition(TextResize()) addTarget(viewB) interpolator = FastOutLinearInInterpolator() }/ }/

Slide 55

Slide 55 text

Slide 56

Slide 56 text

Slide 57

Slide 57 text

Slide 58

Slide 58 text

Transition Tips 1. Make them quick, debug them slow 2. Choreograph customisations with Kotlin 3. Respect your parents 4. Remember that arc motion is a thing

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

addTransition(ChangeBounds()) { pathMotion = ArcMotion() }/

Slide 61

Slide 61 text

addTransition(ChangeBounds()) { pathMotion = ArcMotionPlus() }/

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

https:/ /github.com/neild001/ArcMotionPlus Using the ArcMotionPlus Using the ArcMotionPlus is straightforward. There are a just two settings • Arc Angle • Reflect Neil Davies neild001

Slide 64

Slide 64 text

https:/ /gist.github.com/chris-horner/80837fd3f3ac54052b766648d80ddbd2 class ArcMotionPlus(private val arcAngle: Float = 90f, private val reflected: Boolean = false) : PathMotion() { override fun getPath(startX: Float, startY: Float, endX: Float, endY: Float): Path { val start = PointF(startX, startY) val end = PointF(endX, endY) require(!(start.x == end.x && start.y == end.y)) { "Start and end points cannot be the same." } require(arcAngle in 1.0..179.0) { "Arc angle must be between 1 and 179 degrees." } val angleRadians: Double = Math.toRadians(arcAngle.toDouble()) val deltaX: Float = start.x - end.x val deltaY: Float = start.y - end.y val halfChordLength: Float = sqrt((deltaX * deltaX + deltaY * deltaY).toDouble()).toFloat() / 2f val radius: Float = halfChordLength / sin(angleRadians / 2f).toFloat() // The length of the line from the start or end point to the control point. chris-horner / ArcMotionPlus.kt

Slide 65

Slide 65 text

Transition Tips 1. Make them quick, debug them slow 2. Choreograph customisations with Kotlin 3. Respect your parents 4. Remember that arc motion is a thing

Slide 66

Slide 66 text

ScrollView Shenanigans

Slide 67

Slide 67 text

ScrollView Shenanigans

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

Slide 72

Slide 72 text

Slide 73

Slide 73 text

Slide 74

Slide 74 text

Slide 75

Slide 75 text

Slide 76

Slide 76 text

Forget ActionBar

Slide 77

Slide 77 text

Slide 78

Slide 78 text

Slide 79

Slide 79 text

Slide 80

Slide 80 text

Slide 81

Slide 81 text

AndroidManifest.xml activity_main.xml MainActivity.kt setSupportActionBar(toolbar)

Slide 82

Slide 82 text

Toolbar Fragment A

Slide 83

Slide 83 text

Title A Fragment A Fragment B

Slide 84

Slide 84 text

Title B Fragment A Fragment B

Slide 85

Slide 85 text

Title B Fragment B override fun onViewCreated() { setHasOptionsMenu(true) } override fun onCreateOptionsMenu() { super.onCreateOptionsMenu(menu, inflater) menu.clear() inflater.inflate(R.menu.screen_b, menu) }

Slide 86

Slide 86 text

Title B

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

Title B

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

Don’t

Slide 91

Slide 91 text

Slide 92

Slide 92 text

transitionSet { addTransition(ChangeBounds()) { addTarget(toolbar) } addTransition(Slide()) { addTarget(fromView) addTarget(toView) } }

Slide 93

Slide 93 text

Styling Toolbars

Slide 94

Slide 94 text

Slide 95

Slide 95 text

Slide 96

Slide 96 text

Slide 97

Slide 97 text

Slide 98

Slide 98 text

Slide 99

Slide 99 text

Slide 100

Slide 100 text

<item name="colorPrimary">@color/blue_700</item> <item name="colorPrimaryVariant">@color/blue_700_variant</item> <item name="colorPrimaryDark">@color/blue_700_variant</item> <item name="colorOnPrimary">@android:color/white</item> <item name="colorOnPrimarySurface">@android:color/white</item>

Slide 101

Slide 101 text

<item name="colorPrimary">@color/blue_700</item> <item name="colorPrimaryVariant">@color/blue_700_variant</item> <item name="colorPrimaryDark">@color/blue_700_variant</item> <item name="colorOnPrimary">@android:color/white</item> <item name="colorOnPrimarySurface">@android:color/white</item>

Slide 102

Slide 102 text

Slide 103

Slide 103 text

Basically, never do this

Slide 104

Slide 104 text

Respect Themes

Slide 105

Slide 105 text

android:background="@color/brandPrimary"\

Slide 106

Slide 106 text

android:background="?attr/colorPrimary"\

Slide 107

Slide 107 text

Slide 108

Slide 108 text

Slide 109

Slide 109 text

Required Watching https://www.youtube.com/watch?v=Owkf8DhAOSo

Slide 110

Slide 110 text

Inset Issues

Slide 111

Slide 111 text

Inset Issues https://www.youtube.com/watch?v=_mGDMVRO3iE BECOMING A MASTER WINDOW FITTER @chrisbanes

Slide 112

Slide 112 text

Is one of my parent ViewGroups “insets aware”? How far down do I need to put fitsSystemWindows="true"? How do I cope with cumulative padding? Questions I Always Had

Slide 113

Slide 113 text

Give up

Slide 114

Slide 114 text

Always render edge-to-edge Give up

Slide 115

Slide 115 text

override fun onCreate(savedInstanceState: Bundle?) { // Render under the status and navigation bars. window.decorView.systemUiVisibility = window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE } Always render edge-to-edge

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

toolbar.updatePaddingWithInsets(top = true) bottomNav.updatePaddingWithInsets(bottom = true)

Slide 118

Slide 118 text

https:/ /gist.github.com/chris-horner/4718402eb6ebd4d89b63491245a359ff fun View.updatePaddingWithInsets(left: Boolean = false, top: Boolean = false, right: Boolean = false, bottom: Boolean = false) { doOnApplyWindowInsets { insets, padding -> updatePadding(left = if (left) padding.left + insets.systemWindowInsetLeft else padding.left, top = if (top) padding.top + insets.systemWindowInsetTop else padding.top, right = if (right) padding.right + insets.systemWindowInsetRight else padding.right, bottom = if (bottom) padding.bottom + insets.systemWindowInsetBottom else padding.bottom) } } inline fun View.doOnApplyWindowInsets(crossinline block: (insets: WindowInsets, padding: Rect) -> Unit) { // Create a snapshot of padding. val initialPadding = Rect(paddingLeft, paddingTop, paddingRight, paddingBottom) // Set an actual OnApplyWindowInsetsListener which proxies to the given lambda, also passing in the original padding. chris-horner / InsetUtils.kt

Slide 119

Slide 119 text

https:/ /github.com/chrisbanes/insetter

Slide 120

Slide 120 text

https:/ /github.com/material-components/material-components-android/releases

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

Beware the Shadow Barber

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

No content

Slide 126

Slide 126 text

No content

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

Slide 129

Slide 129 text

Slide 130

Slide 130 text

Slide 131

Slide 131 text

Drawable Digressions

Slide 132

Slide 132 text

Slide 133

Slide 133 text

Slide 134

Slide 134 text

Dynamic?

Slide 135

Slide 135 text

Dynamic?

Slide 136

Slide 136 text

class CustomLayout : LinearLayout { fun display(items: List) { for (index in items.indices) { val view: View = inflate(R.layout.item) if (index != 0) { val params = view.layoutParams params.topMargin = dpToPx(16) } addView(view) } } }

Slide 137

Slide 137 text

class CustomLayout : LinearLayout { fun display(items: List) { for (index in items.indices) { val view: View = inflate(R.layout.item) if (index != 0) { val space = Space(context) space.layoutParams.height = dpToPx(16) addView(space) } addView(view) } } }

Slide 138

Slide 138 text

What if…?

Slide 139

Slide 139 text

We do have…

Slide 140

Slide 140 text

Slide 141

Slide 141 text

Slide 142

Slide 142 text

Slide 143

Slide 143 text

Recycler Wrongdoings

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

No content

Slide 146

Slide 146 text

No content

Slide 147

Slide 147 text

Generally, as long as you give views an ID they’ll try and restore their state

Slide 148

Slide 148 text

You’re in a race!

Slide 149

Slide 149 text

fun onRestoreInstanceState(state: Parcelable) You’re in a race!

Slide 150

Slide 150 text

dataStream .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { items -> adapter.setItems(items) }\

Slide 151

Slide 151 text

dataStream .replay(1) .refCount() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { items -> adapter.setItems(items) }\

Slide 152

Slide 152 text

dataStream .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .replay(1) .refCount() .subscribe { items -> adapter.setItems(items) }\

Slide 153

Slide 153 text

.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .replay(1) .refCount() .subscribe { items -> adapter.setItems(items) }\ val dataStream: Flow>

Slide 154

Slide 154 text

dataStream .onEach { items -> display(items) } .launchIn(scope)

Slide 155

Slide 155 text

dataStream .onEach { items -> display(items) } .launchIn(scope) val scope = ???

Slide 156

Slide 156 text

dataStream .onEach { items -> display(items) } .launchIn(scope) val scope = viewModelScope

Slide 157

Slide 157 text

dataStream .onEach { items -> display(items) } .launchIn(scope) val scope = MainScope()

Slide 158

Slide 158 text

/** * Creates the main [CoroutineScope] for UI components. */ val scope = MainScope()

Slide 159

Slide 159 text

val scope = MainScope() ContextScope(SupervisorJob() + Dispatchers.Main)

Slide 160

Slide 160 text

val scope = MainScope() val scope = viewModelScope ContextScope(SupervisorJob() + Dispatchers.Main)

Slide 161

Slide 161 text

val scope = MainScope() val scope = viewModelScope ContextScope(SupervisorJob() + Dispatchers.Main) SupervisorJob() + Dispatchers.Main.immediate

Slide 162

Slide 162 text

Attach and put data in your adapter right after your views are inflated

Slide 163

Slide 163 text

What Takeaways?

Slide 164

Slide 164 text

Takeaways ConstraintLayout and MotionLayout are great! But remember there are alternatives Widgets sometimes have surprising properties Handle Toolbars and window insets manually Understand and get the most out of the theming system Watch out for gotchas when it comes to state restoration and rendering

Slide 165

Slide 165 text

chris_h_codes chris-horner chrishorner.codes Android UI Patterns, Practices, Pitfalls