简体   繁体   中英

How to set navigation home button in android for appcompat activity for toolbar?

I am using the appcompat activity for Android v-21. I want to enable the home button which I have set it to true in my code. I also have overridden the onOptionsItemSelected but it's still not working.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_apply_card);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    //Action bar
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    onBackPressed();
    return true;
}

Simple way to add action bar home enable in Appcompat activity

getSupportActionBar().show();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Add this two pulic functions in your activity also--

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked; go home
            this.finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
}

Following code snippet set navigation icon in toolbar,

 toolbar.setNavigationIcon(R.mipmap.ic_launcher);

I hope it will help you.

I know it is an old question but in order to prevent others to devote their time to solve this issue, I want to share the working method for me.

I am not sure about the reason of this. Probably, since ActionBar is deprecated and gave way to Toolbar after AppCompat, some methods of AppCompatActivity may not work as it is expected. Although the Burger (navigation button) is defined as the home button of ActionBar, we could not control click events of this button by using .onOptionsItemSelected(MenuItem). Toolbar view presents us another method to achieve this, toolbar.setNavigationOnClickListener(View.OnClickListener).

To exemplify, I tried to use balysv's MaterialMenuIcon with this method instead of .onOptionsItemSelected(MenuItem) as follows:

private void setupToolbar() {
    toolbar = (Toolbar) ((LinearLayout) findViewById(R.id.app_bar)).getChildAt(0);
    setSupportActionBar(toolbar);

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (drawerLayout.isDrawerVisible(GravityCompat.START)) {
                drawerLayout.closeDrawer(GravityCompat.START);
                materialMenu.animatePressedState(MaterialMenuDrawable.IconState.BURGER);
            } else {
                drawerLayout.openDrawer(GravityCompat.START);
                materialMenu.animatePressedState(MaterialMenuDrawable.IconState.ARROW);
            }
        }
    });

    materialMenu = new MaterialMenuIconToolbar(this, Color.WHITE, MaterialMenuDrawable.Stroke.THIN) {
        @Override public int getToolbarViewId() {
            return R.id.toolbar;
        }
    };

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            super.onDrawerSlide(drawerView, slideOffset);
            materialMenu.setTransformationOffset(
                    MaterialMenuDrawable.AnimationState.BURGER_ARROW,
                    isDrawerOpened ? 2 - slideOffset : slideOffset
            );
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            isDrawerOpened = true;
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            isDrawerOpened = false;
        }

        @Override
        public void onDrawerStateChanged(int newState) {
            super.onDrawerStateChanged(newState);
            if (newState == DrawerLayout.STATE_IDLE) {
                if (isDrawerOpened) materialMenu.setState(MaterialMenuDrawable.IconState.ARROW);
                else materialMenu.setState(MaterialMenuDrawable.IconState.BURGER);
            }
        }
    });
}

I hope it helps.

I am using Xamarin Android, and for AppCompatActivity I also used this method which did not work for me.

SupportActionBar.SetHomeButtonEnabled(true);

but after finding on internet and I found another method, which worked for me, and showed the home navigation button.

SupportActionBar.SetDisplayHomeAsUpEnabled(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM