Invalid attribute for Android app title bar

I am sure that this is something very simple that I just don’t notice. If I set the header in the code, everything seems to work fine:

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

Setting in xml layout does not 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 is worth noting that I am using the AppCompat v7 library and testing the android sdk version 18.

+6
source share
3 answers

You are not using the correct attribute. I think Action Bar docs explain this 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

Note that the showAsAction attribute above uses its own namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, since these attributes do not exist on the Android platform on older devices. Therefore, you should 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> 
+8
source

Can you try this code?

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayShowTitleEnabled(true); getSupportActionBar().setTitle("title"); 
+1
source

Use the following code for the given header:

 toolbar = (Toolbar) findViewById(R.id.toolbar); this.setSupportActionBar(toolbar); ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setHomeButtonEnabled(true); 
-1
source

Source: https://habr.com/ru/post/978453/


All Articles