Android Tips: Hello AppCompatActivity, Goodbye ActionBarActivity

James Montemagno

With the latest updates to the Android Support v7 AppCompat Library, revision 22.1, integrating Google’s Material Design has never been easier. ToolbarIf you need to get caught up with AppCompat, we have already covered how to add Material Theming to your apps with AppCompat and how to replace your ActionBar with the new advanced Toolbar in previous posts. With revision 22.1, Google has made some drastic positive changes to the API.

Getting Started

It’s easy to get started with the Support v7 AppCompat library by simply installing the NuGet through the NuGet package manager in Visual Studio or Xamarin Studio.

AppCompatNuGets

After you have the latest NuGet packages installed, you will want to ensure that you properly configure your theme by following this guide.

Introducing AppCompatActivity

ActionBarActivity was originally introduced years ago to bring ActionBar support to pre-Android 3.0 devices. However, as Android advances and the AppCompat library continues to grow, changes must be made. If you were using older versions of the support libraries, once you have upgraded your NuGet packages and build, you will now see that ActionBarActivity has been deprecated. It has been replaced with AppCompatActivity, which will be used to cover the full scope of everything that is in AppCompat.

public class MainActivity : AppCompatActivity
{
}

It may seem like just a name change, but you now have access to the internal logic of AppCompat by using AppCompatDelegate enabling any Activity to hook into lifecycle methods, theming, color tinting, and more if you want to just use Activity. However, the easiest way to ensure forward and backwards compatibility is to simply use AppCompatActivity.

Beautiful Dialogs

One of my favorite features of revision 22.1 of AppCompat has to be Support AlertDialog. This support AlertDialog brings Material Design to all of your alert dialogs with a simple name change. Previously, to open a dialog you may have used the following code:

var builder = new AlertDialog.Builder (this);

builder.SetTitle ("Hello Dialog")
       .SetMessage ("Is this material design?")
       .SetPositiveButton ("Yes", delegate { Console.WriteLine("Yes"); })
       .SetNegativeButton ("No", delegate { Console.WriteLine("No"); }); 
  
builder.Create().Show ();

Now, you can replace AlertDialog with Android.Support.V7.App.AlertDialog or add the following statement at the top to make your dialog shine.

using AlertDialog = Android.Support.V7.App.AlertDialog;

Here is the before and after:

Support Alert Dialogs

Important Updates

You should also be aware of a few important changes to theme attributes in revision 22.1.

Updates to Theme

When you setup your custom theme inside of values/styles.xml there are several attributes that you can use to specify your color palette and use of the toolbar. Yours may look similar to this:

<resources>
  <style name="MyTheme" parent="MyTheme.Base">
  </style>
  <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">#2196F3</item>
    <item name="colorPrimaryDark">#1976D2</item>
    <item name="colorAccent">#FF4081</item>
  </style>
</resources>

One of these attributes is not like the others. As you can see android:windowNoTitle has the “android:” prefix, however in revision 22.1 this has been corrected so your theme simply reads:

<resources>
  <style name="MyTheme" parent="MyTheme.Base">
  </style>
  <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">#2196F3</item>
    <item name="colorPrimaryDark">#1976D2</item>
    <item name="colorAccent">#FF4081</item>
  </style>
</resources>

Updates to Toolbar

Similar to the theme, the support toolbar has also received a few updates to its XML. Previously, you needed to add a local namespace xmlns, but this is no longer needed and you can create the toolbar with the following code:

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

So Much More

I have only touched on revision 22.1 of AppCompat in this post, however the other Support Libraries including Support v4, Leanback, RecyclerView, and Palette have also received updates. These include several enhancements such as drawable tinting, animation interpolators, SortedList for RecyclerView, and drastic speed improvements to Palette. Be sure to read through the official Android developer blog for all of the latest details.

0 comments

Discussion is closed.

Feedback usabilla icon