16

Today Google updated the AppCompat library to version 22.1.0, and we can now use AppCompatActivity instead of ActionBarActivity. This means we no longer need to have a Toolbar view in our activity layout.

The problem is that in order to create a Drawer toggle button, I cannot use new ActionBarDrawerToggle anymore, because it expects a Toolbar parameter, which will not exist.

How am I supposed to add the toggle button to the ActionBar now?

Guilherme
  • 7,839
  • 9
  • 56
  • 99
  • Out of curiosity - who's saying that *"we no longer need to have a Toolbar view in our activity layout"*? I can't find any reference for this. – reVerse Apr 21 '15 at 22:42
  • The new `AppCompatActivity` already features a toolbar (which I believe can be removed/hidden if you want), so you don't need to add another one – Guilherme Apr 21 '15 at 23:15
  • The `AppCompatActivity` features an `ActionBar` per default. You still have to modify the Theme to `Theme.AppCompat.NoActionBar` and add a `Toolbar` to your layout. – reVerse Apr 22 '15 at 07:24
  • @AlexMomotov not yet – Guilherme Apr 24 '15 at 19:31

4 Answers4

23

A possible solution

Activity:

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity 
{

    DrawerLayout drawerLayout;
    ActionBarDrawerToggle toggle;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        toggle = new ActionBarDrawerToggle
            (
                    this,
                    drawerLayout,
                    R.string.navigation_drawer_open,
                    R.string.navigation_drawer_close
            )
            {
            };
        drawerLayout.setDrawerListener(toggle);
        toggle.syncState();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if (toggle.onOptionsItemSelected(item))
        {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Layout:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <ListView
        android:id="@+id/list_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#f1f2f7"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent" />

</android.support.v4.widget.DrawerLayout>

Style :

<resources>


    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    </style>

</resources>

It's important that your app inherit the AppCompat theme.

If you replaced the actionbar by a toolbar do not forget to put back the actionbar by removing this line in the styles.xml :

<item name="windowActionBar">false</item>

Gradle :

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:22.1.1'
    compile 'com.android.support:appcompat-v7:22.1.1'
}

I put this code on github : https://github.com/bbouabou/AppCompatActivity-With-ActionBarDrawerToggle .

Brahim Bouaboud
  • 390
  • 2
  • 11
5

As per official docs, the ActionBarDrawerToggle class from v7 features a toolbar-independent constructor:

public ActionBarDrawerToggle (Activity activity, DrawerLayout drawerLayout, int openDrawerContentDescRes, int closeDrawerContentDescRes)

This will work with the default ActionBar provided through activity. So, as always, either:

  1. you inherit from an action bar theme, and call new ActionBarDrawerToggle(...);
  2. you inherit from a .NoActionBar theme, instantiate/inflate a Toolbar object, and call new ActionBarDrawerToggle(..., Toolbar t, ...)

Looks to me that nothing changed with the ActionBarActivity refactoring.

natario
  • 24,954
  • 17
  • 88
  • 158
4

If you're using Android Studio's default Navigation Drawer setup, then I found success by changing NavigationDrawerFragment.java's ActionBarDrawerToggle class from v4 to v7 in the import statement and omitting the Toolbar argument from the ActionBarDrawerToggle constructor.

SQLiteNoob
  • 2,958
  • 3
  • 29
  • 44
  • hi @SQLiteNoon, please can you show your new constructor. Mine does have these parameters (new ActionBarDrawerToggle(getActivity, DrawerLayout, R.drawable.ic_drawer,"open, "close" – Val Okafor May 18 '15 at 23:33
-3

I couldn't find a way to use the AppCompatActivity's default toolbar, so the workaround I used was to use the Theme.AppCompat.NoActionBar theme and manually add the toolbar to the XML like I was doing before.

Guilherme
  • 7,839
  • 9
  • 56
  • 99