Cannot Catch Toolbar Home Button Click Event

Cannot catch toolbar home button click event

If you want to know when home is clicked is an AppCompatActivity then you should try it like this:

First tell Android you want to use your Toolbar as your ActionBar:

setSupportActionBar(toolbar);

Then set Home to be displayed via setDisplayShowHomeEnabled like this:

getSupportActionBar().setDisplayShowHomeEnabled(true);

Finally listen for click events on android.R.id.home like usual:

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == android.R.id.home) {
Timber.d("Home pressed");
}
return super.onOptionsItemSelected(menuItem);
}

If you want to know when the navigation button is clicked on a Toolbar in a class other than AppCompatActivity you can use these methods to set a navigation icon and listen for click events on it. The navigation icon will appear on the left side of your Toolbar where the the "home" button used to be.

toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_nav_back));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("cek", "home selected");
}
});

If you want to know when the hamburger is clicked and when the drawer opens, you're already listening for these events via onDrawerOpened and onDrawerClosed so you'll want to see if those callbacks fit your requirements.

How provide Up navigation with toolbar's home button on v7 toolbar

It's very simple. Just do the following steps:

  1. Add the toolbar to the activity_child.xml:

(In my project, this AppBarLayout is placed inside a ConstraintLayout)

<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

...

  1. Set up the toolbar in the ChildActivity.java
public class ChildActivity extends AppCompatActivity
{

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow_back_black_24dp);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

...
}


  1. set the parentActivityName property in AndroidManifest.xml
<activity android:name=".ChildActivity"
android:theme="@style/AppTheme.NoActionBar"
android:parentActivityName=".ParentActivity"/>

Important

For this to work properly, do not call finish() in the parent activity after you call startActivity(intent).

How to catch navigation icon click on toolbar from fragment?

add code block toolbar.setNavigationOnClickListener after setSupportActionBar(toolbar)

Back button of Toolbar doesn't work

Add

if(item.getItemId() ==android.R.id.home){
onBackPressed();
}

to your onOptionsItemSelected override.

This way, when you're tapping that button you'll invoke the onBackPressed method, which is the default action to go back (a cleaner solution than brutally invoking finish())

That should do it.

How to click in the Toolbar on the navigation button?

To make a click on the navigation button which is located in the toolbar you need to do the following:

Add text to the navigation button

toolbar.setNavigationContentDescription("up");

After finding it in the following way

final ArrayList<View> outViews = Lists.newArrayList();
activity.findViewById(R.id.toolbar).findViewsWithText(outViews, "up", View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
outViews.get(0).performClick();


Related Topics



Leave a reply



Submit