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 for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
Theming Material Design Buttons in Android with Examples
Next article icon

Theming Material Design Buttons in Android with Examples

Last Updated : 19 Jul, 2022
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 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 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. In the previous article Material Design Buttons in Android with Example, we have discussed four types of Buttons, Contained Button, Outlined Button, Text Button, and Toggle Button. So in this article, we are going to discuss another popular type of Button i.e Theming Material Design Buttons. At the end of this article, one can easily add icons to material design buttons and change or override the default theme of the material design buttons. A sample image is given below to get an idea about what types of Button we are going to create in this article. 

Theming material Design Button

Step for Creating Theming Material Design Buttons

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 Material design dependency on the app level Gradle file

Invoke the following line of code into the dependencies field.

implementation 'com.google.android.material:material:1.3.0-alpha02'

If unable to locate the app level Gradle file refer to the following image to get it under Project view.

Gradle file

Step 3: Change the Base application theme

Go to app > src > main > res > values > styles.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. Below is the code for the styles.xml file.

XML
<resources>      <!-- Base application theme. -->     <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">         <!-- Customize your theme here. -->         <item name="colorPrimary">@color/colorPrimary</item>         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>         <item name="colorAccent">@color/colorAccent</item>     </style>  </resources> 

If unable to locate the styles.xml file refer to the following image.

styles.xml file

Step 4: Working with the activity_main.xml

In this case, a typical 3 material design buttons are invoked. Those are the Contained Button, Outlined Button, Text Button. To know the usage and how to invoke them, one can read the Material Design Buttons in Android with Example article. Invoke the following code in the activity_main.xml file to implement all types of material design buttons. Comments are added inside the code to understand the code in more detail.

XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout      xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     tools:context=".MainActivity"     tools:ignore="HardcodedText">      <!--Contained Button-->     <!--there is no style attribute for contained button-->     <!--by default the button itself is a contained button-->     <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:layout_marginTop="64dp"         android:text="Contained Button" />      <!--Outlined Button-->     <!--need to carefully invoke the style attribute-->     <Button         style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:layout_marginTop="64dp"         android:text="Outlined Button" />      <!--Text Button-->     <!--need to carefully invoke the style attribute-->     <Button         style="@style/Widget.MaterialComponents.Button.TextButton.Icon"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:layout_marginTop="64dp"         android:text="Text Button" />  </LinearLayout> 

Output: Run on Emulator

Output

Steps to Add Icons to the Material Design Buttons

Step 1: Import vector icon(s) to the drawable folder

To import the vector icons to the drawable folder right click on the drawable folder > goto New > click on Vector Asset. Now select the desired vector from the pop-up window. One can refer to the following image to get a vector icon selector pop-up window.

Vector File

Click on the marked button in the below image, to select the desired vector icons.

Vector File

Step 2: Working with the activity_main.xml file

By adding the app:icon attribute in each of the buttons and selecting the desired vector icon from the drawable folder, will add the icon to the material design buttons. So, invoke the following code.

XML
<?xml version="1.0" encoding="utf-8"?><!--there is need to invoke the app namespace                                            to add icons to material design button--> <LinearLayout      xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:app="http://schemas.android.com/apk/res-auto"      xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     tools:context=".MainActivity"     tools:ignore="HardcodedText">      <!--Contained Button-->     <!--there is no style attribute for contained button-->     <!--by default the button itself is a contained button-->     <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:layout_marginTop="64dp"         android:text="Contained Button"         app:icon="@drawable/ic_add_black_24dp" />      <!--Outlined Button-->     <!--need to carefully invoke the style attribute-->     <Button         style="@style/Widget.MaterialComponents.Button.OutlinedButton"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:layout_marginTop="64dp"         android:text="Outlined Button"         app:icon="@drawable/ic_add_black_24dp" />      <!--Text Button-->     <!--need to carefully invoke the style attribute-->     <Button         style="@style/Widget.MaterialComponents.Button.TextButton.Icon"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:layout_marginTop="64dp"         android:text="Text Button"         app:icon="@drawable/ic_add_black_24dp" />  </LinearLayout> 

Output: Run on Emulator

There are two methods of overriding the theme of the material design buttons:

Here the icons are optional. So, in this case, the icon has been removed. However, the theme can be overridden with or without the icon.

Method 1:

This method of implementation will affect other material components, as well as material design buttons. Invoke the following code in the styles.xml file to override the default theme of the material design buttons.

XML
<resources>      <!-- Base application theme. -->     <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">         <!-- Customize your theme here. -->         <item name="colorPrimary">@color/colorPrimary</item>         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>         <item name="colorAccent">@color/colorAccent</item>          <!--this item is to override the fontFamily of the material design buttons-->         <item name="textAppearanceButton">@style/TextAppearance.App.Button</item>          <!--this item is to override the cornerFamily and cornerSize of the material design buttons-->         <item name="shapeAppearanceSmallComponent">@style/ShapeAppearance.App.SmallComponent</item>      </style>      <!--This overrides the fontFamily and textSize of material design buttons-->     <style name="TextAppearance.App.Button" parent="TextAppearance.MaterialComponents.Button">         <item name="fontFamily">sans-serif-black</item>         <item name="android:fontFamily">sans-serif</item>         <item name="android:textSize">18sp</item>     </style>      <!--This overrides the cornerFamily and cornerSize of the default material design button-->     <style name="ShapeAppearance.App.SmallComponent" parent="ShapeAppearance.MaterialComponents.SmallComponent">         <item name="cornerFamily">cut</item>         <item name="cornerSize">8dp</item>     </style>  </resources> 

Output: Run on Emulator

Output

One can notice the change in the corner cut of 8dp, similar in all the buttons.

Note: By using this method if the application contains the material design floating action button, the default theme of those all floating action button overridden by same attributes invoke in above code.

Method 2:

This method of overriding the default theme of the material design button will affect each of the individual buttons. In this case, different attributes can be overridden for Contained Button, Outlined Button, and Text Button. Invoke the following code in the styles.xml file.

XML
<resources>      <!-- Base application theme. -->     <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">         <!-- Customize your theme here. -->         <item name="colorPrimary">@color/colorPrimary</item>         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>         <item name="colorAccent">@color/colorAccent</item>          <!--this item is invoked in app theme to override the contained material button-->         <item name="materialButtonStyle">@style/myCustomContainedButton</item>          <!--this item is invoked in app theme to override the outlined material button-->         <item name="materialButtonOutlinedStyle">@style/myCustomOutlinedButton</item>     </style>     <!--Base application theme ends here-->      <!--theming of contained button begins here-->     <!--parent name is to be give carefully-->     <style name="myCustomContainedButton" parent="Widget.MaterialComponents.Button">         <!--these all styles are implemented outside, because to get all the overridden attributes easily-->         <item name="shapeAppearance">@style/customContainedButtonTheme</item>         <item name="android:textAppearance">@style/myCustomTextForMaterialButtons</item>     </style>     <!--overriding cornerSize and cornerFamily of the contained button-->     <style name="customContainedButtonTheme">         <item name="cornerFamily">rounded</item>         <item name="cornerSize">24dp</item>     </style>     <!--theming of contained button ends here-->      <!--theming of outlined button begins here-->     <!--parent name is to be give carefully-->     <style name="myCustomOutlinedButton" parent="Widget.MaterialComponents.Button.OutlinedButton">         <!--these all styles are implemented outside, because to get all the overridden attributes easily-->         <item name="shapeAppearance">@style/customOutlinedButtonTheme</item>         <item name="android:textAppearance">@style/myCustomTextForMaterialButtons</item>     </style>     <!--overriding cornerSize and cornerFamily of the outlined button-->     <style name="customOutlinedButtonTheme">         <item name="cornerFamily">cut</item>         <item name="cornerSize">12dp</item>     </style>     <!--theming of outlined button ends here-->      <!--Font family and size of all the buttons in the entire application should be same-->     <!--so this child is common for all the material buttons in entire application-->     <style name="myCustomTextForMaterialButtons">         <item name="fontFamily">sans-serif-condensed-medium</item>         <item name="android:textSize">18sp</item>         <item name="android:fontFamily">sans-serif</item>     </style>  </resources> 

Note that here the default theme of the text button is not overridden, because it's not much affected as it has no border and no background color. Only the text color and text font can be changed in the activity_main.xml file itself. After overriding the default theme of all buttons, there is a need to change the style attribute of the outline button in the activity_main.xml file. This needs to be changed because the outlined button is child class of material button, if not invoked the outlined button will be inheriting the style of the contained button.

XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout      xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     tools:context=".MainActivity"     tools:ignore="HardcodedText">      <!--Contained Button-->     <!--there is no style attribute for contained button-->     <!--by default the button itself is a contained button-->     <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:layout_marginTop="64dp"         android:text="Contained Button" />      <!--Outlined Button-->     <!--need to carefully invoke the style attribute-->     <Button         style="@style/myCustomOutlinedButton"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:layout_marginTop="64dp"         android:text="Outlined Button" />      <!--Text Button-->     <!--need to carefully invoke the style attribute-->     <Button         style="@style/Widget.MaterialComponents.Button.TextButton.Icon"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:layout_marginTop="64dp"         android:text="Text Button" />  </LinearLayout> 

Output: Run on Emulator

Output


Next Article
Theming Material Design Buttons in Android with Examples

A

adityamshidlyali
Improve
Article Tags :
  • Android

Similar Reads

    Material Design Buttons in Android with Example
    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 Andro
    6 min read
    Theming Material Design Snackbars in Android with Example
    In the previous article, it's been discussed Snackbar Material Design Components in Android. In this article, it's been discussed how we can theme material design Snackbar and increase the User Experience. Theming Example 1:This method is done using the styles.xml file. Where we need to override the
    5 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
    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 Floating Action Buttons in Android with Example
    Prerequisite: Floating Action Button (FAB) in Android with ExampleExtended Floating Action Button in Android with Example Android application developers want to seek the attention of the users by customizing and theming the android application widgets and keep more traffic of customers only by the d
    12 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