I want to show back button in toolbar under some circumstances, and hide it under another. I wrote below code:
public void setupToolbar() {
if (mStacks.get(mCurrentTab).size() > 1) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
toolbar.setNavigationOnClickListener(v -> onBackPressed());
} else {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
}
}
But my problem is when I set home button as false, It won't be visible anymore even the first condition becomes true. Can anybody help me?
Show/Hide Back Button in Toolbar. please try it
show code
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
and hide code
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With