4

I have been following the official documentation on how to create an app toolbar. The tutorial says that, after following the instructions,

"Your app now has a basic action bar. By default, the action bar contains just the name of the app and an overflow menu."

Well, mine doesn't, it looks like this: enter image description here

As you can see from this image, I've done some styling to get the colours as I want them, and I've added a menu item, just to see if it appeared correctly. I'll work out how to fix the colour of the icon later.

My problem is that, as you can see, the app title isn't centred vertically. It's too close to the top. This irritates me because, as far as I can tell, it should be centered automatically and I don't know why mine isn't, but I've nevertheless tried to fix this myself using styles, with absolutely no success. No style attributes seem to have any effect on the padding of just the Toolbar Title. I can add padding to the whole Toolbar, but that affects the menu item too, and I don't want it to.

My implementation looks like this:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:fitsSystemWindows="true">
            <android.support.v7.widget.Toolbar
                android:id="@+id/main_activity_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                style="@style/MyToolbarStyle"
                app:theme="@style/MyToolbarTheme" />
    </LinearLayout>

styles.xml:

<resources>

<!-- Base application theme. -->
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="MyToolbarStyle" parent="Base.Widget.AppCompat.Toolbar">
    <item name="android:elevation">4dp</item>
    <item name="android:background">@color/colorPrimary</item>
</style>

<style name="MyToolbarTheme" parent="Base.Widget.AppCompat.Toolbar">
    <item name="android:textColorPrimary">#ffffff</item>
</style>

Ideally, if somebody could explain where I've gone wrong so that my application title isn't centered vertically, that'd be great, but if not, can somebody tell me how to add a margin or padding just to the application title in the toolbar so that I can attempt to centre it myself?

P.S. This isn't a duplicate of this question - that question deals with a more complex custom Toolbar. Mine is supposed to be the default implementation.

Update: As requested, here's the code for my MainActivity.java file:

package com.example.samplescanner;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;

public class MainActivity extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar MainActivityToolbar = (Toolbar) findViewById(R.id.main_activity_toolbar);
        setSupportActionBar(MainActivityToolbar);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_items, menu);
        return super.onCreateOptionsMenu(menu);
    }
}
Community
  • 1
  • 1
Philip Stratford
  • 4,513
  • 4
  • 45
  • 71
  • I think you're missing `android:fitsSystemWindows=“true”` on the toolbar or outer layout – OneCricketeer Dec 16 '16 at 15:30
  • Are you setting the title anywhere? – just_user Dec 16 '16 at 15:31
  • In other words, it probably is centered, but the entire toolbar is behind the status bar – OneCricketeer Dec 16 '16 at 15:33
  • Possible duplicate of [Toolbar overlapping below status bar](http://stackoverflow.com/questions/29738510/toolbar-overlapping-below-status-bar) – OneCricketeer Dec 16 '16 at 15:35
  • 1
    @cricket_007 I was missing that setting, but even now I've added it, the problem persists. I'll update my code with the full activity_main.xml code. – Philip Stratford Dec 16 '16 at 15:49
  • @just_user I am setting it in AndroidManifest.xml. It isn't really "My Application", I just changed it to that for the purposes of this question. – Philip Stratford Dec 16 '16 at 15:49
  • Post your `MainActivity` code. – Bryan Dec 16 '16 at 16:26
  • @Bryan I've updated the question with the MainActivity.java code. – Philip Stratford Dec 16 '16 at 16:40
  • Hm, I cannot seem to replicate the issue. Does anything happen if you remove the `theme` and `style` attributes? – Bryan Dec 16 '16 at 16:54
  • @Bryan Removing the `theme` and `style` attributes does correct the issue, and a process of elimination reveals that it's the `theme` attribute causing the problem - without it, the app title is correctly positioned. As you can see, there's only one attribute specified in MyToolbarTheme, so I'm not sure what's causing the title not to be centered, but then again I am finding Android's themes and styles to be Machiavellian. – Philip Stratford Dec 16 '16 at 17:00
  • Ok, I've got it, thanks to @Bryan. In my `styles.xml` file, changing the `parent` attribute of the MyToolbarTheme style to `@style/ThemeOverlay.AppCompat.Light` fixed the issue. – Philip Stratford Dec 16 '16 at 17:13

1 Answers1

5

I couldn't find the exact source of the issue from Base.Widget.AppCompat.Toolbar, but it alters the Gravity of the Toolbar. Inherit from ThemeOverlay.AppCompat.Dark.ActionBar instead:

<style name="MyTheme.ToolbarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:background">@color/colorPrimary</item>
    <item name="android:elevation">4dp</item>
</style>

They use ThemeOverlay.AppCompat.ActionBar in the documentation you mentioned, but the Dark version should set the textColorPrimary attribute for you.

Bryan
  • 14,756
  • 10
  • 70
  • 125