There are two approaches to modifying the look of Android apps. The first approach involves directly modifying the properties of views in layout XML files. This approach is feasible only if you are working on a simple app that has a small number of views and activities. The second approach involves creating and using custom styles and themes. If you are familiar with web development, the first approach is akin to using inline CSS styles, and the second approach is akin to using style sheets.
In this tutorial, you'll learn how to create custom styles and themes for your Android apps. You'll also learn how to use Android Studio's tools and shortcuts that facilitate style creation.
1. Creating Styles
Styles are obviously applied to UI components. Therefore, let's start by creating a new empty activity and adding two views to its layout XML file.
- <View android:layout_width="100dp"
- android:layout_height="100dp"
- android:layout_margin="5dp"
- android:background="#009688"
- android:id="@+id/box1" />
- <View android:layout_width="100dp"
- android:layout_height="100dp"
- android:background="#00BCD4"
- android:layout_margin="5dp"
- android:id="@+id/box2" />
To create a new style for the first view, right click on it and select Refactor > Extract > Style.
You will now see a dialog where you can give a name to the style and also select the attributes that should be included in it. Let the name be MyBox and select all the attributes except background.
![]() |
- <View android:background="#009688"
- android:id="@+id/box1"
- style="@style/MyBox" />
- <style name="MyBox">
- <item name="android:layout_width">100dp</item>
- <item name="android:layout_height">100dp</item>
- <item name="android:layout_margin">5dp</item>
- </style>
- <View android:background="#00BCD4"
- android:id="@+id/box2"
- style="@style/MyBox" />
![]() |
Android allows you to create styles that use other styles as a foundation. In other words, it allows you to extend existing styles.
There are two different syntaxes you can follow while extending a style. The first syntax is often called the implicit syntax and uses a dot notation. For example, here's how you create two derived styles, called TEAL and CYAN, using MyBox as the parent style:
- <style name="MyBox.TEAL">
- <item name="android:background">#009688</item>
- </style>
- <style name="MyBox.CYAN">
- <item name="android:background">#00BCD4</item>
- </style>
The second syntax for creating a derived style is usually referred to as the explicit syntax. It involves using a parent attribute whose value is set to the name of the parent style. Here's a code snippet that defines a style called TealBox.
- <style name="TealBox" parent="MyBox">
- <item name="android:background">#009688</item>
- </style>
- <View android:id="@+id/box1"
- style="@style/TealBox" />
- <View android:id="@+id/box2"
- style="@style/MyBox.CYAN" />
3. Creating Themes
All this while, we've only been applying styles to views that are inside an activity. Android allows you to apply styles to entire activities and applications too. When a style is applied to an activity or application, it becomes a theme.
By default, all apps created using the latest version of Android Studio use a theme called AppTheme. AppTheme is a descendant of the well-known AppCompat theme, a large and very comprehensive theme that can affect the looks of almost all the commonly used views.
You can find the definition of AppTheme in styles.xml:
- <!-- Base application theme. -->
- <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
- <item name="colorPrimary">@color/colorPrimary</item>
- <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
- <item name="colorAccent">@color/colorAccent</item>
- </style>
Although you can always create themes by writing XML code—remember, they are just styles—in this tutorial, I'm showing you how to use Android Studio's Theme Editor to do all the hard work for you.
To open the Theme Editor, open the Tools menu and select Android > Theme Editor.
On the right-hand side of the Theme Editor window, you have controls not only to modify themes, but also to create new ones. The left-hand side shows you a preview of the results of the modifications you make to the themes.
![]() |
In the dialog that pops up, set the name of the new theme to MyTheme and press OK.
![]() |
- <style name="MyTheme" parent="AppTheme" />
To modify the value of colorPrimary, click on the colorPrimary button. The Theme Editor will now show you a dialog containing a color picker. Choose any color you want, but make sure that you give it a new name. If you forget to do so, the Theme Editor will overwrite the color used by AppTheme.
![]() |
The definition of MyTheme would now look like this:
- <style name="MyTheme" parent="AppTheme" >
- <item name="colorPrimary">@color/colorPrimaryMyTheme</item>
- <item name="colorPrimaryDark">@color/colorPrimaryDarkMyTheme</item>
- <item name="colorAccent">@color/colorAccentMyTheme</item>
- </style>
Before we apply the theme we created, let us add a few commonly used views to the activity. Doing so will make it easier for us to notice the effects of the theme.
The following code creates a normal Button, a borderless Button, a colored Button, a Checkbox, a RadioButton, a Switch, a Seekbar, a TextView, and an EditText.
- <Button android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="normal"
- android:id="@+id/normal_button" />
- <Button android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="borderless"
- android:id="@+id/borderless_button"
- style="@style/Widget.AppCompat.Button.Borderless" />
- <Button android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="colored"
- android:id="@+id/colored_button"
- style="@style/Widget.AppCompat.Button.Colored" />
- <CheckBox android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="New CheckBox"
- android:id="@+id/checkBox" />
- <RadioButton android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="New RadioButton"
- android:id="@+id/radioButton" />
- <Switch android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="New Switch"
- android:id="@+id/switchButton" />
- <SeekBar android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/seekBar" />
- <TextView android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="New Text"
- android:id="@+id/textView" />
- <EditText android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/editText"
- android:hint="Input" />
![]() |
To apply MyTheme, the theme we created in the previous step, to your activity, open your project's manifest file and add an android:theme attribute to the activity's definition. Set its value to @style/MyTheme.
- <activity android:name=".MainActivity"
- android:theme="@style/MyTheme">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
If you take a look at your activity now, it should look very different.
![]() |
In this tutorial, you learned how to create and apply custom Android styles and themes. Feel free to use this knowledge to give new and better looks to your apps. However, try not to get too carried away—most Android users today are so accustomed to Material Design that deviating from its guidelines can annoy them.
Written by Ashraff Hathibelagal
If you found this post interesting, follow and support us.
Suggest for you:
The Complete Android Developer Course: Beginner To Advanced!
Android: From Beginner to Paid Professional
Android Development with Appcelerator Titanium - Quick Start
The Complete Android Developer Course - Build 14 Apps
Python For Android Hacking Crash Course: Trojan Perspective
No comments:
Post a Comment