0

I have a custom view with a background drawable.

The drawable has an inside shape with padding. No padding is applied on the view itself.

Whenever I set a padding on the view programmatically using View.setPadding(left,top,right,bottom), the shape padding gets changed too and I honestly cannot figure out why.

The xmls are defined as follows:

Background drawable

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="?android:attr/colorControlHighlight">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="@dimen/radius_large" />
            <solid android:color="@color/yellow" />
            <padding
                    android:left="@dimen/spacing_large"
                    android:right="@dimen/spacing_large"
                    android:top="@dimen/spacing_medium_to_large"
                    android:bottom="@dimen/spacing_medium_to_large" />
        </shape>
    </item>
</ripple>

Custom view extending ConstraintLayout (edited)

The tools: attrs on the <merge> tag are simply used to render what the outcome will be in the layout preview. As you know, <merge> is not a viewgroup, so it doesn't allow attrs such as background etc

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout"
        tools:background="@drawable/background_start_lesson"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <ImageView
            ... />

    <ImageView
            ... />

    <TextView
            ... />


</merge>

In the constructor of my custom view, I simply set the R.drawable.background_start_lesson

And this of course just renders nicely as expected, with large paddings:

Custom view with padding set on the shape

.....but if I just set the padding of the view itself to 0dp programmatically or via xml (and it should be already at 0dp, given that I specified none on the view!), this is what happens:

Custom view with 0dp padding set programmatically

In conclusion, why does setPadding() change the <shape> drawable padding and not the padding of the view?

reavcn
  • 300
  • 2
  • 14
  • the padding amount will sum up. what is the type of your custom view? if you use imageView you won't have this issue. – Elias Fazel Dec 04 '19 at 14:38
  • @EliasFazel The type of the custom view is ConstraintLayout (you can see it from the tools:parentTag in ). As you can see, it is not summing up – reavcn Dec 04 '19 at 15:56

0 Answers0