Skip to content

Multiple Accent Colors In a Project

blurkidi edited this page May 30, 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.

Alternative: Override On Code

This method is discouraged and it should only be used for quick testing or just to have some fun. By doing this, some elements that take the color from the theme will show the default value. For example, if you show a dialog from this activity, it will show a wrong accent color.

To apply this method, we will just need to override the getOverrideAccentColor() method in our 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 we don't override the method or it returns 0, the color will be taken from the theme as usual.

Clone this wiki locally