Example usage of AppCompatActivity in Android

The latest release of android support library, 22.1, deprecates the ActionBarActivity in favor of AppCompatActivity, which promises to bring a single consistent ActionBar for all devices starting with API Level 7 and above.
Also, the new update adds the ability to tint widgets automatically when using AppCompat, and adds support for consistent material design dialogs. Use support.v7.app.AlertDialog (instead of android.app.AlertDialog), to get nice looking material dialogs across multiple versions of devices.


1. In order to benefit from all these things, the first thing you should do is to update the support library to 22.1.0.

dependencies {
   // … 
   compile 'com.android.support:appcompat-v7:22.1.0'
2. Then let your activity extend AppCompatActivity.
public class MainActivity extends AppCompatActivity {
  // ...
3. And finally, change the application theme to AppCompat or any descendants of it.
<application android:theme="@style/Theme.AppCompat">
Configuring the material color palette

Following the steps above will setup the application with the default theme, but the actionbar can be styled further by specifying the material color palette:

<style name="AppTheme" parent="Theme.AppCompat">
   <item name="colorPrimary">@color/primary</item>
   <item name="colorPrimaryDark">@color/primaryDark</item>
   <item name="colorAccent">@color/accent</item>

and then update your application theme to use AppTheme.

<application android:theme="@style/AppTheme">

10 thoughts on “Example usage of AppCompatActivity in Android

  1. Hi, thank you for this post. What we need to first set “@style/Theme.AppCompat” in the third step, then override it to “@style/AppTheme” ? This is the same tag on the AndroidManifest.xml?

    1. Not sure if I understood correctly your question but I will take a guess…

      So, setting directly the application theme to “@style/Theme.AppCompat”, will make your app inherit the default look. There’s however the possibility to override the default theme, and in the example above is showed how you can style it by specifying the material color palette, which basically creates a new theme called “AppTheme” which inherits from “Theme.AppCompat”, and overrides some attributes like “colorPrimary” and “colorDark”.

      And of course, you will need to change your application theme in manifest to point to the new custom theme.

      1. Thanks, okay I think I got you. So when we choose a blank activity we got that @style/Theme.AppCompat (Black ActionBar and White/Grey body) for apply the Material Design on Android 2.3+, and if we want to update an old project with HOLO THEME we also set Theme.AppCompat.

        Otherwise if we want other color we set @style/AppTheme (the file in MY_PROJECT/app/res/style/) ?
        (sorry Im french)

    1. You define the colors according to your preference.
      Create a file called “colors.xml” in the “res/values” directory, and fill in with the appropriate colors.

      In the example above I used these colors:


      <?xml version="1.0" encoding="utf-8"?>
          <color name="primary">#673AB7</color>
          <color name="primaryDark">#512DA8</color>
          <color name="accent">#E040FB</color>
  2. Very useful post, I was struggling with AppCompat and the themes, now it’s much clearer.
    Can you tell me what the tag
    is doing if I put it in any of the items of the menu.xml ?

  3. I’m working this code in Eclipse neon. I’m stuck already in styles.xml:
    produces the following error (message): “Error retrieving parent for item: No resource found that matches the given name ‘Theme.AppCompat’.

    Note that I have imported ‘android.support.v7.app.ActionBar’ and ‘android.support.v7.app.AppCompatActivity’ and that my main activity is set as ‘public class Main extends AppCompatActivity’.
    There’s no other error, only ‘Theme.AppCompat’.

    Note also that working with AppCompatActivity, similar errors have been produced (in other attempts) with ‘Theme.AppCompat.Light.DarkActionBar’, ‘Theme.AppCompat.Light.NoActionBar’, etc.

    I come to suspect that all this occurs because of the SDK (API level) 17 that I have to use as target (because of my Android devices). Is this the reason? Should I forget AppCompatActivity as long as I am programming in that AP level?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s