12

I try to understand the default background of Button (android 26).

sdk/platforms/android-26/data/res/drawable/btn_default_material.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="?attr/colorControlHighlight">
    <item android:drawable="@drawable/btn_default_mtrl_shape" />
</ripple>

references:

sdk/platforms/android-26/data/res/drawable/btn_default_mtrl_shape.xml:

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
       android:insetLeft="@dimen/button_inset_horizontal_material"
       android:insetTop="@dimen/button_inset_vertical_material"
       android:insetRight="@dimen/button_inset_horizontal_material"
       android:insetBottom="@dimen/button_inset_vertical_material">
    <ripple android:color="?attr/colorControlHighlight">
        <item>
            <shape android:shape="rectangle"
                   android:tint="@color/btn_colored_background_material">
                <corners android:radius="@dimen/control_corner_material" />
                <solid android:color="@color/white" />
                <padding android:left="@dimen/button_padding_horizontal_material"
                         android:top="@dimen/button_padding_vertical_material"
                         android:right="@dimen/button_padding_horizontal_material"
                         android:bottom="@dimen/button_padding_vertical_material" />
            </shape>
        </item>
    </ripple>

</inset>

I understand the inset. That is the space around the button background image. I don't understand the padding. There is no text in the background image.

To give an approach to think about it: What would happen if you remove the padding tag?

You may come to the conclusion that nothing would happen (I am not sure, that nothing would happen). But if nothing happens, you have the question what is it useful for? Why did they place it there?

Blcknx
  • 1,921
  • 1
  • 12
  • 38

4 Answers4

6

This padding will be applied to the view to which you set the background on top of the actual padding applied. If you set this background to buttons, then the text area will be reduces by this amount.

Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
  • I dont believe padding reduces the text's dimensions amount, but instead pads it with empty space and increases the total amount of space used by the button – petey Jun 08 '18 at 20:14
  • @petey: Yes, more padding should increase the space, if the padding is applied to the `Button` **tag**. Then the padding is added to the space of the text. However, this question is not about the `Button` tag, but about the button background drawable, which in turn should fill the space resulting from the `Button` tag. So I still don't understand the use of padding in the background drawable. But they put it there for some purpose. – Blcknx Jun 09 '18 at 06:22
  • @petey: I think you are referring to the padding applied to views but OP is asking about the padding element in shape drawable. Whatever, What you refer to is called margin. Padding behaved exactly like i said. – Bertram Gilfoyle Jun 09 '18 at 09:51
  • 1
    @Blcknx: I am referring to the padding element in shape drawable itself. It behaves exactly like I said. See the [doc](https://developer.android.com/guide/topics/resources/drawable-resource#padding-element). I think you tested it by putting this to a layout and adding a child view. It won't work since it is not same as the padding we apply to views. You can verify it by creating a new shape drawable of one color with padding and applying it to an ImageView. If you set the ImageView's src as another color, you can clearly see how the padding affected. – Bertram Gilfoyle Jun 09 '18 at 09:58
  • 1
    I give the points here for finding the official documentation of `padding` inside `shape`. The official documentation is not really satisfying in it's explanation. Unfortunately you provide the link to it only in your last comment. However this is the best hint given. – Blcknx Jun 11 '18 at 19:22
  • @Blcknx Thanks. Please accept the answer if you think it answered your questions completely. If you have something more to add to your question, please specify what. ☺ – Bertram Gilfoyle Jun 11 '18 at 23:18
  • It's still a strange concept to me, that a background image should add padding to the foreground text and I still doubt it really does. An ideal answer would contain some code and screenshots of the resulting behaviour. – Blcknx Jun 12 '18 at 07:26
2

But you are going to enter the text in the button to there must be the specific padding around your button text try to set your own background then set the large text there will no paddind around your text this is for the button look and feel.

2

It is explained the usage of paddings in this page of developers guide website: https://developer.android.com/guide/topics/ui/declaring-layout

In the middle of page is written:

Size, Padding and Margins

The size of a view is expressed with a width and a height. A view actually possess two pairs of width and height values.

The first pair is known as measured width and measured height. These dimensions define how big a view wants to be within its parent. The measured dimensions can be obtained by calling getMeasuredWidth() and getMeasuredHeight().

The second pair is simply known as width and height, or sometimes drawing width and drawing height. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values may, but do not have to, be different from the measured width and height. The width and height can be obtained by calling getWidth() and getHeight().

To measure its dimensions, a view takes into account its padding. The padding is expressed in pixels for the left, top, right and bottom parts of the view. Padding can be used to offset the content of the view by a specific number of pixels. For instance, a left padding of 2 will push the view's content by 2 pixels to the right of the left edge. Padding can be set using the setPadding(int, int, int, int) method and queried by calling getPaddingLeft(), getPaddingTop(), getPaddingRight() and getPaddingBottom().

And in this post you can find more explanations about padding and its differences with margins: https://stackoverflow.com/questions/21959050/android-beginner-difference-between-padding-and-margin

Hossein Seifi
  • 1,380
  • 11
  • 29
  • 2
    Thank you. As I understand your answer refers to layouts. The question refers to a drawable, androids default button background. – Blcknx Jun 09 '18 at 05:48
  • No of course not. It is about the views. As google says: The View objects are usually called "widgets" and can be one of many subclasses, such as Button or TextView. – Hossein Seifi Jun 09 '18 at 11:45
1

Padding is only applied in any container if you are using padding in any layout it will provide the space to you which want.

If you want to space around the content just use android:padding otherwise you can give custom padding. If you are using padding without any layout or container then it will not work. The only thing that will work in this case is margin.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Vishal Sharma
  • 1,051
  • 2
  • 8
  • 15