2

I did some searchs and tried a lot of solutions, but i'm going to the same problem:

The drawer hamburguer icon doesn't show until I swipe the drawer.

enter image description here

Here my code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    setSupportActionBar(toolbar);
    setupDrawer();
    ...
}

//calling the method bellow inside onCreate
public void setupDrawer(){
    mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            syncState();
        }

        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            syncState();
        }
    };

    mDrawerToggle.setDrawerIndicatorEnabled(true);
    drawerLayout.setDrawerListener(mDrawerToggle);
    ActionBar actionBar =  getActionBar();

    if(actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setIcon(R.drawable.ic_launcher);
    }
}

I want it aways showing.

diogojme
  • 2,309
  • 1
  • 19
  • 25

3 Answers3

3

Try to get action bar by getSupportActionBar() method, not getActionBar(). Inside your setupDrawer() method:

mDrawerToggle.setDrawerIndicatorEnabled(true);
drawerLayout.setDrawerListener(mDrawerToggle);
// here
//ActionBar actionBar =  getActionBar();
ActionBar actionBar =  getSupportActionBar();
Mirjalol Bahodirov
  • 2,053
  • 1
  • 11
  • 16
1

As you're using a Toolbar, and probably an ActionBarActivity, you don't have a built-in ActionBar. The ActionBarActivity provides a wrapper around the Toolbar exposing usual ActionBar methods (like setDisplayShowHomeEnabled(boolean enabled)). But to get this wrapper, you have to call getSupportActionBar() instead of getActionBar().

In your code, it simply translates to this:

public void setupDrawer(){
    // ...

    mDrawerToggle.setDrawerIndicatorEnabled(true);
    drawerLayout.setDrawerListener(mDrawerToggle);
    ActionBar actionBar =  getSupportActionBar();

    if(actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setIcon(R.drawable.ic_launcher);
    }
}
Marc Plano-Lesay
  • 6,808
  • 10
  • 44
  • 75
  • Thanks for the answer, but it show the back arrow and I want to show the hamburguer icon, but i fixed it using this solution > http://stackoverflow.com/a/28136349/1879661, but not sure if is the best solution, what you think? – diogojme Dec 15 '15 at 19:25
0

After fixed it from @BahodirovMirjalol solution:

mDrawerToggle.setDrawerIndicatorEnabled(true);
drawerLayout.setDrawerListener(mDrawerToggle);
ActionBar actionBar =  getSupportActionBar();

I got another problem because navigation back icon show instead hamburguer, so I added this two methods above my onCreate method and it works:

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

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}

found here

Community
  • 1
  • 1
diogojme
  • 2,309
  • 1
  • 19
  • 25