Search code examples
androidandroid-layoutandroid-toolbarandroid-resources

remove default margin bottom from Toolbar


In my application I use the android.support.v7.widget.Toolbar for the toolbar.

Here is my layout

<?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"
tools:context="com.todo.app.MainActivity"
style="@style/MainActivityBg">

<android.support.design.widget.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        style="@style/AppTheme.ActionBar"/>

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

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

and

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context="com.todo.app.MainActivity"
android:layout_marginTop="5dp">  <!-- for adding some space between the toolbar and the rest, and this is the cause of the problem! -->

<android.support.v7.widget.RecyclerView
android:id="@+id/task_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />

I try to add some space between the toolbar and the rest of the view. To do that I added 5dp in marginTop to the layout which is right under the view. And this causes this problem: While I scrolling the view there is some space remaining under the toolbar.

Before scrolling

After starting to scroll

In my style files xml, there is no style for margin or padding. How I can adding some space between toolbar and the rest without trigger this problem (when I scrolling the view)?


Solution

  • Reason

    Based on the xml files, it seems to me that:

    <android.support.constraint.ConstraintLayout
        ...
        android:layout_marginTop="5dp"> 
    

    is the reason for cropping RecyclerView content.

    When you set the layout_marginTop between the AppBarLayout and the upper edge of ConstraintLayout appears a 5dp high empty space.

    You can confirm that by turning on one of the Android options for developers: Show Layout Bounds.

    Solution

    For the effect which you want to achieve try to remove android:layout_marginTop and set android:paddingTop for the RecyclerView but also set android:clipToPadding to false. Check the gif in this SO answer.

    <android.support.v7.widget.RecyclerView
        android:id="@+id/task_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="5dp"
        android:clipToPadding="false" />
    

    This will allow you to add the desired space at the beginning of scrollable content but prevent cropping the items.