Android AppCompatActivity

| Page Views: 2749

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.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post Next Post