In Android App Toolbar.Settitle Method Has No Effect - Application Name Is Shown as Title

In android app Toolbar.setTitle method has no effect – application name is shown as title

Found the solution:

Instead of:

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);            
mActionBarToolbar.setTitle("My title");
setSupportActionBar(mActionBarToolbar);

I used:

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);            
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("My title");

And it works.

getSupportActionBar().setTitle() vs toolbar.setTitle()

If you call setSupportActionBar(Toolbar),
then the Action Bar is then responsible for handling the title, therefore you need to call getSupportActionBar().setTitle("My Title"); to set a custom title.

Also check this link where toolbar.setTitle("My title"); may cause problem like below:-
In android app Toolbar.setTitle method has no effect – application name is shown as title

And toolbar is the general form of action bar.

We can have multiple toolbars as layout widget but action is not.

Thus better approach is to use getSupportActionBar().setTitle("My Title");

How to change the toolbar's title in android app?

Try to use this.setTitle(title); in onCreate() method.

how to set title in toolbar?

You can set default Toolbar title as following:

YourActivity.this.setTitle("Your Title");

For fragment:

getActivity().setTitle("Your Title");

Why Application name shown by it self in app bar? And how to remove it?

to remove title from action bar try this

Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");

try this to change actionbar title

Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("your title");// set your title name


Related Topics



Leave a reply



Submit