Android Styles and Themes

Android styles allow you to design the appearance (for example, colors, size, and fonts) of Android UI components from XML resource files. With this method, you only need to set the common style attribute once in the central location.

This is commonly used to reduce style redundancy in a way that is very similar to CSS in the world of web development. You can specify a style for a single central file to apply a consistent style to the entire view of your application.

Style along with the drawable is a way that helps you to easily keep the face of heavy UI customization. Styles work by declaring a style name accompanied by a set of attributes that you apply to a view. Styles can create compound styles that can be inherited from other styles.

Declaring and using Styles

First, you declare the XML style in res/values/styles.xml directory:

<style name ="ExtraLargeBlueFont">
    <item name ="android:textColor">#FF6200EE</item>
    <item name ="android:textSize">50sp</item>
</style>

For using styles inside the activity:

<TextView
    android:id ="@+id/display_app_name"
    style ="@style/ExtraLargeBlueFont"
    android:layout_width ="wrap_content"
    android:layout_height ="match_parent"
    android:text ="@string/app_name" />

Inheritance in Styles

You may want to inherit a style and change certain attributes most of the time. You can specify a style from which your style should inherit properties in the parent attribute of <style> tag. You can use it to inherit properties from existing styles and declare only those properties that you want to modify or add.

<style name ="SmallFont">
    <item name ="android:textSize">15sp</item>
</style>


<style name ="SmallPurpleFont" parent="@style/SmallFont">
    <item name ="android:textColor">#FF6200EE</item>
</style>

You don't even need to use parent attribute if you choose to inherit from a style, you declare yourself. Instead, simply put the name of the new style, separated by a period, before the name of the inherited style as a shortcut.

<style name ="SmallFont">
    <item name ="android:textSize">15sp</item>
</style>


<style name ="SmallFont.Purple">
    <item name ="android:textColor">#FF6200EE</item>
</style>

Using multiple periods, you can continue to inherit styles:

<style name ="SmallFont.Purple.Italic">
    <item name ="android:textStyle">italic</item>
</style>


<style name ="SmallFont.Purple.Large">
    <item name ="android:textSize">40sp</item>
</style>

Android built-in styles cannot be inherited in this manner. You need to use parent attribute to refer a built-in style:

<style name="CustomisedButton" parent="@android:style/Widget.Button">
    <item name="android:gravity">top</item>
    <item name="android:textColor">#FF6200EE</item>
</style>

Using Themes

Sometimes, we try to apply a consonant theme to all activities in the application. Instead of applying styles to specific individual views, you can apply style collections i.e., themes to your activities or applications. That way, you'll apply each attribute that the activity or all views in your application support.

Declaring a Theme

<style name="LightTheme.MyApplication" parent="Theme.AppCompat.Light">
    ...
</style>

This theme contains item attributes that often refer to various other styles or colors:

<style name ="LightTheme.MyApplication" parent ="Theme.AppCompat.Light">
    <item name ="android:windowBackground">@color/teal_200</item>
    <item name ="android:colorBackground">@color/teal_200</item>
</style>

Modifying Themes

In many cases, you might want to modify the default presentation of your application’s views. For example, you can modify the text color of a TextView or Button as the application’s default. It can be accomplished by declaring the styles that are inherited by default, and you can override that property in res / values / styles.xml.

<!-- Base application theme. -->
<style name ="Theme.MyApplication" parent ="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name ="colorPrimary">@color/purple_500</item>
    <item name ="colorPrimaryVariant">@color/purple_700</item>
    <item name ="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name ="colorSecondary">@color/teal_200</item>
    <item name ="colorSecondaryVariant">@color/teal_700</item>
    <item name ="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name ="android:statusBarColor" tools:targetApi ="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
    <!-- TextView custom theme. -->
    <style name ="Widget.TextView.Custom" parent="android:Widget.TextView">
        <item name="android:textColor">@color/teal_200</item>
    </style>
</style>

Use the generated AppTheme to modify the buttonStyle and textViewStyle to determine the default style for the control. Then inherit from the basic Widget.Button or Widget.TextView and modify it.

Apply Theme

To set the theme for all your application's activities, open the AndroidManifest.xml file and edit the application's tag to add the android: theme attribute in the company with the style name. Example:

<application android:theme ="@style/LightTheme.MyApplication">

You can also apply the theme to a specific activity by adding the following attribute:

<activity android:theme ="@style/LightTheme.MyApplication">

Referencing Styles from Themes

Using the ?attr syntax you can refer the currently applied attribute once a theme is applied. For example, to set the EditText base color to use the default base color, you can do it as follows:

<TextView
    android:textColor ="?attr/colorAccent"
    android:layout_width ="match_parent"
    android:layout_height ="wrap_content"
    android:text ="Hello Everyone"/>

Custom theme attributes can also be utilized for button states using state lists:

<selector xmlns:android ="http://schemas.android.com/apk/res/android">
    <item android:color ="?attr/colorTeal" android:state_enabled ="false"/>
    <item android:color ="?attr/colorSecondary"/>
</selector>

Be sure to utilize the AppCompatResources or ContextCompat helper classes to resolve these theme attributes properly:

ContextCompat.getColor(this, R.color.design_default_color_on_primary);


// resolve the default color 
ColorStateList colorState = AppCompatResources.getColorStateList(this, R.color.design_default_color_primary).getDefaultColor();

You should also do the same for drawable as above to allow the theme attributes to be resolved:

Drawable drawable = AppCompatResources.getDrawable(this, R.drawable.ic_baseline_arrow_drop_down_24);

Related Topics

Android Date picker example

How to pick date from the user in Android? Write an android example to pick date from the user and display it. Android provides control for the user to pick a date...

3 minutes read.

Android Studio's Layout Editor

The Layout Editor allows you to efficiently create layouts by dragging UI elements from the Palette into the visual design editor instead of manually creating the layout XML file by...

8 minutes read.

Android Navigation Drawer

A sliding menu used to show important links in the Android Application is called Navigation Drawer. The navigation drawermakes it easier to navigate between the fragments. It is not displayed...

5 minutes read.

Android Styles and Themes

Android styles allow you to design the appearance (for example, colors, size, and fonts) of Android UI components from XML resource files. With this method, you only need to set...

5 minutes read.

Android ScrollView

A ScrollView is a view group that is used to create vertically scrollable views in an android application. Only a single direct child can be contained by ScrollView. For placing...

5 minutes read.

Android TextView

Widgets are user interface (UI) elements that help users to interact with Android applications. It is one of many equipments that can be used to improvise the user interface of...

4 minutes read.

How to show image on an android application

ImageView Widget in Android What you should already KNOW? You should be familiar with: 1) Creating, building, and running apps in Android Studio 2) Basics of android widgets   Explanation: Image View widget is used...

2 minutes read.

Content Providers of Android with Example

An essential part of Android that performs the function of a relational database by storing application data is called a content provider. The function of the content provider in the...

13 minutes read.

Assets folder in Android

You might have noticed that unlike the Eclipse ADT (App Development Tools), the Android Studio does not have an Assets folder, which is mainly used to store the web resources...

3 minutes read.

Android Emulator

A tool for creating Android virtual devices (including software and virtual hardware) on your personal desktop is known as Emulator. We can use these virtual devices as the target device...

4 minutes read.

How to insert an email in android application in android version 8

Email Address Insertion It is used to insert email adrress from the user in the android application.The only point of difference between an EditText and an Emailtext is that EmailText consisits...

1 minute read.

Top Programming Languages for Android Development

For mobile apps, there are so many programming languages. You would need to spend days simply researching if you had to pick one from a list of 30 coding languages...

8 minutes read.

Creating a Calendar View App in Android

This article demonstrates how to make an Android application that uses CalendarView to display the Calendar. It also provides the option to pick the current date and view the date....

3 minutes read.

Different Ways to Create aar File in Android Studio

A file called an Android archive file (*.aar) can be created using Android Studio and comprises classes and methods that use related files and classes for Android. The Android library...

4 minutes read.

Event Handling in Android

Events are a convenient way to gather data about user interactions with interactive elements of your application. Like a button click or a screen touch. A queue of events on...

3 minutes read.

Android ListView

A kind of AdapterView that displays a vertical list of scrollable views, each of which is laid out below each other is called ListView. Using adapters, items are inserted into...

4 minutes read.

Button Widget in Android Tutorial

Explanation: Button is a widget under Buttons section which is used to execute specific code. Example: XML file: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context=".MainActivity">     <Button         android:id="@+id/button"         android:layout_width="wrap_content"    ...

1 minute read.

What is Android Studio

Android Studio powered by the IntelliJ platform is an integrated development environment (IDE) and is officially recognized for developing androidapplications. Its foundation lies on JetBrains' IntelliJ IDEA and is specialized...

3 minutes read.

Top 7 Android App Development Books

Do you want to create Android apps but don't know where to start? One should first be familiar with the basics of Android before we discuss these books. With the...

5 minutes read.

ImageView Widget in Android

What you should already KNOW? You should be familiar with: 1) Creating, building, and running apps in Android Studio 2) Basics of android widgets Explanation: Image View widget is used to insert the image...

2 minutes read.