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 android.support.v7.app.ActionBar inside you activity are:

  1. To be running API level 7 or higher.
  2. Then extend this class.
  3. Set you activity theme to android.support.v7.appcompat.R.style#Theme_AppCompat Theme.AppCompat or similar theme.

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

<activity
      android:name=".MainActivity"
      android:label="@string/app_name"
      android:theme="@style/AppTheme.NoActionBar">....</activity>

AppCompatActivity is defined inside the android.support.v7.app package:

package android.support.v7.app;

It derives from android.support.v4.app.FragmentActivity:

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.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Setting AppCompatActivity theme

AppCompatActivity like other activity themes can be set either via androidmanifest

<activity
      android:name=".MainActivity"
      android:label="@string/app_name"
      android:theme="@style/AppTheme.NoActionBar">....</activity>

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:

<resources>

    <!-- 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>
    </style>

</resources>

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

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">

        </activity>
    </application>

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 android.support.v7.widget.Toolbar 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:

...
<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            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) {}

Example:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

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:




Recommendations


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