Why was ActionBarActivity deprecated

ActionBar is deprecated ever since Toolbar was introduced. Toolbar can be seen as a ‘superset’ of any action bar. So the ‘old’ ActionBar is now an example of a Toolbar. If you want similar functionality, but without deprecation warnings do the following:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
    toolbar.setTitle(R.string.app_name);
    setSupportActionBar(toolbar);
}

You need to define the Toolbar in your layout xml:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>

With this new functionality you can create your own custom ActionBar and let Android do the heavy lifting. Just create your own custom view that extends from Toolbar.


Also, you should use AppCompatActivity instead of ActionBarActivity, it was introduced in the latest version of the appcompat library. So dont forget to update gradle

compile 'com.android.support:appcompat-v7:22.1.1'

Leave a Comment