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>
Add the image to the drawable folder in resources. Set the image in the drawable attribute of the toolbar in the activity_main. xml file. Make sure the image and the toolbar have the same dimensions.
In LinearLayout , use: android:layout_gravity="center" . In RelativeLayout , use: android:layout_centerInParent="true" .
For example, you can make an ImageView act like a simple Button by adding android:onClick to the ImageView . In this task you make the images in your layout clickable.
You can do it as following it works for me :
<androidx.appcompat.widget.Toolbar
android:id="@+id/mainToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="@dimen/abc_action_bar_default_height_material">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/headerLogo"
android:contentDescription="@string/app_name" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
You need to use the AppBarLayout widget with the Toolbar widget and setting the contentInsetStart
to 0dp
. Otherwise the Toolbar will about 8dp of padding at the start (to insert the navigation buttons if needed)
Here's the working code:
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:fitsSystemWindows="false"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:contentInsetStart="0dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="@drawable/company_logo" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With