Would you like to contribute to the Titanium docs? To get started, sign up for an account on the Appcelerator Wiki and sign our CLA.

Skip to end of metadata
Go to start of metadata

Introduction

Android allows you to set the appearance of your application using themes. A theme specifies default colors, fonts, and images, for an Android activity or an entire application. Your application can use the device's built-in themes, or include custom themes. By default, Titanium applications use a custom theme called Titanium for their main activity. This theme defines the splash screen when the application starts, but otherwise defers to a device default theme. This theme is based on the AppCompat theme with a translucent background.

To update the look of your application, you can either:

  • Use a different built-in Android theme
  • Use a Titanium-defined theme
  • Create a custom theme and define the elements you want to change
  • Use the Android Material Theme to quickly customize the theme's color palette
  • Use third-party tools to help you generate a custom theme

You can specify a unique theme per window or per activity.

Android Themes

Android provides some built-in themes to easily change the overall appearance of your application.  To use a built-in theme, you need to create a theme XML file for your project, specify the built-in theme you want to use, and reference it in the Android manifest section of your tiapp.xml file.

First, create a theme XML file in ./platform/android/res/values.  Do NOT name the file theme.xml.  Titanium uses this name as its default theme file, which is required to build the application.  If you create a file called theme.xml, it will overwrite the default Titanium one and break the build process.

In the theme XML file, add the theme you want to use. Themes defined by the Android system, excluding the AppCompat ones, are prefaced with @android:style (for example, @android:style/Theme.Translucent). Custom themes defined by the application are prefaced with @style (for example, @style/Theme.MyTheme).  For example, the file below adds support for some of the common built-in Android themes.

The Titanium SDK uses the appcompat library to provide support for themes.  A benefit of using the appcompat library is that it provides ActionBar support on devices running Android 2.1.x and later.  Prior to the usage of the appcompat library, the ActionBar was only available to devices running Android 3.x and later.  Note that applications can only use the AppCompat themes.

platform/android/res/values/builtin_themes.xml

Finally, to use a theme in your application, modify the Android section of your tiapp.xml file to reference the style name you want to use:

The screenshots below show the difference between the various built-in themes:

AppCompat.Light

AppCompat/HoloAppCompat.Light.DarkActionBar

Titanium Themes

The Titanium SDK includes a few predefined themes.  You can use these themes in your application rather than creating your own.

NOTE: Do not create a theme with the same name as a predefined theme.

Theme NameSDK VersionDescription
Theme.Titanium*

Theme based off the Android SDKs default AppCompat theme with the splash screen image as the default background.

Prior to 3.3.0, displays the title bar.

Since 3.3.0 and later, hides the action and title bar.

Theme.AppCompat.Translucent3.4.0Theme based off the AppCompat theme with a transparent background and a solid action bar
Theme.AppCompat.Translucent.NoTitleBar3.4.0Same as Theme.AppCompat.Translucent but with no action or title bar.
Theme.AppCompat.Translucent.NoTitleBar.Fullscreen3.4.0Same as Theme.AppCompat.Translucent.NoTitleBar but the window is fullscreen (covers the status bar).
Theme.AppCompat.Fullscreen3.4.0Theme based off the AppCompat theme with a fullscreen window (covers the status bar) with no action or title bar.

Theme.AppCompat.NoTitleBar

4.2.0Theme based off the AppCompat theme with no action and title bar.

Theme.AppCompat.NoTitleBar.Fullscreen

4.2.0Same as Theme.AppCompat.NoTitleBar with a fullscreen window (covers the status bar).

To apply a theme globally, in the tiapp.xml file, set the android:theme attribute to the theme name in the application element of the Android manifest section.

To change the theme on a per-window basis, set the theme name to the Window's theme property (available since Release 3.4.0).

Custom Themes

To define custom themes, place the theme XML files with your custom styles in the platform/android/res/values folder. Do NOT name the file theme.xml.  Titanium uses this name as its default theme file, which is required to build the application.  If you create a file called theme.xml, it will overwrite the default Titanium one and break the build process.

Note that you can also define version-specific themes by adding a values-v<version> folder. For example a theme defined in values-v11 will be used for API level 11 (3.0/Honeycomb) and above, and a theme defined in values-v14 will be used for API level 14 (4.0/Ice Cream Sandwich) and above.

For example, if you want your theme to be based on the Light theme, create the following theme file:

platform/android/res/values/mytheme.xml

The theme file above contains two custom style items:

  1. The first item defines the "Theme.MyTheme" based on the Android Light theme.  The style sets the window background using the image located at platform/android/res/drawable/example.png.
  2. The last item overrides the Light theme's default cursor color for text fields and uses the TextField's color property as the cursor color.

To use the "Theme.MyTheme" theme in your application, modify the Android section of your tiapp.xml file:

Refer to Android Developers: Styles and Themes for detailed information on customizing items.

Material Theme

Starting with Release 4.0.0, the Titanium SDK exposes the Material Theme color palette attributes, introduced in Android 5.0 (Lollipop).  These attributes allow you to style the status bar, action bar and several control widgets, such as toggle switches and text fields, without having to individually style each component.

To use the Material Theme with Titanium, you need to create a custom theme that extends one of the AppCompat themes, then set the color palette attributes you want to change from the default theme (see the attribute table below).  You will need to build your application with Android SDK 5.0 (API 21) and greater.  Note that some of the attributes will not be applied when the application is ran on devices running previous versions of Android (pre-Lollipop) (see the comparison table below).

Icon

Do not set the parent attribute to @android:style/Theme.Material or one of its variants. The material theme is only supported on Android 5.0 and greater. Titanium uses the AppCompat library to provide support for the action bar to previous versions of Android and cannot directly inherit from the material theme.

MaterialThemeAnnotated

Color Palette AttributeDescription
colorPrimaryDarkSets the color of the status bar. Only works with Android 5.0 (API 21) and greater.
colorPrimarySets the color of the action bar.

colorAccent

Sets the accent color, which is usually the color of the control when it is activated.

colorControlNormal

Sets the color of the control when it is not activated.

colorControlActivated

Sets the color when the control is activated. Overwrites the colorAccent attribute.

colorControlHighlight

Sets the color when the user clicks on a control. Only works with Android 5.0 (API 21) and greater.

colorSwitchThumbNormal

Sets the color of a toggle switch's thumb when it is not enabled. Only works with Android 5.0 (API 21) and greater.
android:colorButtonNormalSets the color of a button when it is not pressed. Only works with Android 5.0 (API 21) and greater.
android:colorEdgeEffectSets the edge effect color when the user tries to scroll beyond the content's boundaries. Only works with Android 5.0 (API 21) and greater.

android:navigationBarColor

Sets the color of the navigation bar (the bar that appears at the bottom of the device that contains the Back, Home and Recent App buttons). Only works with Android 5.0 (API 21) and greater.

android:textColorPrimary

Sets the color of text on controls. Prior to Android 5.0, this only sets the color of the action bar title and overflow menu.

Example:

The following XML file below defines a theme that uses the default AppCompat theme and applies additional color palette attributes.  This theme was applied to the application in the previous screen shot.

platform/android/res/values/custom_theme.xml

Theme Comparison:

The following table compares the same application when using different Android versions and themes. Note that the Android 4.4. screenshots use the old style toggle button due to Android bug #78262.

Example Material Theme / Android 5.0Example Material Theme / Android 4.4Default AppCompat / Android 4.4

 

Further Reading:

Override a Theme

If you have a global theme set, the application can override the theme for a window or activity, or if you want to specify a custom theme for a specific window or activity.

Override a Window Theme

Starting with Release 3.4.0, the Titanium SDK exposes the theme property for Windows.  Use the theme property to override the global theme for an individual window.  Set the property to the name of the theme you want to apply to the window.  The property can only be set when creating the Window object and cannot be changed after it is set.

Override an Activity Theme

As you can see in the previous examples, an application-wide theme can be specified in the <application> element of your tiapp.xml file, but it can also be overridden on a per-activity basis.  These activities are defined in the AndroidManifest.xml file, generated by the build process. You can find the generated AndroidManifest.xml file in the build/android folder under your project folder. Inside the AndroidManifest.xml, you'll find code like this:

Icon

The build folder is hidden by default in Studio's App Explorer and Project Explorer views, but visible in the Navigator view.

To override the theme for one of these activities, copy the activity definition into the tiapp.xml file, and place it inside the <android><manifest> element. The <activity> element must be nested inside an <application> element, as in the original AndroidManifest.xml file. The end result should look something like this:

Icon

If you change the name of the application, you will need to copy the newly generated android:name value from the AndroidManifest.xml file and overwrite the old value in the tiapp.xml file.

Custom Theme Generator

Android Holo Colors Generator

The Android Holo Colors Generator is a website tool that builds and generates an Android Holographic theme based on the customizations you select.  This tool generates the XML files and graphic assets you need and packages them in to a ZIP file, which you can unpack in your project.

To create a custom Holo theme:

  1. Go to http://android-holo-colors.com/
  2. Enter a name for your theme.  This name will be used to reference your theme in the Android manifest.  If you want to use an action bar style generated by the Android Action Bar Style generator, do not use the same name.
  3. Pick a base color for your theme.
  4. If you set Min SDK Version to < 11, make sure Compatibility is set to APPCOMPAT.
  5. Set at least one widget control to Yes, except SwitchSwitch Jellybean and Drawer must be set to No.
  6. Click Download .ZIP button near the button of the web page to download your custom theme.

Once you have your custom theme, unpack the ZIP file and copy the res folder to your project's platform/android folder.  Then, modify your tiapp.xml file to use the theme, for example:

 

Android Action Bar Style Generator

The Android Action Bar Style Generator is a website tool that builds and generates an Android Action Bar style based on the customizations you select.  This tool generates the XML files and graphic assets you need and packages them in to a ZIP file, which you can unpack in your project.

To create a custom Action Bar style:

  1. Go to Android Action Bar Style Generator.
  2. Enter a name for your style.  This name will be used to reference your style in the Android manifest.  If you want to use a theme generated by the Android Holo Colors Generator, do not use the same name.
  3. For Style Compatibility, use AppCompat.
  4. Choose the style customizations you want to use.
  5. Click Download .ZIP button near the button of the web page to download your custom theme.

Once you have your custom Action Bar style, unpack the ZIP file and copy the res folder to your project's platform/android folder.  Then, modify your tiapp.xml file to use the custom style, for example:

Use Both the Android Holo Colors Generator and Android Action Bar Style Generator

If you want to use both a custom theme generated by the Android Holo Colors Generator and the styles generated by the Android Action Bar Style Generator, you need to merge together both res directories and modify the XML style file generated by the Action Bar Style Generator to use your custom theme rather than the built-in Android theme.  You also cannot use the same name for the custom theme and action bar style.

  1. Open your Action Bar style file: ./platform/android/res/values[-vxx]/style_<actionbarstylename>.xml, where <actionbarstylename> is the name your gave to your Action Bar style.  Note that you may have more than one style file located in different values-vxx directories.
  2. Edit the first style element (should be line #22 in the file).  Change the parent attribute to your custom theme.: 

    To: 

  3. Modify the tiapp.xml file to use the custom style generated by the Android Action Bar Style Generator not the custom theme generated by the Android Holo Colors Generator.

Further Reading