Navigation drawer icon not showing using appcompat v7

2.7k views Asked by At

style.xml

<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <item name="android:homeAsUpIndicator">@drawable/ic_back</item>
        <item name="homeAsUpIndicator">@drawable/ic_back</item>
    </style>

MainActivity.java

ActionBar actionBar = getSupportActionBar();

actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP);
        actionBar.setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        View actionBarView = inflator.inflate(R.layout.custom_title, null);

actionBar.setCustomView(actionBarView);


        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_action_nav_menu, //nav menu toggle icon
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ) {
            public void onDrawerClosed(View view) {
                tvTitle.setText(mTitle);
                //getActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                tvTitle.setText(mDrawerTitle);
                //getActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };

enter image description here

you can see in above image, where in black circle i want to show 3 line image rather than back symbol image.

2

There are 2 answers

0
Edward Tan On

Use android.support.v7 instead of android.support.v4 as following:

import android.support.v7.app.ActionBarDrawerToggle;

And change your mDrawerToggle to the following:

 mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.string.app_name, // nav drawer open - description for accessibility
            R.string.app_name // nav drawer close - description for accessibility
    )...

Add following codes:

actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);

// Defer code dependent on restoration of previous instance state.
mDrawerLayout.post(new Runnable() {
     @Override
     public void run() {
         mDrawerToggle.syncState();
     }
});
0
user3385125 On

change package import android.support.v4.app.ActionBarDrawerToggle; to import android.support.v7.app.ActionBarDrawerToggle;

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.app_name, R.string.app_ ); mDrawerlayout is object DrawerLayout