3

I have a linear layout. In Parent's view, I have padding defined. But I want to remove the padding from the child view. Is it possible to remove middle child view padding?. I know one solution is that I need to give padding separately. But i don't want to use this solution. So any alternative solution.

Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tool="http://schemas.android.com/tools"
    android:padding="16dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:visibility="gone"
        tool:text="Header text" />

    <View
        android:id="@+id/separator"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginTop="10dp"
        android:background="#cccccc"
        android:visibility="gone"
        app:layout_constraintTop_toBottomOf="@id/header" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/dialog_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="5dp" />

</LinearLayout>

Above code looks like this

enter image description here

Expected Output

enter image description here

Kotlin Learner
  • 3,995
  • 6
  • 47
  • 127
  • The answer is not to put padding on the parent if you don't want it to be universal. Instead, put margin on the elements. If this is hard because there's a lot of elements, use a second layout to group them and put it on the group. – Gabe Sechan Nov 08 '21 at 17:17

1 Answers1

4

This could be done adding in the LinearLayout

android:clipToPadding="false"

and setting negative horizontal margins in the View:

android:layout_marginStart="-16dp"
android:layout_marginEnd="-16dp"

This works because you have a LinearLayout, it seems that negative margins are not supported for other ViewGroups than LinearLayout and RelativeLayout as this answer says: https://stackoverflow.com/a/10673572/7794806

Jesús Barrera
  • 400
  • 1
  • 5
  • 15
  • 1
    That link is out of date. _ConstraintLayout_ supports negative margins as of 210-alpha-2 see [here](https://androidstudio.googleblog.com/2020/12/constraintlayout-210-alpha-2.html). – Cheticamp Nov 08 '21 at 18:42
  • @JesúsBarrera it work great. Can you explain what `android:clipToPadding` is doing ? – Kotlin Learner Nov 09 '21 at 09:34
  • @vivekmodi Setting clipToPadding=false avoids that the children of the LinearLayout be trimmed when they move outside the limits imposed by the padding, this can occur during scrolling or animation. I've seen this is often used in recycler view. Thanks for accepting my answer. – Jesús Barrera Nov 10 '21 at 08:50
  • @JesúsBarrera why it's not working in programticall? – Kotlin Learner Nov 18 '21 at 11:56