Been trying to center a logo inside my toolbar. When i navigate to the next activity, the "up affordance" icon is present, and it will push my logo slightly to the right. How can I keep my logo at the center of the toolbar, without removing the up affordance icon?
This is my toolbar tag
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
android:paddingTop="@dimen/app_bar_top_padding"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
app:theme="@style/CustomToolBarTheme">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_android_logo"
android:layout_gravity="center"
android:paddingBottom="3dp"/>
</android.support.v7.widget.Toolbar>
The easiest solution that works even with menu actions present is this:
In javadoc, it says:
It means you cannot do it unless you create a custom
toolbar
or remove theicon
.I am bit late to answer this , but have wonderful solution
and here is my toolbar_background.xml drawable
You need to use the AppBarLayout widget with the Toolbar widget and setting the
contentInsetStart
to0dp
. Otherwise the Toolbar will about 8dp of padding at the start (to insert the navigation buttons if needed)Here's the working code:
Also, make sure you use
?attr/actionBarSize
as the height for the toolbar as this is the default actionBar height and will adjust to all the different screen sizes.You can do it as following it works for me :
For the devs who are using vector drawable or SVG images.
This can be your background for toolbar, drawable_toolbar_background.xml
and Yes, you have to fix the width and height of item inside drawable, although the vector drawable is already having fixed size.
This is your toolbar view,