Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java
  • Android
  • Kotlin
  • Flutter
  • Dart
  • Android with Java
  • Android Studio
  • Android Projects
  • Android Interview Questions
Open In App
Next Article:
Material Design Buttons in Android with Example
Next article icon

Material Design Buttons in Android with Example

Last Updated : 12 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Material Design Components (MDC Android) offers designers and developers a way to implement Material Design in their Android applications. Developed by a core team of engineers and UX designers at Google, these components enable a reliable development workflow to build beautiful and functional Android applications. If you like the way how the UI elements from Google Material Design Components for android which are designed by Google are pretty awesome, then here are some steps that need to be followed to get them, and one of them is Google Material Design Components (MDC) Buttons. A Button is a user interface that is used to perform some action when clicked or tapped. Under the Button category, there are mainly 4 types of buttons in Google material design components:

  1. Contained Button
  2. Outlined Button
  3. Text Button
  4. Toggle Button

Below is a demo of all types of Buttons that we are going to create in this project.

Why MDC Buttons in Android?

Before going to implement all types of Buttons let's understand why choosing these material components over ordinary inbuilt components in Android? Please refer to the following points to understand this.

  1. Choosing these material components saves time and these make the app look more like materially designed, and makes the way for user interactions hassle-free for developers.
  2. If we take the buttons as an example we need to create a ripple as root element in custom_button.xml inside the drawable and then we need to set the background of the button as custom_button.xml, then only it will create the ripple effect for the ordinary button. But in Google MDCs there is no need of creating the manual ripple layout for the buttons.
  3. And the main thing about the Google Material Design Components is that these are open source and free. If you wish to contribute to the Google MDCs Click Here.
  4. One more thing great about the Google Material Design Components is that they support for cross-platform that includes Android, IOS, Flutter, Web applications.
  5. If the user wants to switch their system theme to Dark Theme, then the Google MDCs will automatically adapt and change their color and background to match the Dark Theme. But in the case of ordinary widgets, they won't get adapt to the system theme change. The differences between the normal button and the Google MDC button when the dark theme is enabled is shown below:
normal-vs-mdc

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. 

Step 2: Add Required Dependency

Include google material design components dependency in the build.gradle.kts file. After adding the dependencies don't forget to click on the "Sync Now" button present at the top right corner.  

implementation ("com.google.android.material:material:1.12.0")

Note: While syncing your project you need to be connected to the network and make sure that you are adding the dependency to the Module-level Gradle file as shown below.

Step 3: Change the Base application theme

Go to app > src > main > res > values > themes.xml and change the base application theme. The MaterialComponents contains various action bar theme styles, one may invoke any of the MaterialComponents action bar theme styles, except AppCompat styles.

Why the theme needs to be changed:

Let's discuss why we need to change the action bar theme to the material theme of the app to invoke all the Google MDC widgets in our android application:

  1. Because all the Google material design components are built and packaged inside the MaterialTheme for Android.
  2. If you are invoking the AppCompat action bar theme you will not end up with the error, but the application crashes immediately after launching it. Below is the code for the themes.xml file.

themes.xml:

styles.xml
<resources xmlns:tools="http://schemas.android.com/tools">     <!-- Base application theme. -->     <style name="Base.Theme.Demo" parent="Theme.Material3.DayNight.NoActionBar">         <!-- Customize your light theme here. -->         <item name="colorPrimary">@color/colorPrimary</item>         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>         <item name="colorAccent">@color/colorAccent</item>     </style>      <style name="Theme.Demo" parent="Base.Theme.Demo" /> </resources> 


Step 4: Change the colors of the application

One can change the color combination of the application and it's an optional step. Go to app > src > main > res > colors.xml file and choose your color combination. 

colors.xml:

colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources>     <color name="colorPrimary">#0f9d58</color>     <color name="colorPrimaryDark">#006d2d</color>     <color name="colorAccent">#55cf86</color> </resources> 


Step 5: Adding Google MDC buttons to the activity_main.xml file

Now in this file, we are going to design the material button as the user requirements. Note that for each of the Button styles the "style" attribute is different.

Style 1 - Contained Button:

Contained buttons are high-emphasis, distinguished by their use of elevation and fill. They contain actions that are primary to the app. Note that the contained button is the default style if the style is not set. Below is the XML code for the contained button.

XML
<Button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="Contained Button" /> 

Design UI (Contained Button):

design-ui-contained-button


Style 2 - Outlined Button:

Outlined buttons are medium-emphasis buttons. They contain actions that are important but aren’t the primary action in an app. These are used for more emphasis than text buttons due to the stroke. Below is the XML code for the Outlined button.

XML
<Button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     style="@style/Widget.Material3.Button.OutlinedButton"     android:text="Outlined Button" /> 

Design UI (Outlined Button):

design-ui-outlined-buttonn


Style 3 - Text Button:

Text buttons are typically used for less-pronounced actions, including those located in dialogs and cards. In cards, text buttons help maintain an emphasis on card content.  These are typically used for less important actions. Below is the XML code for the Text button.

XML
<Button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     style="@style/Widget.Material3.Button.TextButton"     android:text="Text Button" /> 

Design UI (Text Button):

design-ui-text-button


Style 4 - Toggle Button:

Toggle buttons group a set of actions using layout and spacing. They’re used less often than other button types. There are 2 types of Toggle Buttons in Google Material Design Components. 

  • Toggle Button
  • Toggle Button with icons

Toggle Button:

To emphasize groups of related toggle buttons, a group should share a common container. Add the below XML code for the Toggle button inside a parent layout. 

activity_main.xml:

XML
<com.google.android.material.button.MaterialButtonToggleGroup     android:id="@+id/toggleButton"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     app:layout_constraintBottom_toBottomOf="parent"     app:layout_constraintEnd_toEndOf="parent"     app:layout_constraintStart_toStartOf="parent"     app:layout_constraintTop_toTopOf="parent">      <Button         android:id="@+id/button1"         style="?attr/materialButtonOutlinedStyle"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Button 1" />      <Button         android:id="@+id/button2"         style="?attr/materialButtonOutlinedStyle"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Button 2" />      <Button         android:id="@+id/button3"         style="?attr/materialButtonOutlinedStyle"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Button 3" />  </com.google.android.material.button.MaterialButtonToggleGroup> 

Design UI (Toggle Button):

design-ui-toggle-button


Toggle Button with Icons:

To implement the toggle button with icons only import the icons in the drawable folder. In this example, we have imported format_black, format_italic, format_underline icons. To import the icons right-click on the drawable folder, goto new, and select Vector Asset as shown below.

import-vector-asset


After clicking on Vector Asset, go to Local file (SVG, PSD) select the icon you want to import to the drawable folder as shown below.

asset-studio


Add the following code in themes.xml file:

XML
<!-- add the following code under the resources tag      without deleting any existing code--> <style name="Widget.App.Button.OutlinedButton.IconOnly" parent="Widget.Material3.Button.OutlinedButton">     <item name="iconPadding">0dp</item>     <item name="iconSize">24dp</item>     <item name="android:minWidth">48dp</item>     <item name="android:minHeight">48dp</item> </style> 


After importing the desired icons invoke the code in the parent layout of the activity_main.xml file. Below is the XML code for the Toggle button with icons.

activity_main.xml:

XML
<com.google.android.material.button.MaterialButtonToggleGroup     android:layout_width="wrap_content"     android:layout_height="wrap_content"     app:layout_constraintBottom_toBottomOf="parent"     app:layout_constraintEnd_toEndOf="parent"     app:layout_constraintStart_toStartOf="parent"     app:layout_constraintTop_toTopOf="parent">      <com.google.android.material.button.MaterialButton         android:id="@+id/bold_button"         style="@style/Widget.App.Button.OutlinedButton.IconOnly"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         app:icon="@drawable/format_bold_24dp" />      <Button         android:id="@+id/italic_button"         style="@style/Widget.App.Button.OutlinedButton.IconOnly"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         app:icon="@drawable/format_underlined_24dp" />      <Button         android:id="@+id/underline_button"         style="@style/Widget.App.Button.OutlinedButton.IconOnly"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         app:icon="@drawable/format_italic_24dp" />  </com.google.android.material.button.MaterialButtonToggleGroup> 

Design UI (Toggle Button with icons):

design-ui-toggle-button-with-icons



Next Article
Material Design Buttons in Android with Example

A

adityamshidlyali
Improve
Article Tags :
  • Android
  • Kotlin Android
  • Java-Android

Similar Reads

    Theming Material Design Buttons in Android with Examples
    Material Design Components (MDC Android) offers designers and developers a way to implement Material Design in their Android application. Developed by a core team of engineers and UX designers at Google, these components enable a reliable development workflow to build beautiful and functional Androi
    7 min read
    Material Design EditText in Android with Example
    EditText is one of the important UI elements. Edittext refers to the widget that displays an empty text field in which a user can enter the required text and this text is further used inside the application. In this article it has been discussed to implement the special type of text fields, those ar
    3 min read
    Theming of Material Design EditText in Android with Example
    Prerequisite: Material Design EditText in Android with Examples In the previous article Material Design EditText in Android with Examples Material design Text Fields offers more customization and makes the user experience better than the normal text fields. For example, Icon to clear the entire Edit
    4 min read
    Context Menu in Android with Example
    In Android, there are three types of menus available to define a set of options and actions in the Android apps. Here in this article let's discuss the detail of the Context Menu. In Android, the context menu is like a floating menu and arises when the user has long-pressed or clicked on an item and
    4 min read
    Modal Bottom Sheet in Android with Examples
    In this article, we will learn about how to add Modal Bottom Sheet in our app. We have seen this UI component in daily applications like Google Drive, Maps, or Music Player App. The modal Bottom sheet always appears from the bottom of the screen and if the user clicks on the outside content then it
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences