Material Design Components (MDC Android) provide a way for designers and developers to incorporate Material Design into their Android applications. Developed by a team at Google, these components streamline the development process for creating visually appealing and functional Android apps.
If you appreciate the UI elements from Google Material Design Components for Android, then you might be interested in Google Material Design Components (MDC) Buttons. A button is a key UI element used to trigger actions when clicked or tapped. There are four main types of buttons in Google Material Design Components:
Before delving into the implementation of these buttons, let's explore why opting for these material components is advantageous over using ordinary built-in components in Android:
Illustration of a Normal Contained Button's behavior under a dark theme.
If you encounter difficulties with the aforementioned steps, you can refer to the provided image for guidance.
Changing the color scheme of the application is an optional step. To do this:
The colors.xml file contains color values defined like:
Color Name | Color Value |
---|---|
colorPrimary | #0f9d58 |
colorPrimaryDark | #006d2d |
colorAccent | #55cf86 |
If you need visual guidance, refer to the provided image of colors.xml:
In the activity_main.xml
file, we will design Google MDC buttons based on user requirements. There are different styles for these buttons:
Contained buttons are prominent buttons with elevation and fill, signifying primary app actions. The default style is the contained button. Below is the XML code for the contained button:
Ensure to set the style attribute for each button style accordingly.
For instance, a contained button would look like:
Output UI:
Outlined buttons are buttons with medium emphasis. They are used for important actions in an app that are not the primary action. These buttons provide more emphasis than text buttons due to their stroke.
Below is the XML code for the Outlined button:
Text buttons are mainly used for actions that are less pronounced, such as those found in dialogs and cards. In cards, text buttons help to maintain focus on the card content. These buttons are typically used for less important actions.
Below is the XML code for the Text button:
<resources> |
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar"> |
<item name="colorPrimary">@color/colorPrimary</item> |
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> |
<item name="colorAccent">@color/colorAccent</item> |
</style> |
<style name="Widget.App.Button.OutlinedButton.IconOnly" parent="Widget.MaterialComponents.Button.OutlinedButton"> |
<item name="iconPadding">0dp</item> |
<item name="android:insetTop">0dp</item> |
<item name="android:insetBottom">0dp</item> |
<item name="android:paddingLeft">12dp</item> |
<item name="android:paddingRight">12dp</item> |
<item name="android:minWidth">48dp</item> |
<item name="android:minHeight">48dp</item> |
</style> |
</resources> |
Toggle Button with icons: