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:
.....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:
In conclusion, why does setPadding() change the <shape>
drawable padding and not the padding of the view?