Skip to content

Multiple Accent Colors In a Project

blurkidi edited this page Jun 4, 2014 · 5 revisions

There are basically two ways to set different color to different activities. The first option is to define several styles with a specific accent color for each, similar to what was explained in the basic setup guide. The other option is to programmatically override the accent color in the activity.

Create Themes With Different Colors

1 - Create a file called 'styles.xml' in the 'res/values' folder. Actually the file can have any name you want.

2 - For this example, we will create three different themes: ThemeRed, ThemeGreen, ThemeBlue. The content of 'styles.xml' is the following:

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<style name="ThemeRed" parent="Theme.HoloAccent.Light">
		<item name="accentColor">#ff0000</item>
	</style>
	<style name="ThemeGreen" parent="Theme.HoloAccent.Light">
		<item name="accentColor">#00ff00</item>
	</style>
	<style name="ThemeBlue" parent="Theme.HoloAccent.Light">
		<item name="accentColor">#0000ff</item>
	</style>
</resources>

Note that for we extended the light version of HoloAccent but you can replace it by any of the available themes.

3 - Finally, we have to apply the themes to our activities in the 'AndroidManifest.xml' file.

We will imagine a simple application with three activities: MainActivity, SubActivity1 and SubActivity2. We will apply ThemeRed to the whole application so it will be the default theme. We will apply ThemeGreen to SubActivity1, and fianlly, ThemeBlue to SubActivity2. The 'AndroidManifest.xml' will look like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.example"
	android:versionCode="1"
	android:versionName="1" >

	<uses-sdk
		android:minSdkVersion="14"
		android:targetSdkVersion="19" />

	<application
		android:allowBackup="true"
		android:icon="@drawable/ic_launcher"
		android:label="@string/app_name"
		android:theme="@style/ThemeRed" >
		<activity android:name=".MainActivity" >
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		<activity
			android:name=".SubActivity1"
			android:theme="@style/ThemeGreen" />
		<activity
			android:name=".SubActivity1"
			android:theme="@style/ThemeBlue" />
	</application>
</manifest>

Notice that the MainActivity doesn't explicitly specify a 'android:theme' attribute. This means that it will take the default value specified in the application: ThemeRed. Remember also that the activities have to be set up as explained in the basic setup guide.

Override the Accent Color in Code

You can override the accent color specified in the theme by overriding the getOverrideAccentColor() method in your AccentActivity:

public class MyActivity extends AccentActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}
	@Override
	protected int getOverrideAccentColor() {
		return Color.RED;
	}
}

If you don't override the method or you return 0, the color defined in the theme will be used.

This is a powerful way to set your accent color, but you will have to specify it in each of the activities. In the other hand, you could even have a preference that lets the user select the color for the UI, select a color depending on the viewing content... the possibilities are endless!

Clone this wiki locally