Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android toolbar: how to have a toolbar with image in the background and menu items on top

Using material design how to create a toolbar with background image.

the following is what i want:

toolbar with image and overflow and search menu icon.

I am trying the following code:

<?xml version="1.0" encoding="utf-8"?>
<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/actionBar"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp" 
    android:minHeight="200dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageViewplaces"
        android:src="@drawable/puri" 
        android:layout_gravity="center"/>

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

and what i am getting is:

enter image description here

like image 372
Santhosh Avatar asked Sep 16 '15 11:09

Santhosh


2 Answers

If you insist on using an ImageView over using the .setBackground(...) function.

You can try using a RelativeLayout with the Toolbar overlaying an ImageView like this:

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

    <ImageView
        android:id="@+id/imageViewplaces"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/YOUR_IMAGE" />

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar_actionbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

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


</RelativeLayout>
like image 136
RWIL Avatar answered Oct 12 '22 13:10

RWIL


Instead of adding an element with the image to the toolbar add the image as background of the toolbar, code below.

<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/actionBar"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp"
    android:background="@drawable/puri"
    android:minHeight="200dp">

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

If you want to change it in java code, you can call toolbar.setBackground(ContextCompat.getDrawable(yourcontext, R.drawable/puri);

like image 40
yennsarah Avatar answered Oct 12 '22 13:10

yennsarah