Android AppCompatActivity

AppCompatActivity is a class that acts as the super class for activities that want to take advantage of the support library action bar features.

The requirement to use the inside you activity are:

  1. To be running API level 7 or higher.
  2. Then extend this class.
  3. Set you activity theme to Theme.AppCompat or similar theme.

Themes do get set in the AndroidManifest.xml e.g


AppCompatActivity is defined inside the package:


It derives from

public class AppCompatActivity extends FragmentActivity{}

And implements several interfaces:

public class AppCompatActivity extends FragmentActivity implements AppCompatCallback,TaskStackBuilder.SupportParentable, ActionBarDrawerToggle.DelegateProvider {}

Here's an example of a class deriving from AppCompatActivity:

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

Setting AppCompatActivity theme

AppCompatActivity like other activity themes can be set either via androidmanifest


or programmatically via the setTheme() method:

public void setTheme(@StyleRes final int resid) {}

We can create a custom material theme to be used by AppCompatActivity:


    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>


Then we can also set that theme globally to the whole application this way using the android:theme="..." attribute:

        <activity android:name=".MainActivity">


Retrieving AppCompatActivity ActionBar

AppCompatActivity gives us a method to retrieve a reference to its action bar:

public ActionBar getSupportActionBar(){}

If it doesn't have one then null gets returned.

Using ToolBar as an ActionBar in AppCompatActivity

We can use instead of an actionbar. Toolbars have the advantage of flexibility in use and customizations.

ActionBars are normally part of the Activity's opaque window decor. Hence they are controlled by the framework.

ToolBars on the other hand can be used within your application's layout. Hence they are flexible.

Say that we have a toolbar defined as follows:

            app:popupTheme="@style/AppTheme.PopupOverlay" />

inside our activity_main.xml.

We can use it as action bar using the setSupportActionBar() where we pass the toolbar reference as a parameter.

public void setSupportActionBar(@Nullable Toolbar toolbar) {}


Toolbar toolbar = (Toolbar) findViewById(;

To clear it you pass null.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Previous Post Next Post