George
George

Reputation: 119

Android Layouts: Why do my buttons have automatic padding and how can I get rid of it?

Button with implicit padding

Here is an example. For some reason there seems to be implicit padding in the button. I tried setting padding, minWidth, and minHeight to 0dp in the root level RelativeLayout but for some reason the padding still persists. I also tried putting these attributes in each subview like all the buttons and TextViews but still nothing. Any idea what's going on here?

<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:padding="0dp"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:background="@drawable/splash_screen"
    tools:context=".MainActivity">

            <!--
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"-->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="30dp">


        <ImageView
            android:id="@+id/navdy_logo"
            android:src="@drawable/ic_navdy_logo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="false"
            android:paddingLeft="58dp"
            android:paddingRight="58dp"
            android:contentDescription="navdy logo">
        </ImageView>
        <!-- android:scaleType="fitXY" -->
        <!-- android:adjustViewBounds="true" -->

        <Space
            android:id="@+id/space"
            android:layout_below="@+id/navdy_logo"
            android:layout_height="55dp"
            android:layout_width="fill_parent">
        </Space>

        <TextView
            android:id="@+id/titleText"
            android:layout_below="@+id/space"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:gravity="center"
            android:textSize="30sp"
            android:fontFamily="sans-serif-light"
            android:textColor="#fff"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Design to change your driving experience"
            android:layout_centerHorizontal="true" />

        <Space
            android:id="@+id/space2"
            android:layout_below="@+id/titleText"
            android:layout_height="32dp"
            android:layout_width="fill_parent" >
        </Space>

        <Button
            android:id="@+id/create_account_button"
            android:layout_below="@+id/space2"
            android:layout_centerHorizontal="true"
            android:layout_height="50dp"
            android:layout_width="283dp"
            android:text="@string/create_account"
            android:textSize="18sp"
            android:textColor="#fff"
            android:backgroundTint="@color/md_blue_500">

        </Button>

        <Space
            android:id="@+id/space3"
            android:layout_below="@+id/create_account_button"
            android:layout_height="16dp"
            android:layout_width="fill_parent">
        </Space>

        <Button
            android:id="@+id/learn_more_button"
            android:layout_height="46dp"
            android:layout_width="279dp"
            android:scaleType="fitXY"
            android:textSize="18sp"
            android:background="@drawable/button_border_white"
            android:text="@string/learn_more"
            android:textColor="#fff"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="0dp"
            android:layout_below="@+id/space3"
            android:layout_alignEnd="@+id/create_account_button">

        </Button>

        <Space
            android:id="@+id/space4"
            android:layout_below="@+id/learn_more_button"
            android:layout_height="24dp"
            android:layout_width="fill_parent">
        </Space>


        <RelativeLayout
            android:layout_below="@+id/space4"
            android:layout_width="270dp"
            android:layout_height="20dp"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"
            android:layout_centerHorizontal="true"
            android:id="@+id/bottom_text_for_signup">
            <TextView
                android:id="@+id/already_have_an_account_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_marginRight="15dp"
                android:textSize="17sp"
                android:fontFamily="sans-serif-light"
                android:textColor="#fff"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="@string/already_have_account"
                android:layout_centerHorizontal="true" />

            <TextView
                android:id="@+id/already_have_account_sign_in"
                android:text="@string/sign_in"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:textSize="17sp"
                android:fontFamily="sans-serif-light"
                android:textColor="@color/md_blue_500"
                android:textAppearance="?android:attr/textAppearanceLarge" />

        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>

There seems to be top and bottom padding on my TextViews as well:

TextView with implicit padding

EDIT:

Anand Singh mentioned dimens.xml in the comments. I actually have no idea what this resource does and decided to check it out. Here's the code:

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
</resources>

However, I never reference this in my layout so I don't understand how its being used? Moreover, even when I comment out these stylings in the dimens.xml file, I still get the same padding problem.

Upvotes: 0

Views: 121

Answers (1)

Damian Kozlak
Damian Kozlak

Reputation: 7083

Problem is now with padding or margin, but with drawable for Button. Instead of setting negative values, you can create own Button using 9-patch: http://developer.android.com/tools/help/draw9patch.html

Upvotes: 1

Related Questions