Search code examples
androidxmlandroid-actionbartoolbar

How to center logo while having no action bar menu


I'm hidding my action bar base on some condition. But when it's hided, the logo inside my toolbar is not center.

How can I fix that ?

    <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/bleuClair"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:titleTextColor="@color/colorBlack"
        app:titleMarginStart="0dp">
        <ImageView
            android:padding="3dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/logo_hd"
            android:contentDescription="@string/remi_logo_desc" />
    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

Solution

  • Try This way -

    <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">
    
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/bleuClair"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:titleTextColor="@color/colorBlack"
        app:titleMarginStart="0dp">
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">
    
        <ImageView
            android:padding="3dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/logo_hd"
            android:contentDescription="@string/remi_logo_desc" />
    
    </LinearLayout>
    </android.support.v7.widget.Toolbar>