Search code examples
androidxmlandroid-actionbarandroid-toolbar

Android setLogo isnt placed on the center of Action Bar


Hi I am using a toolbar and trying to set a logo to the center of action bar. But in the center there was title and (I think) thats why logo was starting from the left side of the action bar. Then I removed title to place the logo in center but it did no good. How can I solve this?

Here is the method I use to set the toolbar.

 private void setToolBar(){
       toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setBackgroundColor(getResources().getColor(colorActionBar));
        toolbar.setLogo(R.drawable.logoactionbar);

    }

Here is the theme in my manifest

 android:theme="@style/Theme.AppCompat.Light.NoActionBar">

And here is the decleration of toolbar

      <<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorActionBar"
    android:fitsSystemWindows="true"
    tools:context="com.anadolusigorta.rhextranet.com.anadolusigorta.rhextranet.HomeActivity">

    <View
        android:id="@+id/statusBarBackground"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp"
        android:background="@color/colorActionBar"
        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/colorActionBar"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/logoactionbar"
            android:layout_gravity="center"
            android:id="@+id/toolbar_title" />

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

    <include layout="@layout/content_home" />

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

Solution

  • Try this:

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">
    
        <LinearLayout
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <ImageView
                android:src="@android:drawable/btn_star_big_off"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    
    
    
        </LinearLayout>
    
    </android.support.v7.widget.Toolbar>