Android DrawerLayout doesn't show the right indicator icon-ThrowExceptions

Exception or error:

I’m trying to use the new DrawerLayout for a list. The problem is though I set the drawer listener, the indicator on the actionbar is still the arrow icon instead of the 3-line icon which I intends to draw. The following is the OnCreate function:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_front_page);

    // Swiping Pager set up
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // Sliding Drawer set up
    mHabitContract = new HabitsContract(this);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.front_page_layout);
    mDrawerList = (ListView) findViewById(R.id.habit_list);
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    mDrawerList.setAdapter(new HabitAdapter(mHabitContract.GetHabitItems(), this));

    // Fixme: Indicator image doesn't show up
    mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            R.drawable.ic_navigation_drawer,  /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description for accessibility */
            R.string.drawer_close  /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            //getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            //getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    // Action Bar set up
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayShowHomeEnabled(true);
}

Can anyone help?

Update: I found the problem. I added the onPostCreate function as follows, and now it’s working.

protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}
How to solve:

You need to syncState() call of your ActionBarDrawerToggle object from onPostCreate()

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

###

For those who are searching this or came here looking for an answer: you have to use to DrawerToggle support.v7. Following how I use it:

drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_opened, R.string.drawer_closed){
        public void onDrawerClosed(View view){
            //actionBar.setTitle(mTitle);
            invalidateOptionsMenu(); // crea la llamada para onPrepareOptionsMenu
        }
        public void onDrawerOpened(View view){
            //actionBar.setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // crea la llamda para onPrepareOptionsMenu
        }
    };

This works fine and has an animation that rocks!

###

The icon you are using is called “ic_navigation_drawer”. You could check if It is the proper one because the icon that Google offers in the official guide (http://developer.android.com/training/implementing-navigation/nav-drawer.html) is called “ic_drawer”.

###

Also check carefuly if custom ActionBar theme is defined into your project in style.xml file with condition android:displayOptions=showTitle. Like this:

 <style name="OrangeBar" parent="@android:style/Widget.Holo.ActionBar">
    <item name="android:displayOptions">showTitle</item>
    ...
 </style>

and it can spoil drawer indicator. To avoid this add showHome at android:displayOptions:

    <item name="android:displayOptions">showHome|showTitle</item>

Leave a Reply

Your email address will not be published. Required fields are marked *