Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override default Android themes

Tags:

java

android

I've been able to override any themes that have names with "android:" prepended to them, but the Android themes.xml also defines properties that don't seem to be able to be overridden. For example:

<!-- Variation on the Light theme that turns off the title -->
<style name="Theme.Codebase" parent="android:style/Theme.Light">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="colorBackground">@color/off_white</item>
</style>

colorBackground is defined in the Theme.Light xml, but adding this here gives me a

/res/values/styles.xml:10: error: Error: No resource found that matches the given name: attr 'colorBackground'.

error. How do I override that style for the Application as a whole?

like image 504
typeoneerror Avatar asked Jan 28 '11 23:01

typeoneerror


People also ask

How do I change the default device theme?

Settings. Under Display Options, tap Theme. Select the theme for this device: Light—White background with dark text.

What is the difference between themes and styles?

A style is a collection of attributes that specify the appearance for a single View . A style can specify attributes such as font color, font size, background color, and much more. A theme is a collection of attributes that's applied to an entire app, activity, or view hierarchy—not just an individual view.


2 Answers

You can overwrite standard attributes the same way you modified such properties as windowNoTitle, just don't forget to add android: prefix like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="SEclubTheme" parent="@android:style/Theme">
        <item name="android:colorForeground">@color/bright_foreground_dark</item>
        <item name="android:colorBackground">@color/background_dark</item>
    </style>
</resources>
like image 87
Malcolm Avatar answered Oct 06 '22 05:10

Malcolm


Without the attr prefix, your colorBackground becomes an attribute that you need to define. Consider the following example where theme_dependent_icon is defined in a styles.xml:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <declare-styleable name="custom_menu">
            <attr name="theme_dependent_icon" format="reference"/>
    </declare-styleable>
    <style name="MyDarkTheme" parent="android:Theme" >
        <item name="theme_dependent_icon">@drawable/ic_search_dark</item>
    </style>
    <style name="MyLightTheme" parent="android:Theme.Light" >
        <item name="theme_dependent_icon">@drawable/ic_search_light</item>
    </style>
</resources>

Then, you can use the attribute via ?attr/theme_dependent_icon in your main_activity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="?attr/theme_dependent_icon" />
</LinearLayout>

In this example, because I used custom theme names MyDarkTheme and MyLightTheme, they need to be selected during onCreate of your main activity prior to setContentView, i.e.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.MyDarkTheme); // causes ic_search_dark.png to be shown
    // setTheme(R.style.MyLightTheme); // causes ic_search_light.png to be shown
    setContentView(R.layout.main_activity);
}

Calling setTheme() is one way of selecting a theme during runtime. Another way is to define multiple versions of styles.xml in your resources under the values, values-11, values-14 corresponding to default theme, theme for Android 3.0 (API-11) and theme for Android 4.0 (API-14).

like image 32
Stephen Quan Avatar answered Oct 06 '22 05:10

Stephen Quan