Search code examples
javaandroidandroid-actionbarandroid-manifestandroid-toolbar

Android - How to set title for custom Toolbar in Manifest file? Label attribute doesn't work


I'm using a custom Toolbar widget in my app, I found that the label attribute in Manifest file doesn't work. Any suggestion except using "setTitle()" method in Activity/Fragment? Thank you.

<activity
            android:name=".CartActivity"
            android:label="@string/cart_activity_label"
            android:parentActivityName=".CatalogActivity" />

I wish the title can be shown by using attributes in Manifest file.


Solution

  • Ok...I just found out that I forgot to link the View (Toolbar) object to the View id. Follwing is the complete setup for using a Toolbar as a default ActionBar:

    private Toolbar toolbar;

    toolbar = findViewById(R.id.toolbar); //I missed this

    setSupportActionBar(toolbar);

    • Toolbar title explain:

      1. You change the default system style to ".NoActionBar", the default ActionBar disappears.
      2. You add a custom Toolbar to the Layout, an "ActionBar" without title appears.
      3. There is no title on the Toolbar even though you've already set the label attribute in Manifest file, because the Toolbar is not recognized as default ActionBar by the system, that's why we need to use "setSupportActionbar()" method to make it work like a normal ActionBar.
      4. Don't forget to link the View of the Toolbar.