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 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.

AdapterViewFlipper in Android with Example

The AdapterViewFlipper class, a subclass of the ViewAnimator class, is what we use to switch from two or multiple views when only one is present at a time. The AdapterViewFlipper...

10 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 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.

How to insert the mobile number on an android application using EditText

Number Insertion in android application: Sometimes while dealing with numbers, we don't want to insert any string inside an number so, we premiliary define an EditText so we externally specify...

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.

Android Broadcast Receivers

Android applications can send or receive broadcast messages from the Android operating system and other Androidapplications, like the publish-subscribe design pattern. When an event of interest happens,these broadcasts are sent. For...

4 minutes read.

Android 9.0 Pie vs. Android 10, which one is better

Android smartphone users are always searching for new system upgrades and keep an eye on system updates whenever the next version is published. Android 9 Pie and Android Q 10 are...

5 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 Toast

Android is one of the most successful operating systems available in today's world. It is an open-source phone platform, compatible with almost all the devices. There are numerous features and...

3 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.

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.

Android RecyclerView

RecyclerView is a ViewGroup brought to AndroidStudio as successor to ListView and GridView. These are all improvements and are included in the latest v7 support package. It was written to...

8 minutes read.

How to reduce the size of APK in Android

What exactly is an APK file? The full form of APK is Android Package Kit. It is the Android operating system's application file format. We use Android Studio to generate APK...

7 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.

How to crop Image from Camera and Gallery in Android?

In this tutorial, we'll learn how to crop photos in the Android Gallery and Camera. There is no crop feature in this project, contrary to what was covered in the...

5 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.

Android EditText

In Android, EditText is a UI widget utilized to allow the user to input or change the text. While using the EditTextwidget in your Android application, you need to use...

4 minutes read.

How to Update Gradle in Android Studio?

The Android Gradle plugin adds a number of features that are unique to develop Android apps to the Gradle build system, which is the foundation of the Android Studio development...

2 minutes read.