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:
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);
}
}