3
votes

I am following the android training , in styling the Action bar.

I am facing error because of android:theme=@style/MyActionBar.

I want to know how to style the action bar.

 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyActionBar" >

IllegalStateException: You need to use a Theme.AppCompat theme with this activity

I thought the error from :

<style name="CustomActionBarTheme"
               parent="@android:style/Theme.Holo.Light.DarkActionBar">
            <item name="android:actionBarStyle">@style/MyActionBar</item>
        </style>

so I modify it to

<style name="CustomActionBarTheme"
           parent="@android:style/Theme.AppCompat.Light ">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

I had the following error :

error: Error retrieving parent for item: No resource found that matches the given name '@android:style/Theme.AppCompat.Light '.

I created a new file as the training explained

res/values/themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->

    <style name="MyActionBar"
       parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">#aaaaaa</item>
</style>
</resources>

And here is the manifest

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

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
       android:theme="@style/MyActionBar" >
        <activity
            android:name="com.example.helloworld.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
        android:name="com.example.helloworld.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.Helloworld.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.Helloworld.MyActivity" />
    </activity>

    </application>

</manifest>
3
Have you added the library into project properties?Harry's Lab

3 Answers

3
votes

edit your style as follows:

<style name="CustomActionBarTheme"
    parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>

    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<!-- ActionBar styles -->
<style name="MyActionBar"
    parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@drawable/actionbar_background</item>

    <!-- Support library compatibility -->
    <item name="background">@drawable/actionbar_background</item>
</style>

for support library you have to use

name="background">@drawable/actionbar_background</item> 

instead of

name="android:background">@drawable/actionbar_background</item>

and then use this theme in your manifest... before use don't forget to add support:appcompact dependencies in build.gradle if you are using Android Studio... thanks...

2
votes

appcompat v7 libary has two kinds of files. One is jar, the other is reses like string.xml styles.xml. You question implies that you didn't import reses of appcompat library. So, import the project of .../Android-SDK/sdk/extras/android/support/v7/appcompat into your Eclipse, and then add it as a libary project of your main project.

0
votes

I think you have not added a reference of appcompat lib in your project. Add the reference of it to your project and check

https://developer.android.com/tools/support-library/setup.html