How to add Toolbar

Mario Viviani
Google Developer Experts
3 min readApr 23, 2015

--

to an Activity

which doesn’t extend AppCompatActivity

Android Support Library 22.1 introduces AppCompatDelegate

The new AppCompat support library version 22.1 introduces a lot of cool new features which allows to easily add Material Design/API 21+ features to our old, non-AppCompat Activities.

We know that extending AppCompatActivity is the best way to go, but sometimes you need to go old-school, right? ;-)

You can use the new AppCompatDelegate component provided by the Support Library to easily add a Toolbar to your Activity.

1) Add the Toolbar widget to the activity layout

ActionBar is now deprecated and it should be replaced by the Toolbar widget, which allows much more UI flexibility and also allows your Activity to be easily compliant with Material Design UI Patterns.

Since we’re using the Support Library here, we’ll use the support version of Toolbar (which, otherwise, is only available from API 21+).

Add it to your xml layout like this:

<android.support.v7.widget.Toolbar

android:id="@+id/my_awesome_toolbar"
android:layout_height="@dimen/abc_action_bar_
default_height_material"
android:layout_width="match_parent"
android:minHeight="@dimen/abc_action_bar_
default_height_material"
android:background="?attr/colorPrimary"

/>

2) Modify your Theme

Since we’re embedding the Toolbar in the layout, we need to use a ActionBar-free theme.

So, be sure to use a Theme.AppCompat.NoActionBar theme in your styles.xml.

ProTip: Add the Material Design color items to your theme, to easily allow Toolbar and Status Bar Tinting (only on 21+ for Status Bar).

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">

<!-- colorPrimary is used for coloring the Toolbar -->
<item name="colorPrimary">#3F51B5</item>

<!-- colorPrimaryDark is used for coloring the status bar -->
<item name="colorPrimaryDark">#303F9F</item>

<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#FFAB40</item>

</style>

3) Add AppCompatDelegate to your Activity

The AppCompatDelegate is a delegate that provides the AppCompatActivity features to your Activity, and can only be created using the create() method, which takes 2 arguments: the Activity itself, and an AppCompatCallback.

So, first of all, let’s have our activity implement AppCompatCallback (we can leave these methods empty, in this demo).

public class MainActivity extends Activity implements AppCompatCallback {

@Override
public void onSupportActionModeStarted(ActionMode mode) {
//let's leave this empty, for now
}

@Override
public void onSupportActionModeFinished(ActionMode mode) {
// let's leave this empty, for now
}

Then, in the onCreate() of our Activity:

  1. Create the AppCompatDelegate with AppCompatDelegate.create()
  2. Call AppCompatDelegate.onCreate() (There are some Activity lifecycle methods which should be proxied to the delegate)
  3. Inflate the layout with AppCompatDelegate.setContentView()
  4. Add the Toolbar to the delegate with AppCompatDelegate.setSupportActionbar()
private AppCompatDelegate delegate;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//let's create the delegate, passing the activity at both arguments (Activity, AppCompatCallback)
delegate = AppCompatDelegate.create(this, this);

//we need to call the onCreate() of the AppCompatDelegate
delegate.onCreate(savedInstanceState);

//we use the delegate to inflate the layout
delegate.setContentView(R.layout.activity_main);

//Finally, let's add the Toolbar
Toolbar toolbar= (Toolbar) findViewById(R.id.my_awesome_toolbar);
delegate.setSupportActionBar(toolbar);
}

ProTip: An Activity can only be linked with one AppCompatDelegate instance, so the instance returned from create() should be kept until the Activity is destroyed.

Now you’re done! Run your app and your Activity will magically have a Toolbar, and totally looks like a real Material Design AppCompatActivity!

Quite easy, uh? ;-)

Again, this is for edge cases, and you should always consider the usage of AppCompatActivity before going for the AppCompatDelegate.

Be sure to check out the complete reference of AppCompatDelegate, there are a lot of methods that you should take care of wrapping in your Activity if you want it to fully act as an AppCompatActivity.

Here’s a Github Project that demonstrate what is described in this post.

If you liked this post, follow me on Google+ or on Twitter for more informations about Android!

--

--