Android Toolbar with both home and back button
Asked Answered
R

3

10

Is it possible to display both home icon and back icon in the toolbar? 1) Is it possible change the order of display of back button icon and home icon. Currently it displays the arrow button first and then the logo (home button)

2) Second requirement is to clear the activity stack on clicking the home icon and going back to previous screen in case of back button.

I have the following code which will display a arrow back key and home icon which is set as logo. Is it possible to handle click events on both these icons:

Toolbar toolbar = (Toolbar)findByViewID(R.id.toolbar);
toolbar.setNavigationIcon(R.drwable.btn_back);
setSuppportActionBar(toolbar);
getSupportActionBar().setLogo(R.drawable.home_icon);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

I am able to handle to the click on arrow icon by handling it in onOptionsITemSelected method. Is there a way to handle click on logo icon? My idea is to use the home button click to clear the stack of activities and use the back button to navigate back to previous screen.

I tried with

toolbar.setNavigationOnClickListener() 

but it has no effect on back button click.

Handling android.R.id.home works when handled in

onOptionsItemSelected()
Riparian answered 24/5, 2015 at 4:21 Comment(3)
similar to this #26525729.??. Toolbar is a view group. you can have custom view and place them whereever you want.Malachite
@Raghunandan, Its not a duplicate question and I will try with a custom layout and set it to toolbar. That might work.Riparian
i never said its a duplicate. I just wanted to know how your toolbar should look. You can have custom views and place them accordingly.Malachite
C
6

For navigating back. This worked for me.

@Override 
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case android.R.id.home:
                Intent homeIntent = new Intent(this, HomeActivity.class);
                homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(homeIntent);
        } 
        return (super.onOptionsItemSelected(menuItem));
    } 
Christeenchristel answered 29/1, 2016 at 7:4 Comment(0)
C
1

try with this

toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            if (item.getItemId() == android.R.id.home) {
                getActivity().finish();
            }
            return true;
        }
    });
Cobber answered 8/8, 2015 at 10:54 Comment(0)
W
0
  1. Design our custom layout as a separate "toolbar_content.xml" and include this layout inside toolbar tag in your "main_layout.xml".
  2. Write click listeners for your items in "toolbar_content.xml" in your base activity so that listeners will be available thru out the app.
Whitman answered 24/5, 2015 at 6:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.