Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

android - Changing Navigation drawer hamburger icon

I am trying to change hamburger menu icon for NavigationView but I am unable to do so.

Here is what I have tried so far

I have a base activity where nav drawer setup is done. Here is relevant piece of code

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

private void setupDrawer() {
    mDrawerLayout.setDrawerListener(this);

    mDrawerToggle = new ActionBarDrawerToggle(this,
            mDrawerLayout,
            R.string.open,
            R.string.close);

    mDrawerToggle = new ActionBarDrawerToggle(mContext,
            mDrawerLayout,
            R.string.open,
            R.string.close);

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
        mDrawerToggle.setDrawerIndicatorEnabled(false);
       mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_share_48pt_2x);
    }
    mDrawerToggle.syncState();

    mNavigationView.setNavigationItemSelectedListener(
            menuItem -> {
                mMenuItem = menuItem.getItemId();
                mDrawerUtil.onNavMenuItemClicked(mMenuItem);
                mDrawerLayout.closeDrawers();
                return true;
            });
}

@Override 
public void setContentView(int layoutResID) {
    getLayoutInflater().inflate(layoutResID, mContainer);
}


@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

However it doesn't seem to be working for me. I have also tried calling setDrawerIndicatorEnabled(false) and setHomeAsUpIndicator(R.drawable.ic_share_48pt_2x) on SupportActionBar but that also doesn't work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following code works nicely for me,

protected void onCreate(Bundle savedInstanceState) {
    ...
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
    toggle.setDrawerIndicatorEnabled(false);
    toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon);
    ...
}

I also had to add a toolbar navigation click listener to listen for click events on the custom drawer icon

protected void onCreate(Bundle savedInstanceState) {
    ...
    toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                drawer.openDrawer(GravityCompat.START);
            }
        }
    });
    ...
}

Finally, I can update the icon dynamically as

toggle.setHomeAsUpIndicator(R.drawable.ic_new_icon);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...