Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new ActionBarDrawerToggle when using AppCompatActivity

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?

like image 458
Guilherme Avatar asked Apr 21 '15 22:04

Guilherme


3 Answers

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 .

like image 123
Brahim Bouaboud Avatar answered Nov 10 '22 23:11

Brahim Bouaboud


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.

like image 32
natario Avatar answered Nov 10 '22 23:11

natario


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.

like image 4
SQLiteNoob Avatar answered Nov 10 '22 22:11

SQLiteNoob