### Assist Chip Layout Example
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/Chip.md
Example of an activated assist chip in a layout. Ensure minimum touch target size for accessibility.
```xml
```
--------------------------------
### Floating Toolbar Layout Example
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/FloatingToolbar.md
This example demonstrates how to implement a vibrant floating toolbar at the bottom of the screen. It includes an icon button with a bold style.
```xml
...
```
--------------------------------
### Start Activity with Transition Options
Source: https://github.com/material-components/material-components-android/blob/master/docs/theming/Motion.md
Initiate navigation to another Activity with transition options using `ActivityOptions.makeSceneTransitionAnimation`.
```kotlin
val bundle = ActivityOptions.makeSceneTransitionAnimation(this).toBundle()
startActivity(Intent(this, ActivityB::class.java), bundle)
```
--------------------------------
### Install Catalog App
Source: https://github.com/material-components/material-components-android/blob/master/docs/catalog-app.md
Run this Gradle command to install the Material Components Catalog app on your device.
```sh
./gradlew :catalog:installDebug
```
--------------------------------
### Deprecated SwitchMaterial Layout Example
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/Switch.md
Example demonstrating a list of five SwitchMaterial components in a layout, including checked and disabled states.
```xml
```
--------------------------------
### Material Button Theming Example
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/CommonButton.md
Illustrates Material theming applied to text, outlined, and filled button types. This example showcases how to customize button appearance through theming attributes.
```xml
```
--------------------------------
### Split button XML layout example
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/SplitButton.md
Example of how to define a split button in an XML layout. Ensure you are using a Material Components theme for auto-inflation.
```xml
```
--------------------------------
### FAB Layout Example
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/FloatingActionButton.md
Use this XML layout to implement a regular FAB with a plus icon. Ensure the `app:srcCompat` attribute points to your drawable resource.
```xml
```
--------------------------------
### Configure Enter Transition for Fragment B
Source: https://github.com/material-components/material-components-android/blob/master/docs/theming/Motion.md
Sets up a MaterialFadeThrough transition for a Fragment's entry. This transition is triggered when the Fragment appears and requires no specific setup beyond assignment.
```kotlin
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enterTransition = MaterialFadeThrough()
}
```
--------------------------------
### Start Container Transform Transition
Source: https://github.com/material-components/material-components-android/blob/master/docs/theming/Motion.md
Initiate the container transform by creating an Intent for the target activity and using `ActivityOptions.makeSceneTransitionAnimation` with the shared element and its transition name.
```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())
```
--------------------------------
### Outlined MaterialCardView Example
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/Card.md
An example of an outlined MaterialCardView. This style includes a visual boundary around the card for greater emphasis. It demonstrates a common layout with media, text, and action buttons.
```xml
```
--------------------------------
### Start date picker in text input mode
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/DatePicker.md
Configure the date picker to open in text input mode by default.
```kotlin
MaterialDatePicker.Builder.datePicker()
...
.setInputMode(MaterialDatePicker.INPUT_MODE_TEXT)
```
--------------------------------
### Small FAB layout example
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/FloatingActionButton.md
Use `?attr/floatingActionButtonSmallStyle` to define a small FAB, suitable for smaller screens or visual continuity. Place it within a `CoordinatorLayout` for integrated behaviors.
```xml
```
--------------------------------
### Set first day of the week
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/DatePicker.md
Customize the displayed first day of the week in the calendar picker. This example sets Monday as the first day.
```kotlin
val constraintsBuilder =
CalendarConstraints.Builder()
.setFirstDayOfWeek(Calendar.MONDAY)
```
--------------------------------
### Bottom Navigation Menu Resource with Four Items
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/BottomNavigation.md
An example menu resource defining four items for a bottom navigation bar. Ensure icons and string resources are correctly defined.
```xml
```
--------------------------------
### Add start icon to TextInputLayout
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/TextField.md
Use app:startIconDrawable and app:startIconContentDescription to add a leading icon to a text field.
```xml
...
```
--------------------------------
### Setup TabLayout with ViewPager
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/Tabs.md
Connect a TabLayout with a ViewPager to synchronize tab selections and page swipes. This method should be called after setting the adapter on the ViewPager.
```kotlin
tabLayout.setupWithViewPager(viewPager)
```
--------------------------------
### Docked Toolbar Layout Example
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/DockedToolbar.md
This XML layout demonstrates how to integrate a DockedToolbarLayout within a CoordinatorLayout. It includes a NestedScrollView for content and configures the DockedToolbarLayout with a HideViewOnScrollBehavior. The toolbar itself contains an OverflowLinearLayout with sample buttons.
```xml
```
--------------------------------
### Configure Exit Transition for Fragment A
Source: https://github.com/material-components/material-components-android/blob/master/docs/theming/Motion.md
Sets up a MaterialFadeThrough transition for a Fragment's exit. This transition is triggered when the Fragment disappears and requires no specific setup beyond assignment.
```kotlin
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
exitTransition = MaterialFadeThrough()
}
```
--------------------------------
### Standard Side Sheet Basic Usage in XML
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/SideSheet.md
This XML layout demonstrates the basic setup for a standard side sheet within a CoordinatorLayout. Ensure the correct layout_behavior is applied.
```xml
```
--------------------------------
### Wrap Context for Dynamic Colors
Source: https://github.com/material-components/material-components-android/blob/master/docs/theming/Color.md
Use this helper method to get a context with the dynamic color theme overlay applied, if available on the device. Ensure M3 base themes are applied first.
```java
context = DynamicColors.wrapContextIfAvailable(context);
```
--------------------------------
### Outlined Text Field Code Interaction
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/TextField.md
Provides Kotlin code examples for interacting with an outlined text field. Use `editText?.text.toString()` to get the input and `doOnTextChanged` to observe changes.
```kotlin
// Get input text
val inputText = outlinedTextField.editText?.text.toString()
outlinedTextField.editText?.doOnTextChanged { inputText, _, _, _ ->
// Respond to input text change
}
```
--------------------------------
### Instantiating and Configuring MaterialTimePicker
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/TimePicker.md
Demonstrates how to create a MaterialTimePicker instance using its builder, setting the time format, initial hour and minute, and a title. It also shows how to determine the system's 24-hour format and apply it.
```APIDOC
## Instantiating and Configuring MaterialTimePicker
### Description
This section shows how to create a `MaterialTimePicker` using `MaterialTimePicker.Builder`, allowing configuration of time format, initial time, and title. It also includes logic to adapt to the system's 24-hour format.
### Method
N/A (Code examples for instantiation)
### Endpoint
N/A
### Parameters
#### Request Body
N/A
### Request Example
```kt
// Example using 12-hour format
val picker12Hour = MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_12H)
.setHour(12)
.setMinute(10)
.setTitleText("Select Appointment time")
.build()
// Example adapting to system's 24-hour format
val isSystem24Hour = is24HourFormat(this)
val clockFormat = if (isSystem24Hour) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H
val pickerSystemFormat = MaterialTimePicker.Builder()
.setTimeFormat(clockFormat)
.setHour(14)
.setMinute(30)
.setTitleText("Set Event Time")
.build()
```
### Response
N/A
### Response Example
N/A
```
--------------------------------
### Configure Activity Enter Transition
Source: https://github.com/material-components/material-components-android/blob/master/docs/theming/Motion.md
Configure a `MaterialFadeThrough` enter transition for Activity B, setting `allowEnterTransitionOverlap` to true to play transitions concurrently.
```kotlin
// ActivityB.kt
override fun onCreate(savedInstanceState: Bundle?) {
window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)
val enter = MaterialFadeThrough().apply {
addTarget(R.id.b_container)
}
window.enterTransition = enter
// 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)
...
}
```
--------------------------------
### Navigation Icon Drawable
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/BottomAppBar.md
Example of a vector drawable for the navigation icon, specifying tint color. This is a minimal example; actual drawables may contain more path data.
```xml
...
```
--------------------------------
### Configure Activity Enter Transition
Source: https://github.com/material-components/material-components-android/blob/master/docs/theming/Motion.md
Configure a `MaterialSharedAxis` enter transition for Activity B. Ensure `window.allowEnterTransitionOverlap` is set to true to play enter and exit transitions concurrently.
```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)
...
}
```
--------------------------------
### Basic MaterialAlertDialogBuilder Example
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/Dialog.md
Use this snippet to create a basic dialog with a title, supporting text, and up to three action buttons (neutral, negative, positive). Ensure the `context` is available and strings are properly defined.
```kotlin
MaterialAlertDialogBuilder(context)
.setTitle(resources.getString(R.string.title))
.setMessage(resources.getString(R.string.supporting_text))
.setNeutralButton(resources.getString(R.string.cancel)) {
dialog, which ->
// Respond to neutral button press
}
.setNegativeButton(resources.getString(R.string.decline)) {
dialog, which ->
// Respond to negative button press
}
.setPositiveButton(resources.getString(R.string.accept)) {
dialog, which ->
// Respond to positive button press
}
.show()
```
--------------------------------
### Get Elevation Overlay Color with Theme Surface Color
Source: https://github.com/material-components/material-components-android/blob/master/docs/theming/Dark.md
Use `ElevationOverlayProvider#compositeOverlayWithThemeSurfaceColorIfNeeded` to get the `colorSurface` blended with the elevation overlay color. This method handles theme-level elevation overlay enablement.
```java
elevationOverlayProvider.compositeOverlayWithThemeSurfaceColorIfNeeded(context, color, elevation)
```
--------------------------------
### Get User Selection
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/TimePicker.md
Retrieve the selected hour and minute from the time picker after the user has made a selection.
```kotlin
picker.minute
picker.hour
```
--------------------------------
### Fragment A: Show FAB with MaterialFade
Source: https://github.com/material-components/material-components-android/blob/master/docs/theming/Motion.md
Demonstrates transitioning a Floating Action Button (FAB) into view using MaterialFade with a duration of 150ms.
```kotlin
showButton.setOnClickListener {
val materialFade = MaterialFade().apply {
duration = 150L
}
TransitionManager.beginDelayedTransition(container, materialFade)
fab.visibility = View.VISIBLE
}
```
--------------------------------
### Instantiate Material Time Picker
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/TimePicker.md
Use MaterialTimePicker.Builder to create a time picker instance. Configure time format, initial hour, minute, and title text.
```kotlin
val picker =
MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_12H)
.setHour(12)
.setMinute(10)
.setTitleText("Select Appointment time")
.build()
```
--------------------------------
### Material Button Theming Example
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/ButtonGroup.md
Demonstrates Material Theming for Material Buttons, showcasing text, outlined, and filled button types with custom color and shape. Refer to MaterialButton documentation for API details.
```xml
```
--------------------------------
### Get user selection from date picker
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/DatePicker.md
Retrieve the date or date range selected by the user from the picker instance.
```kotlin
picker.selection
```
--------------------------------
### Deprecated SwitchMaterial Code Example
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/Switch.md
Kotlin code snippets for checking the state of a SwitchMaterial and listening for checked/unchecked state changes.
```kotlin
// To check a switch
switchmaterial.isChecked = true
// To listen for a switch's checked/unchecked state changes
switchmaterial.setOnCheckedChangeListener { buttonView, isChecked
// Responds to switch being checked/unchecked
}
```
--------------------------------
### List Popup Window Menu Implementation in Kotlin
Source: https://github.com/material-components/material-components-android/blob/master/docs/components/Menu.md
This code demonstrates how to create and display a list popup window menu when a button is clicked. It sets up the anchor view, adapter, and item click listener.
```kotlin
val listPopupWindowButton = view.findViewById