Title, etc. not showing in Android Toolbar
Asked Answered
R

3

16

I've just added an Android Toolbar to an app I'm working on. The toolbar placement works correctly with the toolbar layout appearing at the top of the display. However, the toolbar is showing up as a blank colored bar...the App Title and Overflow Icon do not show up in the Toolbar. Any ideas on how to fix?

ToolbarActivity.java:

public abstract class ToolbarActivity extends ActionBarActivity {

private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    if (toolbar != null) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true); 
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    toolbar.setTitle("app name");
    }
}

toolbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:background="@color/ColorPrimary" >
</android.support.v7.widget.Toolbar>

menu_main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:orderInCategory="100"
    app:showAsAction="always" />
</menu>

style.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
      <item name="windowActionBar">false</item>
      <item name="android:windowNoTitle">true</item>
      <item name="colorPrimary">@color/ColorPrimary</item>
      <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
</style>
Recital answered 1/2, 2015 at 16:55 Comment(1)
E
28

EDIT: Ok so this needs to be set in a completely different way to work. I assumed (because I hadn't worked with the ToolBar much yet, that when using setActionBar(toolbar) it added the custom ToolBar to the content view automatically - but that wasn't the case.

So the big issue is that the ToolBar is never added to the current content view and hence will never be visible until you actually add the ToolBar to the current content view.

There are different approaches, but I'm only gonna show one of the them.

Instead of having a toolbar.xml file, you should add the ToolBar directly into the layout file of the Activity.

Here's an example of the Activity class and the XML to go together with the Activity:

MainActivity.java

public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if(toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle("My custom toolbar!");
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@android:color/holo_red_light"
        android:minHeight="100dp">

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

</RelativeLayout>

Your styles.xml file is correct as it is.

It's important to note, that after setting your ActionBar to the custom ToolBar, you use the default methods for changing how the ActionBar looks like. For instance setting the title, should be set from getSupportActionBar().setTitle() and not from toolbar.setTitle().

Experimentalize answered 1/2, 2015 at 17:1 Comment(8)
Ah, I understand. I inflated with the code you suggested and the Toolbar shows properly. But the toolbar it is still blank, just a colored bar. Any thoughts?Recital
@user3796660 I changed my answer completely - I had to dig a little around, but the above should be working correctly now - use with your own code :-) In my opinion it's a bit confusing how the ToolBar needs to be implemented to use it as the ActionBar...Experimentalize
Awesome, that did the trick. I need to do some formatting on the toolbar but the custom Title you used and the default Up arrow and Overflow symbol displayed correctly. Thank you for sharing your Android knowledge, cheers.Recital
Yeah change what you need. This was merely an example on how it can be used :-) Always happy to help ;-)Experimentalize
I get this error: "This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead." What should I do?Psychosexual
@Psychosexual look in your style.xml file. You should have this set: <item name="windowActionBar">false</item> in your current active Style.Experimentalize
Oh my! Thanks a bunch! I was looking forever for a solution to this!Perigon
As of today, using appcompat-v7:24.2.0, Toolbar can be in its own xml file and included in activity's layout. However, getSupportActionBar().setTitle("My title") is still the correct approach to set the title.Generality
D
2

Tutorial: http://www.viralandroid.com/2015/08/android-toolbar-example.html

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
  <!-- Customize your theme here. -->
</style>

layout file

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

java

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Dim answered 14/1, 2016 at 0:56 Comment(0)
O
0

In manifest, add the following...

 <activity android:name=".Activity.ToolbarActivity"
            android:label="@string/yourStringTitle"
            />

Hope this helps

Observation answered 18/1, 2017 at 3:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.