0

No matter what I do I can't set the title in a support toolbar. The only thing on the toolbar is an icon regardless of if I set it.

Followed the guidelines here: http://android-developers.blogspot.de/2014/10/appcompat-v21-material-design-for-pre.html

I have tried every combination of getSupportActionBar.set... and none of them alter the toolbar at all.

Activity:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.gallery);
    mToolbar = (Toolbar) findViewById(R.id.galleryToolbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setSubtitle("blah blah");
    getSupportActionBar().setTitle("blah");
    getSupportActionBar().setLogo(R.drawable.icon);
    ...
}

Layout:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/galleryLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:id="@+id/galleryToolbar"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary">

</android.support.v7.widget.Toolbar>
...

Theme:

<style name="GalleryTheme" parent="@style/Theme.AppCompat.NoActionBar">
    <item name="windowActionModeOverlay">true</item>
    <item name="android:windowBackground">@android:color/black</item>
    <item name="showcaseViewStyle">@style/CustomShowcaseTheme</item>
    <item name="actionOverflowButtonStyle">@style/Custom.Widget.ActionButton.Overflow</item>
</style>
Anthony
  • 7,638
  • 3
  • 38
  • 71
  • 1
    well what about `mToolbar.setTitle` – tyczj Apr 08 '15 at 20:39
  • `getSupportActionBar().setTitle("blah);` typo in the question, or in your code? – stkent Apr 08 '15 at 20:43
  • @tyczj It's not well documented but support toolbar is to be used in this manner. – Anthony Apr 08 '15 at 21:09
  • @Anthony it is not wrong to do that, either way is correct – tyczj Apr 08 '15 at 21:36
  • SO and Goog claim differently: https://code.google.com/p/android/issues/detail?id=77763 http://stackoverflow.com/questions/28265208/title-etc-not-showing-in-android-toolbar I think the main issue is to use the action item inflation you must set the toolbar and there are quite a few oddities as to how it behaves at that point. – Anthony Apr 08 '15 at 22:11

1 Answers1

1

If you donot use setSupportActionBar(mToolbar) to set the Toolbar as the actionbar, the below code snippet will let you change the Title on the Support Toolbar.

Use the mToolbar instance to call the setTitle() method.

 mToolbar = (Toolbar) findViewById(R.id.galleryToolbar);

    if (mToolbar != null) {
        mToolbar.setTitle("My Title");
    }

Moreover , call other methods on the mToolbar instance to customise further.

For more info check the developer docs

Nishant Srivastava
  • 4,723
  • 2
  • 24
  • 35
  • That will not work for support toolbar, I'm calling it in the manner described by the documentation. – Anthony Apr 08 '15 at 21:06
  • Updated my answer with the developer docs link. I am using the Support Toolbar myself and this is the way it works. Checkout the setTitle() method in the developer docs. – Nishant Srivastava Apr 08 '15 at 21:14
  • That's odd, because these posts claim differently: https://code.google.com/p/android/issues/detail?id=77763 http://stackoverflow.com/questions/28265208/title-etc-not-showing-in-android-toolbar How are you styling the toolbar? – Anthony Apr 08 '15 at 21:19
  • Found the difference.Updated my answer. – Nishant Srivastava Apr 08 '15 at 21:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74759/discussion-between-radix-and-anthony). – Nishant Srivastava Apr 08 '15 at 21:34