Bilal Hussain
Bilal Hussain

Reputation: 149

how to place logo in the center of actionbar toolbar in android?

I want to display the logo at the centre of the Action Bar. Here's my layout code: the below code should work but it did not why? please help me

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo"
        android:layout_gravity="center"
        />

</LinearLayout>

And in the onCreate()

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.custom_logo);

Upvotes: 0

Views: 1613

Answers (1)

Anirudh Sharma
Anirudh Sharma

Reputation: 7974

Your Toolbar is just like a ViewGroup that can hold other views.So an easier approach will be to place an ImageView in the Toolbar.For eg:-

    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/material_blue"
        android:elevation="5dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark"
        app:theme="@style/ToolbarCustomIconColor" >
             <RelativeLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_content" >
                 <ImageView
                     android:id="@+id/imgLogo"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_centerInParent="true"
                     android:src="@drawable/your_icon" />
         </RelativeLayout>
    </android.support.v7.widget.Toolbar>

You can create a custom Toolbar like this and manipulate it as much as you want .

Upvotes: 2

Related Questions