Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
}
like image 980
zhengbli Avatar asked Jul 24 '13 05:07

zhengbli


People also ask

How do you use DrawerLayout?

To use a DrawerLayout, position your primary content view as the first child with width and height of match_parent and no layout_gravity> . Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.

What is Navigation drawer activity in Android?

The navigation drawer is a UI panel that shows your app's main navigation menu. The drawer appears when the user touches the drawer icon in the app bar or when the user swipes a finger from the left edge of the screen.


2 Answers

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();
}
like image 157
Pratik Avatar answered Sep 30 '22 14:09

Pratik


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!

like image 31
Andres Melgarejo Ledesma Avatar answered Sep 30 '22 14:09

Andres Melgarejo Ledesma