Slide 1

Slide 1 text

Adaptive Layout for Any Screen with Jetpack Compose Ahmad Arif Faizin Curriculum Developer at Dicoding

Slide 2

Slide 2 text

Ahmad Arif Faizin Curriculum Developer at Dicoding Adaptive Layout for Any Screen with Jetpack Compose

Slide 3

Slide 3 text

● Material Design Guidance ● Adaptive Navigation in Compose ● List-Detail in Compose ● Foldable Device ● Large Screen App Quality Outline

Slide 4

Slide 4 text

Source: Blog - What's new in foldables, tablets, and large screens

Slide 5

Slide 5 text

Mobile App Developers Be Like…

Slide 6

Slide 6 text

Source: Blog - What's new in foldables, tablets, and large screens

Slide 7

Slide 7 text

270M Active large screen Android devices

Slide 8

Slide 8 text

Apps are Optimizing for Large Screen goo.gle/large-screens-case-studies

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Build your app for the ecosystem increase your reach

Slide 11

Slide 11 text

Material Design Guidance 01

Slide 12

Slide 12 text

Understanding Anatomy 1. Margin 2. Column 3. Gutter

Slide 13

Slide 13 text

Breakpoints Breakpoint range (dp) Portrait Landscape Window size class Columns Minimum margins* 0-599 Handset Phone in portrait Compact 4 8 600-839 Foldable Small tablet Foldable Small tablet Medium 12 12 840+ Large tablet Large tablet Desktop Expanded 12 32

Slide 14

Slide 14 text

Avoid using physical/hardware values for making layout decisions!

Slide 15

Slide 15 text

Use Window size for layout decision!

Slide 16

Slide 16 text

Material Measurement

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Proven Design Pattern: Canonical Layout

Slide 19

Slide 19 text

List-Detail ● Best for browsing content and quickly seeing details. ● Recommended for 840+dp ● Key use case ○ Text message + conversation ○ File browser + open folder ○ Musical artist + album detail

Slide 20

Slide 20 text

Feed ● Equivalent content elements in a configurable grid for quick, convenient viewing of a large amount of content. ● Recommended for 600+dp ● Key use case ○ News ○ Social media

Slide 21

Slide 21 text

Supporting Panel ● The screen is divided between a focus panel (left) and a supporting panel (right). ● Recommended for 600+dp ● Key use case ○ Productivity ○ Document editing and commenting ○ Content and media browsing

Slide 22

Slide 22 text

Component Swapping

Slide 23

Slide 23 text

“Reach” Your Users on Large Screens

Slide 24

Slide 24 text

Window size class Few items Many items compact width bottom navigation bar navigation drawer (leading edge or bottom) medium width navigation rail navigation drawer (leading edge) expanded width persistent navigation drawer (leading edge) persistent navigation drawer (leading edge) Adaptive Navigation

Slide 25

Slide 25 text

Navigation Rail ● Side navigation component that displays three to seven app destinations ● Better ergonomics Easier reach on large screen ● Available in Material3

Slide 26

Slide 26 text

Adaptive Navigation in Compose 02

Slide 27

Slide 27 text

Resizable Emulator

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Window Size Classes API

Slide 30

Slide 30 text

WindowSizeClass Initialization implementation "androidx.compose.material3:material3-window-size-class:1.0.1" @OptIn(ExperimentalMaterial3WindowSizeClassApi::class) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { ReplyTheme { val widthSizeClass = calculateWindowSizeClass(this).widthSizeClass ... } } }

Slide 31

Slide 31 text

WindowSizeClass Implementation val navigationType: ReplyNavigationType //enum class when (widthSizeClass) { WindowWidthSizeClass.Compact -> { navigationType = ReplyNavigationType.BOTTOM_NAVIGATION } WindowWidthSizeClass.Medium -> { navigationType = ReplyNavigationType.NAVIGATION_RAIL } WindowWidthSizeClass.Expanded -> { navigationType = ReplyNavigationType.PERMANENT_NAVIGATION_DRAWER } else -> { navigationType = ReplyNavigationType.BOTTOM_NAVIGATION } }

Slide 32

Slide 32 text

Navigation Visibility Logic if (navigationType == PERMANENT_NAVIGATION_DRAWER) { PermanentNavigationDrawer(...) { ReplyAppContent(navigationType) } } else { ModalNavigationDrawer(...) { ReplyAppContent(navigationType)} } ... @Composable fun ReplyAppContent(navigationType: ReplyNavigationType) { Row(...) { AnimatedVisibility(visible = navigationType == NAVIGATION_RAIL) { NavigationRail() {...} } Column(...) { ListOnlyContent(...) AnimatedVisibility(visible = navigationType == BOTTOM_NAVIGATION) { NavigationBar() {...} } }

Slide 33

Slide 33 text

Composable Preview

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

List-Detail in Compose 03

Slide 36

Slide 36 text

ContentType Classification val contentType: ReplyContentType //enum class contentType = if (widthSizeClass == WindowWidthSizeClass.Expanded) { ReplyContentType.LIST_AND_DETAIL } else { ReplyContentType.LIST_ONLY }

Slide 37

Slide 37 text

Previous Logic @Composable fun ReplyAppContent(navigationType: ReplyNavigationType) { Row(...) { AnimatedVisibility(visible = navigationType == NAVIGATION_RAIL) { NavigationRail() {...} } Column(...) { ListOnlyContent(...) AnimatedVisibility(visible = navigationType == BOTTOM_NAVIGATION) { NavigationBar() {...} } } } }

Slide 38

Slide 38 text

Additional Content Type Logic if (contentType == ReplyContentType.LIST_AND_DETAIL) { Row { ListOnlyContent() DetailContent(selectedItemId) } } else { if (replyHomeUIState.isShowingHomepage) { ListOnlyContent() } else { DetailContent(selectedItemId) } }

Slide 39

Slide 39 text

Screen State Logic in ViewModel //ketika item pada list dipilih fun updateDetailsScreenStates(email: Email) { _uiState.update { it.copy( currentSelectedEmail = email, isShowingHomepage = false ) } }

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

Wait a moment!

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

Navigation Component vs Manual Navigation

Slide 44

Slide 44 text

BackHandler if (contentType == ReplyContentType.LIST_AND_DETAIL) { ... } else { if (selectedItemId != null) { DetailContent(selectedItemId) BackHandler { /* handle returning to ListOnlyContent() */ } } else { ListOnlyContent() } }

Slide 45

Slide 45 text

Additional Screen State Logic in ViewModel fun updateDetailsScreenStates(email: Email) { _uiState.update { it.copy( currentSelectedEmail = email, isShowingHomepage = false ) } } fun resetHomeScreenStates() { _uiState.update { it.copy( currentSelectedEmail = it.currentSelectedEmail, isShowingHomepage = true ) } }

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

Is everything okay now?

Slide 48

Slide 48 text

Foldable Device 04

Slide 49

Slide 49 text

Foldable Emulator *Use Microsoft Surface Duo emulator for dual screen emulator

Slide 50

Slide 50 text

Device Posture windowLayoutInfo ● state() HALF_OPENED/FLAT ● orientation() VERTICAL / HORIZONTAL ● occlusionType() FULL/NONE ● isSeparating() ● bounds.rect() Book TableTop Tent Normal

Slide 51

Slide 51 text

Sample Case

Slide 52

Slide 52 text

Device Posture Classification val devicePostureFlow = WindowInfoTracker.getOrCreate(this).windowLayoutInfo(this) .flowWithLifecycle(this.lifecycle) .map { layoutInfo -> val foldingFeature = layoutInfo.displayFeatures .filterIsInstance().firstOrNull() when { isTableTopPosture(foldingFeature) -> DevicePosture.TableTopPosture else -> DevicePosture.NormalPosture } } .stateIn( scope = lifecycleScope, started = SharingStarted.Eagerly, initialValue = DevicePosture.NormalPosture //enum class )

Slide 53

Slide 53 text

Device Posture Decision fun isTableTopPosture(foldFeature: FoldingFeature?): Boolean { return foldFeature?.state == FoldingFeature.State.HALF_OPENED && foldFeature.orientation == FoldingFeature.Orientation.HORIZONTAL } fun isBookPosture(foldFeature: FoldingFeature?): Boolean { return foldFeature?.state == FoldingFeature.State.HALF_OPENED && foldFeature.orientation == FoldingFeature.Orientation.VERTICAL } fun isSeparating(foldFeature: FoldingFeature?): Boolean { return foldFeature?.state == FoldingFeature.State.FLAT && foldFeature.isSeparating }

Slide 54

Slide 54 text

if (devicePosture == DevicePosture.TableTopPosture) { PlayerContentTableTop() } else { PlayerContentRegular() }

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Large Screen App Quality 05

Slide 57

Slide 57 text

Quality Tiers ● Tier 3 (Basic) 😐 — Large screen ready ● Tier 2 (Better) 🙂 — Large screen optimized ● Tier 1 (Best) 😃 — Large screen differentiated

Slide 58

Slide 58 text

Tier 3 (Basic) — Large screen ready ● Full screen ○ But app layout not ideal ● Basic support for external input ○ Keyboard ○ Mouse ○ Trackpad ● Handles configuration changes and retains or restores its state

Slide 59

Slide 59 text

Tier 2 (Better) — Large screen optimized ● Layout Optimization ○ Leading edge navigation ○ Canonical Layout ○ Scaled Grid layouts ● Enhanced support for external input ○ Shortcut ○ Action by keyboard ○ Zooming using mouse ○ Hover

Slide 60

Slide 60 text

Responsive UI elements

Slide 61

Slide 61 text

Tier 3 (Best) — Large screen differentiated ● Multitasking ● Foldable posture ● Drag & Drop ● Stylus input ● Picture-in-picture mode

Slide 62

Slide 62 text

Dialog Box Placement

Slide 63

Slide 63 text

Test Your App // Checks start_layout is on the left of end_layout with a vertical folding feature. @Test fun testDeviceOpen_Vertical() { activityRule.scenario.onActivity { activity -> val feature = FoldingFeature( activity = activity, state = HALF_OPENED, orientation = VERTICAL) val expected = TestWindowLayoutInfo(listOf(feature)) publisherRule.overrideWindowLayoutInfo(expected) } onView(withId(R.id.start_layout)) .check(isCompletelyLeftOf(withId(R.id.end_layout))) }

Slide 64

Slide 64 text

Further Learning

Slide 65

Slide 65 text

https://m3.material.io/foundations/adaptive-design/canonical-layouts https://developer.android.com/large-screens/gallery https://www.youtube.com/watch?v=Z4h9cvlE66E https://github.com/android/user-interface-samples/tree/main/CanonicalLayouts https://codelabs.developers.google.com/jetpack-compose-adaptability https://www.youtube.com/watch?v=LTLQhC6VadI https://android-developers.googleblog.com/2021/05/whats-new-in-foldables-tablets-and.html https://android-developers.googleblog.com/2022/11/reach-your-users-on-large-screens.html https://developer.android.com/guide/topics/large-screens/large-screen-cookbook Reference

Slide 66

Slide 66 text

Bukan hanya otak dan otot aja yang perlu adaptive, tapi juga UI~ @arif_faizin, 2023

Slide 67

Slide 67 text

Thank you Deck will be available at https://speakerdeck.com/arifaizin