Home > Enterprise >  Is it Possible to remove child padding In Android
Is it Possible to remove child padding In Android

Time:11-10

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

CodePudding user response:

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

  • Related