It's not just about .setTitle
more methods of Support Toolbar (Appcompat v7) in onCreate
works only with
getSupportActionBar().method()
and don't work with mToolbar.method()
examples:
getSupportActionBar().setTitle("toolbar title");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
though next methods works fine without getSupportActionBar()
in onCreate
mToolbar.setVisibility(View.VISIBLE);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//
}
Problem only with onCreate
event, you still can use mToolbar.setTitle()
later instead of annoying getSupportActionBar().setTitle()
, for example if you add this in onCreate
it will work (because it will be executed later, after onCreate
)
mHandler.post(new Runnable() {
@Override
public void run() {
mToolbar.setTitle("toolbar title");
}
});
I prefer to use this solution https://stackoverflow.com/a/35430590/4548520 than https://stackoverflow.com/a/26506858/4548520 because if you change title many times (in different functions) it's more comfortable to use mToolbar.setTitle()
than longer getSupportActionBar().setTitle()
one and you don't see annoying notification about null exception like with getSupportActionBar().setTitle()