Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appcompat Toolbar "android:title" attribute not working

I'm sure this is something very simple that I'm just overlooking. If I set the title in code it all seems to work fine:

import android.support.v7.widget.Toolbar;
...
// Works fine
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("My Title");

Setting it in the layout xml doesn't work:

<!-- Doesn't work -->
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:title="@string/my_title"/>

It's worth noting I'm using the AppCompat v7 Library and testing against android sdk version 18.

like image 787
Gyroscope Avatar asked Nov 20 '14 10:11

Gyroscope


2 Answers

You aren't using the correct attribute. I think the Action Bar docs explain this the best.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:showAsAction="ifRoom"  />
    ...
</menu>

Using XML attributes from the support library

Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.

In other words, you need to change android:title to yourCustomPrefix:title

<YourRootViewGroup xmlns:app="http://schemas.android.com/apk/res/org.seeingpixels.photon"
    xmlns:android="http://schemas.android.com/apk/res/android"
    ... >

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

</YourRootViewGroup>
like image 62
adneal Avatar answered Nov 14 '22 23:11

adneal


Can you try this code?

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle("title");
like image 32
msevgi Avatar answered Nov 14 '22 23:11

msevgi