Shuvo
Shuvo

Reputation: 47

Android toolbar extra gap

I have seen the answer of this question but none of them work for me. In android I have an extra gap between the margin and toolbar so it is not looking like a actionbar. How to remove that gap?activity_main image

toolbar.xml

<?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:layout_width="match_parent"
android:background="#FF7043"
android:elevation="4dp"
android:layout_height="?actionBarSize"

android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp"

android:id="@+id/tool_bar">

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

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"

android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#00E676"
tools:context="com.example.partha.customtitlebar.MainActivity">
<include
    android:id="@+id/tool_bar"
    layout="@layout/toolbar"


    ></include>


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:id="@+id/textView"
    android:layout_below="@+id/tool_bar"
    android:layout_alignParentStart="true"
    android:layout_marginTop="55dp" />
</RelativeLayout>

Upvotes: 0

Views: 755

Answers (2)

The-null-Pointer-
The-null-Pointer-

Reputation: 369

This is because of the padding you have added to the parent RelativeLayout ViewGroup.

you have two options

  • it's either you set the padding to 0dp but you will lose padding for the entire screen
  • or you nest the the TextView inside another ViewGroup like LinearLayout and apply the padding to that LinearLayout rather than the RelativeLayout. with this you get the padding for your entire content while still making sure there is no padding around the Toolbar which will make it fit the screen like the deprecated ActionBar.

This examples illustrate how you can achieve the second option

android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00E676"
tools:context="com.example.partha.customtitlebar.MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/toolbar">
</include>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tool_bar"
android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView"
android:layout_below="@+id/tool_bar"
android:layout_alignParentStart="true"
android:layout_marginTop="55dp" />
</LinearLayout>
</RelativeLayout>

Upvotes: 0

Faiz Malkani
Faiz Malkani

Reputation: 313

If you look closely, you'll notice that you've applied padding for all four sides on the Toolbar's parent, i.e, the RelativeLayout

Removing the following lines will fix it:

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

Upvotes: 1

Related Questions