-1

I created custom Toolbar for my android app by Google's tutorial, but even thought it seems like my code is same as in tutorial, I have an addional margin at my toolbar.

Margin at my toolbar

Here is my layout.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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.gruber.jakub.personalscheduler.MainActivity">

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textSize="30sp"
        android:layout_centerHorizontal="true"
        android:layout_above="@+id/button"
        android:layout_marginBottom="60dp"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="@string/button1"
        android:layout_centerInParent="true" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="@android:drawable/ic_menu_today"
        android:elevation="4dp" />

</RelativeLayout>

and my styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <!--Action bar style-->
    <item name="android:actionBarStyle">@style/ActionBarTheme</item>
</style>

<style name="ActionBarTheme" parent="Widget.AppCompat.ActionBar">
    <item name="android:paddingLeft">0dp</item>
    <item name="android:paddingRight">0dp</item>
    <item name="android:paddingTop">0dp</item>
    <item name="android:paddingBottom">0dp</item>
</style>

It only looks as I want if I change android:padding... property in my layout.xml, but that's not what I would like to do, because I would have to add extra properties to all others elements.

Jakub Gruber
  • 745
  • 1
  • 11
  • 27

2 Answers2

2

Simply take a look at your container:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.gruber.jakub.personalscheduler.MainActivity">

You got padding all over the place! :

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

Those are the margins. Remove them and you are fine. I suggest you start doing some tutorials.

Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116
  • Unfortunately, I copied it here at Stackoverflow and didn't check the content. Of course it's nonsense defining padding by margin constants. Thank you. – Jakub Gruber Sep 01 '16 at 20:02
  • Actually, it's good define paddings using constants. The problem it's that you've used padding in your "AcitivyLayout"... Nowadays, google recommends you separate your Acitivty layout from your content layout. In Acitivty Layout you should put your toolbar, drawer, tabs, etc. In your content, you just put your content with the paddings you have copied. – Adley Sep 01 '16 at 20:12
  • @Adley, do you maybe have some examples of that? – Robin Dijkhof Sep 01 '16 at 21:21
  • 1
    @RobinDijkhof check this question: http://stackoverflow.com/questions/32880722/what-is-the-role-of-content-main-xml-in-android-studio-1-4 – Adley Sep 01 '16 at 21:43
  • @RobinDijkhof The code should look like this: http://pastebin.com/2WMRNJzK – Adley Sep 01 '16 at 21:44
  • Or in acitivty_main,xml instead of use RelativeLayout you can use CoordinatorLayout https://developer.android.com/reference/android/support/design/widget/CoordinatorLayout.html – Adley Sep 01 '16 at 21:46
1

The solution is quite simple. Firstly, you need to remove the padding definition for your parent RelativeLayout. And since you want these properties for the rest of the view other than the toobar create a new LinearLayout/RelativeLayout and define the properties there. Like follows

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.gruber.jakub.personalscheduler.MainActivity">

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"   
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"      
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:textSize="30sp"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="60dp"/>

       <Button
           android:id="@+id/button"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:padding="10dp"
           android:text="@string/button1"
           android:layout_centerInParent="true" />

       <android.support.design.widget.FloatingActionButton
          android:id="@+id/fab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_alignParentRight="true"
          android:src="@android:drawable/ic_menu_today"
          android:elevation="4dp" />
     </RelativeLayout>
</RelativeLayout>
Gaurav Sarma
  • 2,248
  • 2
  • 24
  • 45