Search code examples
androidandroid-actionbar

Add image in background of Toolbar in Android


I'm trying to do something like this:

My current code of toolbar:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/MyTheme.ActionBarStyle"
    android:background="@android:color/transparent">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="-10dp"
        android:gravity="center">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/explore_morning"
            android:contentDescription="@string/app_name" />
    </RelativeLayout>

</android.support.v7.widget.Toolbar>

My image is not covering whole screen


Solution

  • Instead of adding new image view, setting background of toolbar has resolved my problem.

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/explore_morning"
        android:fitsSystemWindows="true"
        android:minHeight="64dp"
        app:theme="@style/MyTheme.ActionBarStyle">
    
    
    </android.support.v7.widget.Toolbar>