### Start Activity B with container transform Source: https://material.io/develop/android/theming/motion Start the container transform from Activity A by constructing an Intent with ActivityOptions.makeSceneTransitionAnimation, passing the start view and the transition name that matches Activity B. ```kotlin val intent = Intent(this, ActivityB::class.java) val options = ActivityOptions.makeSceneTransitionAnimation( this, startView, "shared_element_container" // The transition name to be matched in Activity B. ) startActivity(intent, options.toBundle()) ``` -------------------------------- ### Start Activity B with transition options Source: https://material.io/develop/android/theming/motion Start Activity B with the ActivityOptions bundle to trigger the shared axis transition. Pass the bundle to startActivity as the second argument. ```kotlin val bundle = ActivityOptions.makeSceneTransitionAnimation(this).toBundle() startActivity(Intent(this, ActivityB::class.java), bundle) ``` -------------------------------- ### Run the catalog app with Gradle Source: https://material.io/develop/android/docs/catalog-app Use this Gradle command to install the catalog app on a connected device or emulator. This is an alternative to running the catalog module in Android Studio. ```bash ./gradlew :catalog:installDebug ``` -------------------------------- ### Add matching transitionName to start and end Views in fragment_a.xml and fragment_b.xml Source: https://material.io/develop/android/theming/motion In Fragment A and Fragment B's layouts, identify the start and end Views and add a matching transitionName to each. The start View in fragment_a.xml and the end View in fragment_b.xml both use the transitionName "shared_element_container". There cannot be more than a 1:1 mapping of transitionNames between the start and end layouts. ```xml ``` ```xml ``` ```xml ``` ```xml ``` -------------------------------- ### Activity A layout: start view with transitionName Source: https://material.io/develop/android/theming/motion In Activity A's layout, give the start view a transitionName that will be used as the shared element. The transition name must match the one used in Activity B. ```xml ``` ```xml ``` -------------------------------- ### Example of correct color resource naming Source: https://material.io/develop/android/theming/color Shows the recommended pattern of naming color resources with literal names like brand_blue and brand_grey, which are relevant to the RGB value. ```xml ... ... ``` -------------------------------- ### Activity B: configure enter and return transitions Source: https://material.io/develop/android/theming/motion Configure Activity B for transitions. Set the transition name on the root view to match Activity A's start view, attach a MaterialContainerTransformSharedElementCallback, and set the enter and return transitions to MaterialContainerTransform with durations 300L and 250L. The root view (android.R.id.content) is used as the shared element container. ```kotlin override fun onCreate(savedInstanceState: Bundle?) { // Enable Activity Transitions. Optionally enable Activity transitions in your // theme with true. window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS) // Set the transition name, which matches Activity A’s start view transition name, on // the root view. findViewById(android.R.id.content).transitionName = "shared_element_container" // Attach a callback used to receive the shared elements from Activity A to be // used by the container transform transition. setEnterSharedElementCallback(MaterialContainerTransformSharedElementCallback()) // Set this Activity’s enter and return transition to a MaterialContainerTransform window.sharedElementEnterTransition = MaterialContainerTransform().apply { addTarget(android.R.id.content) duration = 300L } window.sharedElementReturnTransition = MaterialContainerTransform().apply { addTarget(android.R.id.content) duration = 250L } super.onCreate(bundle) setContentView(R.layout.activity_b) ... } ``` -------------------------------- ### Customize MaterialFadeThrough with secondaryAnimatorProvider Source: https://material.io/develop/android/theming/motion Two FragmentA.kt examples showing how to customize MaterialFadeThrough transitions. The first removes the secondary animator provider so the exit transition simply fades out. The second replaces the secondary provider with a SlideDistanceProvider(Gravity.BOTTOM) so the enter transition slides in from the bottom. ```kotlin // FragmentA.kt override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) exitTransition = MaterialFadeThrough().apply { // Remove the exit fade through's secondary scale so this Fragment simply fades out. secondaryAnimatorProvider = null } } ``` ```kotlin // FragmentA.kt override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enterTransition = MaterialFadeThrough().apply { // Replace the enter fade through's secondary animator provider with a SlideDistanceProvider. secondaryAnimatorProvider = SlideDistanceProvider(Gravity.BOTTOM) } } ``` -------------------------------- ### Example of incorrect color resource naming Source: https://material.io/develop/android/theming/color Shows the pattern of naming color resources with slot names like color_primary and color_surface, which the documentation advises against. ```xml ... ... ``` -------------------------------- ### Use Navigation Architecture Component with FragmentNavigatorExtras Source: https://material.io/develop/android/theming/motion When using the Navigation Architecture Component, map the start View in FragmentA and the transitionName of the end View in FragmentB with FragmentNavigatorExtras, then navigate using findNavController().navigate(). ```kotlin // Map the start View in FragmentA and the transitionName of the end View in FragmentB ``` ```kotlin val extras = FragmentNavigatorExtras(view to "shared_element_container") ``` ```kotlin findNavController().navigate(R.id.action_fragmentA_to_fragmentB, null, null, extras) ``` -------------------------------- ### MaterialTextView layout XML example Source: https://material.io/develop/android/supporting/text-view Layout XML for including a TextView in an Android layout. When using the full Material themes, TextView auto-inflates to MaterialTextView; otherwise you must specify in the XML. ```xml ``` -------------------------------- ### Trigger MaterialContainerTransform between Views Source: https://material.io/develop/android/theming/motion Sets up a MaterialContainerTransform between two Views (fab and bottomToolbar) and begins the transition with TransitionManager.beginDelayedTransition. The transform uses MaterialArcMotion for a curved path and removes the scrim by setting scrimColor to Color.TRANSPARENT. To return, reverse the start and end views and undo the visibility changes. ```kotlin val transform = MaterialContainerTransform().apply { // Manually tell the container transform which Views to transform between. startView = fab endView = bottomToolbar // Ensure the container transform only runs on a single target addTarget(endView) // Optionally add a curved path to the transform pathMotion = MaterialArcMotion() // Since View to View transforms often are not transforming into full screens, // remove the transition's scrim. scrimColor = Color.TRANSPARENT } // Begin the transition by changing properties on the start and end views or // removing/adding them from the hierarchy. TransitionManager.beginDelayedTransition(container, transform) fab.visibility = View.GONE bottomToolbar.visibility = View.VISIBLE ``` -------------------------------- ### Configure MaterialFadeThrough enter transition in Activity B Source: https://material.io/develop/android/theming/motion Configure a MaterialFadeThrough enter transition in Activity B. Set window.enterTransition and allowEnterTransitionOverlap to true so Activity A's exit transition plays at the same time. ```kotlin // ActivityB.kt ``` ```kotlin ``` ```kotlin override fun onCreate(savedInstanceState: Bundle?) { ``` ```kotlin ``` ```kotlin window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS) ``` ```kotlin ``` ```kotlin val enter = MaterialFadeThrough().apply { ``` ```kotlin addTarget(R.id.b_container) ``` ```kotlin } ``` ```kotlin window.enterTransition = enter ``` ```kotlin ``` ```kotlin // Allow Activity A’s exit transition to play at the same time as this Activity’s ``` ```kotlin // enter transition instead of playing them sequentially. ``` ```kotlin window.allowEnterTransitionOverlap = true ``` ```kotlin ``` ```kotlin super.onCreate(savedInstanceState) ``` ```kotlin setContentView(R.layout.activity_b) ``` ```kotlin ... ``` ```kotlin } ``` -------------------------------- ### Configure enter transition in Activity B Source: https://material.io/develop/android/theming/motion Configure a MaterialSharedAxis enter transition in Activity B. Set window.enterTransition and allowEnterTransitionOverlap to true so the exit and enter transitions play simultaneously. ```kotlin // ActivityB.kt override fun onCreate(savedInstanceState: Bundle?) { window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS) val enter = MaterialSharedAxis(MaterialSharedAxis.X, true).apply { addTarget(R.id.b_container) } window.enterTransition = enter // TODO: Configure a return transition in the backwards direction. // Allow Activity A’s exit transition to play at the same time as this Activity’s // enter transition instead of playing them sequentially. window.allowEnterTransitionOverlap = true super.onCreate(savedInstanceState) setContentView(R.layout.activity_b) ... } ``` -------------------------------- ### Add shared element to Fragment transaction with childFragmentManager Source: https://material.io/develop/android/theming/motion Add or replace Fragment B, adding the shared element from your start scene to your Fragment transaction. The start View in FragmentA is mapped to the transitionName of the end View in FragmentB using addSharedElement(view, "shared_element_container"). ```kotlin childFragmentManager ``` ```kotlin .beginTransaction() ``` ```kotlin // Map the start View in FragmentA and the transitionName of the end View in FragmentB ``` ```kotlin .addSharedElement(view, "shared_element_container") ``` ```kotlin .replace(R.id.fragment_container, fragmentB, FragmentB.TAG) ``` ```kotlin .addToBackStack(FragmentB.TAG) ``` ```kotlin .commit() ``` -------------------------------- ### Get BottomSheetBehavior programmatically Source: https://material.io/develop/android/components/bottom-sheet-dialog-fragment Programmatically obtain the BottomSheetBehavior from a standard bottom sheet view to apply behavior attributes in code. ```kotlin val standardBottomSheetBehavior = BottomSheetBehavior.from(standardBottomSheet) // Use this to programmatically apply behavior attributes ``` -------------------------------- ### AppCompat theme with required Material attributes Source: https://material.io/develop/android/docs/getting-started Shows an AppCompat theme with the necessary Material Components attributes added to avoid ThemeEnforcement errors. Includes original AppCompat attributes and new Material attributes. ```xml ``` -------------------------------- ### Get modal bottom sheet behavior programmatically Source: https://material.io/develop/android/components/bottom-sheet-dialog-fragment Programmatically access the BottomSheetBehavior of a modal bottom sheet dialog to apply behavior attributes in code. ```kotlin val modalBottomSheetBehavior = (modalBottomSheet.dialog as BottomSheetDialog).behavior // Use this to programmatically apply behavior attributes ``` -------------------------------- ### Build AARs with Gradle (publishToMavenLocal) Source: https://material.io/develop/android/docs/building-from-source Builds the library's AARs and Maven artifacts and publishes them to the local Maven repository (~/.m2/repository). The source shows an empty placeholder block before this command, which is not included. ```bash ./gradlew publishToMavenLocal ``` -------------------------------- ### Standard bottom sheet with BottomSheetBehavior in CoordinatorLayout Source: https://material.io/develop/android/components/bottom-sheet-dialog-fragment Apply BottomSheetBehavior to a direct child View of CoordinatorLayout to create a persistent bottom sheet. The FrameLayout is the bottom sheet in this example. ```xml