Search code examples
androiddrawerlayout

Android DrawerLayout doesn't show the right indicator icon


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();
}

Solution

  • 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();
    }