19th Apr 2022

Getting started with Material You

Mobile App

Written By, Yagnik Vadgama

Overview

Hello folks, hope you are doing well. So you all know that Google has launched Android 13 and has introduced “Material You” which is also called “Material 3” along with Android 12. So, as a developer, you all are curious to know about how to implement Material You in your project.

What is Material You?


source

The simple answer is Material You is an enhanced version or we can say a newer version of material design. Previously in material design, dynamic colour was not introduced but in Material You dynamic colour is the main highlight. So now the question is what is dynamic colour? The dynamic colour means the app will take its colour according to your device’s theme colour. Now I hope you have an idea about Material You.

How to implement/update to Material 3?

In this blog, I am going to create one small and simple application which is named Rent & Ride and we will see implementation in steps so you can have a proper idea of how to implement it. But before we start you must have a basic theme and material components knowledge. So let’s start with step one.

Step 1: The first thing you should do in your build.gradle file is to update/add material dependency to the latest version. In my case, the latest one is 1.5.0-alpha04.

Change the compileSdkVersion and targetSdkVersion to

  • Note: Whenever you create a theme for material 3 then your theme must change to Theme.MaterialComponents.* To Theme.Material3.* in your theme.xml file 
  • Now your theme.xml looks like

As well as color.xml file

Your file structure will be

NOTE: You may get an error message about your AndroidManifest.xml file and Android 12 apps needing to specify an explicit value for android:exported for the activity.

Step 2: Now firstly check-in your AndroidManifest.xml that the android:exported value to be true because due to that you may get an error message. And in Android 12 apps need to specify an explicit value for android:exported=”true” for the activity.

Step 3: Now add your dynamic colours to the application. For that, you have to create a class file that extends Application() class. In my case, I have created a DynamicColor named file and extended it with Application class as you can see below.

Now the main thing is what the applyToActiviesIfAvaliable(this) method means right?

    • So this method will add dynamic colours to all the activities which are present in your project. 
    • In case you want to apply your theme given theme to activities then just add one param applyToActiviesIfAvaliable(this,R.style.<YOUR_THEME_NAME>)

Add class name to AndroidManifest.xml with . symbol.

Step 4: Now we are almost done with dynamic colour implementation and now we will apply colours to our views but you must use Material 3 below. You can see the example.

  • As seen above, I’ve used color.material_dynamic_primary0. The reason is this colour is system generated color if you remember then we had declared color_primary in color.xml file and assign it to <item name=”colorPrimary”>@color/color_primary</item> so android Material 3 generates its different variants of that colour which is different in shades material_dynamic_primary_0 means the darker shade and material_dynamic_primary100 means the lightest shade. In between there are many more like material_dynamic_primary10, material_dynamic_primary200, etc. just remember one thigh as you increase the value the colour will become lighter in shade.
  • The last thing is that you must have an Android 12 or above device or emulator and to enable the theme app icon go to your device setting -> Wallpapers and Style -> Change the theme colours. And see the reflection in your application.
  • So that’s it we had successfully done with material 3 just run and check output is the same as below.


How to set dynamic a Icon?

  • Dynamic icons are easy to implement. Firstly you need to make one change in build.gradle file just change compileSdkVersion 31
    To compileSdkPreview “Tiramisu”. 
  • Google recently released Android 13 Developer Preview 2 so Tiramisu is the official name of Android 13 you can read more here.
  • Now you have to choose any icon for your app. One thing to keep in mind is to choose an icon as simple as the reason is It will look more beautiful with the theme app icon. I have chosen.


  • One more thing to keep in mind that Google recommends is that the logo fits within a 36 x 36 dp area inside a 90 x 90 dp container. If the logo needs to be larger, it can be a maximum of 60 x 60 dp.
  • Now add your app icon in the project the same way as you are adding the icon still shown below.
    • Just want to add an icon then follow the below steps:
      • Right-click on drawable -> New -> vector assets -> choose path of icon -> click on Next and Finish button.
    • Add icon along with icon background colour changes then:
      • Right-click on drawable -> New -> Image Assets (customize your icon with colour, height, width, etc) -> click on Next then Finish button.
  • Add your app icon in AndroidManifest.xml and check if android:roundIcon and android:icon are both in your manifest then you must remove the reference (delete the directory) of android:roundedIcon or add a monochrome icon in the drawable defined by the android:roundedIcon attribute. In my case, I have removed the rounded icon directory.

Now the final thing is to open path rootDirectory -> app -> src -> main -> res -> mipmap-anydpi-v26 -> open ic_launcher.xml (in my case there is ic_launcher_logo.xml) the file looks like

  • And add <monochrome android:drawable=“@drawable/<YOUR_APP_ICON>”/> below the <foreground android …./> this will add support for dynamic icons. 
  • The last thing is that you must have an Android 12 or above device or emulator and to enable the theme app icon go to your device setting -> Wallpapers and Style -> enable Themed Icon.

That’s it. The output will be as below.

Conclusion:

Congratulations 🎉🎉🎉  you have added support for dynamic icons in your project. Want to see the full project? Here’s the github repo link.

Written By,

Android Developer at Yudiz Solutions Pvt. Ltd