Alex Chard
Alex Chard

Reputation: 41

Android AppCompat Themes

I'm running through the Android Developer classes, as I'd like to start building some apps.

I've downloaded the ADT package with Eclipse, as I'm familiar with Eclipse.

I'm playing around with the themes, but for some reason, my project has been set up to only allow the AppCompat themes. This means I have to use themes like Theme.AppCompat.Light. If I try to use Theme.Holo or Theme.Translucent, I get a run time error: IllegalStateException: You need to use a Theme.AppCompat theme

I've set the minimum sdk version in the AndroidManifest.xml as follows:

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

Any help appreciated.

Edited after Guillermo's response:

Thanks Guillermo, but that hasn't worked. Here is the updated AndroidManifest:

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

<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/MyBaseTheme2" >
    <activity
        android:name="com.example.myfirstapp.MainActivity"
        android:label="@string/app_name" 
        android:theme="@style/MyBaseTheme1">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity"  
        android:theme="@style/MyBaseTheme2">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
</application>
</manifest>

I have the following styles.xml file in res/values-14:

<!--
    Base application theme for API 14+. This theme completely replaces
    AppBaseTheme from BOTH res/values/styles.xml and
    res/values-v11/styles.xml on API 14+ devices.
-->
<style name="MyBaseTheme1" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- API 14 theme customizations can go here. -->
</style>
<style name="MyBaseTheme2" parent="android:Theme.Holo.Light">
</style>

</resources>

I use MyBaseTheme1 on my first activity, which loads fine. I use MyBaseTheme2 on my second activity, which crashes the app. If I then use MyBaseTheme1 on my second activity, that works fine, with no crashes.

Edited to add styles files. res/values/styles.xml:

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="MyBaseTheme1" parent="Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="MyBaseTheme1">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

res/values-11/styles.xml

<resources>

    <!--
        Base application theme for API 11+. This theme completely replaces
        AppBaseTheme from res/values/styles.xml on API 11+ devices.
    -->
    <style name="MyBaseTheme1" parent="Theme.AppCompat.Light">
        <!-- API 11 theme customizations can go here. -->
    </style>

</resources>

Upvotes: 1

Views: 9365

Answers (2)

user3663455
user3663455

Reputation: 31

In the activity in which you're trying to apply a different theme, import android.app.Activity.

Also make sure that the class extends Activity, not ActionBarActivity.

When the class extends ActionBarActivity it isrestricted to the AppCompat.Light themes

Upvotes: 3

Guillermo Merino
Guillermo Merino

Reputation: 3257

Holo themes were added in API 14, if you use a minSdkVersion="11" you are being forced to use API 11 compatible resources.

If you still want to keep your app compatible with API 11 you can use HoloEverywhere, more info here

Upvotes: 1

Related Questions