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()
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Satish Navada
  • 371
  • 1
  • 2
  • 9
  • similar to this http://stackoverflow.com/questions/26525229/toolbar-navigation-icon-never-set.??. Toolbar is a view group. you can have custom view and place them whereever you want. – Raghunandan May 24 '15 at 04:45
  • @Raghunandan, Its not a duplicate question and I will try with a custom layout and set it to toolbar. That might work. – Satish Navada May 24 '15 at 04:46
  • 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. – Raghunandan May 24 '15 at 04:47

3 Answers3

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));
    } 
Vinay
  • 1,284
  • 14
  • 24
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;
        }
    });
Son Nguyen Thanh
  • 1,199
  • 15
  • 19
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.