Material Design is a design language developed by Google in 2014. It aims to create a unified and consistent user experience across various platforms, devices, and input methods. Android Material Design Components (MDC) is a set of components and styles that help developers build Android applications in line with Material Design guidelines.
This article provides an overview of Android Material Design Components and demonstrates how to use them effectively in your Android applications.
Material Design Components (MDC) is a library that provides developers with pre-built UI components that adhere to Material Design principles. These components are designed to be adaptive, responsive, and flexible, making it easier to create professional and visually appealing Android applications.
Some of the key advantages of using MDC in your Android applications include:
To start using Material Design Components in your Android project, follow these steps:
build.gradle
file:dependencies {
implementation 'com.google.android.material:material:1.4.0'
}
AndroidManifest.xml
file, set your app theme to inherit from a Material Components theme:<application
...
android:theme="@style/Theme.MaterialComponents.DayNight">
res/values/styles.xml
that extends a Material Components theme:<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<!-- Customize your theme here. -->
</style>
MDC provides several types of buttons, including:
<!-- Text Button -->
<Button
android:id="@+id/text_button"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Button" />
<!-- Outlined Button -->
<Button
android:id="@+id/outlined_button"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Outlined Button" />
<!-- Contained Button -->
<Button
android:id="@+id/contained_button"
style="@style/Widget.MaterialComponents.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contained Button" />
MDC provides a variety of text field styles, including filled and outlined text fields.
<!-- Filled Text Field -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/filled_text_field"
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Filled Text Field" />
</com.google.android.material.textfield.TextInputLayout>
<!-- Outlined Text Field -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/outlined_text_field"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="OutlinedText Field" />
</com.google.android.material.textfield.TextInputLayout>
Cards are a versatile component that can be used to display content and actions related to a single subject. MDC provides a MaterialCardView
component that extends the default CardView
.
<com.google.android.material.card.MaterialCardView
android:id="@+id/material_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="4dp"
app:cardCornerRadius="8dp">
<!-- Card content goes here -->
</com.google.android.material.card.MaterialCardView>
App bars provide a dedicated space for app branding, navigation, and actions. MDC provides a Toolbar
component that can be customized with Material Design styles.
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="App Title" />
</com.google.android.material.appbar.AppBarLayout>
MDC provides several components for implementing navigation within your app, including Bottom Navigation, Navigation Drawer, and Tabs.
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_navigation_menu" />
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Main content goes here -->
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/navigation_drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 1" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 2" />
</com.google.android.material.tabs.TabLayout>
Snackbars provide lightweight feedback about an operation in the form of a brief message at the bottom of the screen.
Snackbar.make(view, "Snackbar message", Snackbar.LENGTH_SHORT).show();
MDC supports powerful theming capabilities that allow you to easily customize the look and feel of your app. You can define a custom theme by extending a Material Components theme and overriding the desired attributes.
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<!-- Primary color -->
<item name="colorPrimary">@color/colorPrimary</item>
<!-- Primary dark color -->
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<!-- Accent color -->
<item name="colorAccent">@color/colorAccent</item>
<!-- Default button background color -->
<item name="materialButtonStyle">@style/MyButtonStyle</item>
</style>
<style name="MyButtonStyle" parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">@color/buttonBackground</item>
<item name="android:textColor">@color/buttonText</item>
</style>
Android Material Design Components provide a powerful and flexible set of pre-built UI components that adhere to Material Design guidelines. By using MDC in your Android applications, you can create consistent, visually appealing, and accessible user experiences with ease. In this article, we’ve explored key MDC components and their usage, as well as theming capabilities that enable you to customize your app’s look and