### 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
```
--------------------------------
### Enable Activity transitions per Activity
Source: https://material.io/develop/android/theming/motion
Enable Activity transitions on an Activity-by-Activity basis by requesting the Window.FEATURE_ACTIVITY_TRANSITIONS feature in onCreate. This is an alternative to setting the theme attribute.
```kotlin
override fun onCreate(savedInstanceState: Bundle?) {
window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)
...
}
```
--------------------------------
### Clone the Material Components for Android repository
Source: https://material.io/develop/android/docs/building-from-source
Clones the material-components-android repository from GitHub. This is the first step to building the library from source.
```bash
git clone https://github.com/material-components/material-components-android.git
```
--------------------------------
### Transition between Views with MaterialFadeThrough
Source: https://material.io/develop/android/theming/motion
Transition between two Views in a layout using MaterialFadeThrough and TransitionManager.beginDelayedTransition. The outgoing View should be visible and the incoming View should be View.GONE or not yet added to the layout.
```kotlin
val fadeThrough = MaterialFadeThrough()
```
```kotlin
```
```kotlin
// Begin watching for changes in the View hierarchy.
```
```kotlin
TransitionManager.beginDelayedTransition(container, fadeThrough)
```
```kotlin
```
```kotlin
// Make any changes to the hierarchy to be animated by the fade through transition.
```
```kotlin
outgoingView.visibility = View.GONE
```
```kotlin
incomingView.visibility = View.VISIBLE
```
--------------------------------
### Configure exit transition in Activity A
Source: https://material.io/develop/android/theming/motion
Configure a MaterialSharedAxis exit transition in Activity A and set it as the window's exitTransition. Use addTarget to limit the transition to a specific container.
```kotlin
// ActivityA.kt
override fun onCreate(savedInstanceState: Bundle?) {
val exit = MaterialSharedAxis(MaterialSharedAxis.X, true).apply {
// Only run the transition on the contents of this activity, excluding
// system bars or app bars if provided by the app’s theme.
addTarget(R.id.a_container)
}
window.exitTransition = exit
// TODO: Add a reenter transition in the backwards direction to animate
// ActivityB out and ActivityA back in in the opposite direction.
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_a)
}
```
--------------------------------
### Configure MaterialFadeThrough for Fragment transitions
Source: https://material.io/develop/android/theming/motion
Configure exit and enter MaterialFadeThrough transitions in Fragment A and Fragment B respectively. Both transitions are used when navigating between the fragments.
```kotlin
// FragmentA.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
exitTransition = MaterialFadeThrough()
}
```
```kotlin
// FragmentB.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enterTransition = MaterialFadeThrough()
}
```
--------------------------------
### Transition between views with MaterialSharedAxis
Source: https://material.io/develop/android/theming/motion
Transition between two views in the same layout using a MaterialSharedAxis. Call TransitionManager.beginDelayedTransition before changing visibility, then set the outgoing view to GONE and the incoming view to VISIBLE.
```kotlin
// Set up a new MaterialSharedAxis in the specified axis and direction.
val sharedAxis = MaterialSharedAxis(MaterialSharedAxis.Y, true)
// Begin watching for changes in the View hierarchy.
TransitionManager.beginDelayedTransition(container, sharedAxis)
// Make any changes to the hierarchy to be animated by the shared axis transition.
outgoingView.visibility = View.GONE
incomingView.visibility = View.VISIBLE
```
--------------------------------
### Theme all switches with Material Theme attributes
Source: https://material.io/develop/android/components/switches
Use this style in res/values/styles.xml to theme all switches and other components. It sets the colorOnSurface and colorSecondary attributes to the shrine pink colors.
```xml
```
--------------------------------
### Implement an outlined text field via XML
Source: https://material.io/develop/android/docs/getting-started
An alternative text field style using the Material Components outlined style.
```xml
```
--------------------------------
### SwitchMaterial layout for five switches
Source: https://material.io/develop/android/components/switches
Layout for a list of five SwitchMaterial switches. The first is checked by default; the last is disabled. Each switch uses wrap_content width and match_parent height.
```xml
```
```xml
```
```xml
```
```xml
```
```xml
```
--------------------------------
### Activity A: configure exit shared element transition
Source: https://material.io/develop/android/theming/motion
Configure Activity A for an exit shared element transition. Enable Activity Transitions, attach a MaterialContainerTransformSharedElementCallback, and keep system bars persistent. Optionally enable transitions in the theme with true.
```kotlin
override fun onCreate(savedInstanceState: Bundle?) {
// Enable Activity Transitions. Optionally enable Activity transitions in your
// theme with true.
window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)
// Attach a callback used to capture the shared elements from this Activity to be used
// by the container transform transition
setExitSharedElementCallback(MaterialContainerTransformSharedElementCallback())
// Keep system bars (status bar, navigation bar) persistent throughout the transition.
window.sharedElementsUseOverlay = false
super.onCreate(bundle)
setContentView(R.layout.activity_a)
...
}
```
--------------------------------
### Configure MaterialSharedAxis Z transitions for Fragment A and B
Source: https://material.io/develop/android/theming/motion
Configure MaterialSharedAxis transitions for Fragment A and Fragment B. Fragment A uses exitTransition with forward=true and reenterTransition with forward=false; Fragment B uses enterTransition with forward=true and returnTransition with forward=false. These settings ensure both fragments move in the same direction when transitioning forward and backward.
```kotlin
// FragmentA.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
exitTransition = MaterialSharedAxis(MaterialSharedAxis.Z, /* forward= */ true)
reenterTransition = MaterialSharedAxis(MaterialSharedAxis.Z, /* forward= */ false)
}
```
```kotlin
// FragmentB.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enterTransition = MaterialSharedAxis(MaterialSharedAxis.Z, /* forward= */ true)
returnTransition = MaterialSharedAxis(MaterialSharedAxis.Z, /* forward= */ false)
}
```
--------------------------------
### Add or exclude transition targets
Source: https://material.io/develop/android/theming/motion
Optionally add or exclude targets to control which Views the transition affects. Use addTarget to include only specific views, or excludeTarget to exclude system bars.
```kotlin
val exit = MaterialSharedAxis(MaterialSharedAxis.X, true).apply {
// Only run the transition on the root ViewGroup of this activity. This will exclude
// other views except what is specified by this method.
addTarget(R.id.a_container)
// OR
// Run the transition on everything except the system status and navigation bars. All
// other Views, besides those explicitly excluded, will be affected by the transition.
excludeTarget(android.R.id.statusBarBackground, true)
excludeTarget(android.R.id.navigationBarBackground, true)
}
```
--------------------------------
### Enable Activity transitions via theme (android:windowActivityTransitions)
Source: https://material.io/develop/android/theming/motion
Enable Activity transitions by setting android:windowActivityTransitions to true in your theme. This is one of two ways to enable Activity transitions; the other is setting the Window.FEATURE_ACTIVITY_TRANSITIONS flag in an Activity.
```xml
```
--------------------------------
### Navigate between Fragments with supportFragmentManager
Source: https://material.io/develop/android/theming/motion
Use a standard Fragment transaction to navigate from Fragment A to Fragment B. The MaterialFadeThrough transitions configured in the fragments will animate the change.
```kotlin
supportFragmentManager
.beginTransaction()
.replace(R.id.fragment_container, FragmentB())
.commit()
```
--------------------------------
### Add and remove BottomSheetCallback for state and slide changes
Source: https://material.io/develop/android/components/bottom-sheet-dialog-fragment
Creates a BottomSheetCallback to listen for state and slide changes, then adds or removes it from the BottomSheetBehavior. The onStateChanged method receives the new state, and onSlide receives the slide offset.
```kotlin
val bottomSheetCallback = object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
// Do something for new state.
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {
// Do something for slide offset.
}
}
```
```kotlin
// To add the callback:
bottomSheetBehavior.addBottomSheetCallback(bottomSheetCallback)
```
```kotlin
// To remove the callback:
bottomSheetBehavior.removeBottomSheetCallback(bottomSheetCallback)
```
--------------------------------
### Transition a View with MaterialFade (show/hide FAB)
Source: https://material.io/develop/android/theming/motion
Shows how to trigger a MaterialFade transition when showing or hiding a Floating Action Button. The first block sets the duration to 150L for the incoming transition; the second uses 84L for the outgoing transition. Both call TransitionManager.beginDelayedTransition(container, materialFade) before changing the view's visibility.
```kotlin
// FragmentA.kt
showButton.setOnClickListener {
val materialFade = MaterialFade().apply {
duration = 150L
}
TransitionManager.beginDelayedTransition(container, materialFade)
fab.visibility = View.VISIBLE
}
```
```kotlin
// FragmentA.kt
hideButton.setOnClickListener {
val materialFade = MaterialFade().apply {
duration = 84L
}
TransitionManager.beginDelayedTransition(container, materialFade)
fab.visibility = View.GONE
}
```
--------------------------------
### Add Google's Maven Repository to build.gradle
Source: https://material.io/develop/android/docs/getting-started
Add the google() repository to your project's build.gradle repositories section to access Material Components for Android.
```groovy
allprojects {
repositories {
google()
jcenter()
}
}
```
--------------------------------
### Theme all switches with switchStyle and theme overlay
Source: https://material.io/develop/android/components/switches
Use these styles in res/values/styles.xml to theme all switches without affecting other components. The switchStyle attribute points to a custom widget style that uses a theme overlay to set the switch colors.
```xml
```
--------------------------------
### Checkbox theming via checkboxStyle and ThemeOverlay
Source: https://material.io/develop/android/components/checkboxes
Themes all checkboxes without affecting other components by using checkboxStyle, a custom widget style, and a theme overlay.
```xml
```
```xml
```
```xml
```
--------------------------------
### Configure MaterialFadeThrough exit transition in Activity A
Source: https://material.io/develop/android/theming/motion
Configure a MaterialFadeThrough in Activity A and set it as the Activity's exitTransition. The transition targets only the contents of the activity, excluding system bars or app bars if provided by the theme.
```kotlin
// ActivityA.kt
```
```kotlin
```
```kotlin
override fun onCreate(savedInstanceState: Bundle?) {
```
```kotlin
```
```kotlin
val exit = MaterialFadeThrough().apply {
```
```kotlin
```
```kotlin
// Only run the transition on the contents of this activity, excluding
```
```kotlin
// system bars or app bars if provided by the app’s theme.
```
```kotlin
addTarget(R.id.a_container)
```
```kotlin
}
```
```kotlin
window.exitTransition = exit
```
```kotlin
```
```kotlin
super.onCreate(savedInstanceState)
```
```kotlin
setContentView(R.layout.activity_a)
```
```kotlin
}
```
--------------------------------
### XML layout for five CheckBoxes
Source: https://material.io/develop/android/components/checkboxes
Layout for a list of five checkboxes. The first is checked by default, the last is disabled. Each uses match_parent dimensions and references string resources for labels.
```xml
```
```xml
```
```xml
```
```xml
```
```xml
```