122

I installed Android Studio freshly and I begun coding an activity to extend ActionBarActivity and it showed that it was deprecated. So how else do I set up an actionbar for my activity. Also the Getting Started Training uses the ActionBarActivity without making reference that it has been deprecated.

Md. Sabbir Ahmed
  • 850
  • 8
  • 22
De-Great Yartey
  • 1,574
  • 2
  • 12
  • 16
  • [AppCompat Activity](https://medium.com/google-developer-experts/how-to-add-toolbar-to-an-activity-which-doesn-t-extend-appcompatactivity-a07c026717b3) – M D Apr 26 '15 at 12:38
  • http://stackoverflow.com/questions/29797172/whats-the-enhancement-of-appcompatactivity-over-actionbaractivity – Bojan Kseneman Apr 26 '15 at 12:47
  • 16
    As of appcompat-v7-r21.1.0 the logic somewhat changed and on top of that `ActionBarActivity` is now `AppCompatActivity`. There's no difference between these two from your point of view. – Eugen Pechanec Apr 26 '15 at 12:58
  • 1
    ActionBarActivity is removed since support library 26.0.0-beta2 https://developer.android.com/sdk/support_api_diff/26.0.0-beta2/changes/alldiffs_index_removals.html – ישו אוהב אותך Feb 14 '18 at 06:23

3 Answers3

121

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'
stkent
  • 19,772
  • 14
  • 85
  • 111
Endran
  • 1,627
  • 1
  • 10
  • 15
  • 9
    Is it as simple as swapping out `extends ActionBarActivity` with `extends AppCompatActivity`? – Ehtesh Choudhury May 01 '15 at 20:34
  • 9
    Internally there are quite some changes, but the interface is identical. So yeah, it is as easy as just replacing the type of activity. – Endran May 02 '15 at 09:19
  • 62
    sucks that the Android Studio wizard still extends ActionBarActivity and then tells you it is deprecated. Do they even communicate internally @Google? – Boy May 15 '15 at 08:07
  • Not the Perfect Solution. Your given answer for ActionBar not for ActionBarActivity. I will go with @DroidMind Solution – Shabbir Dhangot Jul 15 '15 at 04:56
  • Great, more work again. Same as their design to encourage everyone to use Fragments. Google's design principles are great for companies that have 10 people working on the same app. – Muz Sep 30 '15 at 14:53
71

Here is the answer from the post in Android developers blog:

"ActionBarActivity has been deprecated in favor of the new AppCompatActivity."

You can read more about it there.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
nvinayshetty
  • 3,187
  • 1
  • 21
  • 32
8

This answer give a simple way to Eliminate the error message. You can see as an add to others'.

When we change the parent Activity class: ActionBarActivity to AppCompatActivity the error message will disappear.

You can click here for more info.

Wentao Ma
  • 97
  • 1
  • 3