20

I want to create a toolbar like the following image as proposed in the material design guidelines:

enter image description here

I can achieve this via using the toolbar with an relative layout:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Toolbar/>

        <LinearLayout android:layout_marginTop="-17dp" />

</RelativeLayout>

I am not sure this is the correct way or not. Any help is appreciated.

2 Answers 2

22

Thanks Gabriele for all the help. Here is working code:

Activity :

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mai);

        Toolbar toolbar = (Toolbar) findViewById(R.id.card_toolbar);
        if (toolbar != null) {
            // inflate your menu
            toolbar.inflateMenu(R.menu.main);
            toolbar.setTitle("Card Toolbar");
            toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    return true;
                }
            });
        }

        Toolbar maintoolbar = (Toolbar) findViewById(R.id.toolbar_main);
        if (maintoolbar != null) {
            // inflate your menu
            maintoolbar.inflateMenu(R.menu.main);
            maintoolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    return true;
                }
            });
        }
    }

}

Layout XML File:

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_main"
        android:layout_width="match_parent"
        android:layout_height="@dimen/action_bar_size_x2"
        android:background="@android:color/holo_orange_dark"
        android:minHeight="?attr/actionBarSize" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar_main"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="@dimen/action_bar_size"
        android:orientation="vertical" >

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <android.support.v7.widget.Toolbar
                    android:id="@+id/card_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/action_bar_size"
                    android:background="@android:color/white" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@android:color/darker_gray" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp"
                    android:text="@string/app_name"
                    android:textSize="24sp" />
            </LinearLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>

</RelativeLayout>

Make sure your activity theme is extending Theme.AppCompat.Light.NoActionBar.

Here is what it will look like:

enter image description here

Few things to note:

  • If you are using card elevation then you need to alter the margin top so that your card aligns with the main toolbar.
  • I am still seeing 1-2 pixel margin between bottom of main toolbar and card toolbar. Not sure about what to do in this case. For now, I am aligning it manually.
5
  • Hi.. What is value of @dimen/action_bar_size_x2 and @dimen/action_bar_size. Because my cardview is not overlapping first toolbar.
    – Sanket
    Jan 9, 2015 at 11:41
  • x2 is double the value, you use for action bar size.
    – mudit
    Jan 9, 2015 at 15:52
  • It doesn't render like the screenshot. Are you sure about android:layout_below="@+id/toolbar_main" ? I would remove it to make the first LinearLayout to start at the top of the screen...
    – tbruyelle
    Apr 23, 2015 at 12:42
  • yes.. this is an actual screenshot from the device.
    – mudit
    Apr 27, 2015 at 4:46
  • I use this instead of manual adjusting main toolbar's height: final ViewGroup.LayoutParams lp = mainToolbar.getLayoutParams(); lp.height += ((ViewGroup.MarginLayoutParams) card.getLayoutParams()).topMargin; mainToolbar.setLayoutParams(lp); Dec 29, 2015 at 10:36
11

You can obtain it using a Toolbar as ActionBar, a CardView and another Toolbar(standalone) inside the Card.

For the Toolbar standalone inside a Card you can use something like this:

<android.support.v7.widget.CardView>

   <LinearLayout>

        <Toolbar  android:id="@+id/card_toolbar" />

        //......

   </LinearLayout>

</CardView>

Then you can inflate your menu to obtain the icon actions.

 Toolbar toolbar = (Toolbar) mActivity.findViewById(R.id.card_toolbar);
   if (toolbar != null) {
         //inflate your menu    
         toolbar.inflateMenu(R.menu.card_toolbar);
         toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                         //.....
                    }
                });
   }

For the toolbar used as action bar and the main layout you can use:

option1:

<RelativeLayout>

    <toolbar/>  //Main toolbar 

    <View
        android:background="@color/primary_color"
        android:layout_height="@dimen/subtoolbar_height"/>

    <CardView /> //described above

</RelativeLayout>

Option2: An extended toolbar (as actionbar) and a CardView as described above playing with margins.

3
  • I want opposite of that (not sure exactly opposite).. my card view should overlay on toolbar.. the toolbar should not come inside cardview.
    – mudit
    Nov 10, 2014 at 17:25
  • In the screen you have a toolbar used as actionbar, and another toolbar (standalone) inside the card. Nov 10, 2014 at 17:28
  • 1
    ohh.. so basically you are saying i have to use 2 toolbars.
    – mudit
    Nov 10, 2014 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.