N Sharma
N Sharma

Reputation: 34497

How to set the padding of all views without adding viewgroup

Hi I want to add padding to my all views android:padding="16dp" except toolbar. I can't set this on ConstraintLayout as this will apply it on toolbar too.

I can add one more relative layout and add padding there but it is increasing a hierarchy of my layout which is bad design. Does anyone know how to deal with it?

Adding android:padding="16dp" on each view is correct approach here ?

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.app.android.registration.RegistrationActivity"
    tools:ignore="missingPrefix">

    <include
        android:id="@+id/toolbar_registration_layout"
        layout="@layout/toolbar" />

    <TextView
        android:id="@+id/textview_registration_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="@font/lato_medium"
        android:gravity="center"
        android:padding="16dp"
        android:text="@string/registration_title"
        android:textColor="@color/primary_text"
        android:textSize="22sp"
        app:layout_constraintTop_toBottomOf="@id/toolbar" />

    <android.support.design.widget.TextInputLayout
        android:id="@+id/til_registration_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColorHint="@color/secondary_text"
        app:hintTextAppearance="@style/MyTextInputLayoutStyle"
        android:padding="16dp"
        app:layout_constraintTop_toBottomOf="@id/textview_registration_title">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/registration_name"
            android:imeOptions="actionNext"
            android:textColor="@color/primary_text" />
    </android.support.design.widget.TextInputLayout>


</android.support.constraint.ConstraintLayout>

Upvotes: 0

Views: 444

Answers (1)

nomag
nomag

Reputation: 300

You may define left/right/top/bottom Guidelines with 16dp each and align child views w.r.t guidelines instead of parent.

For more information on Guidelines - check below link:

https://developer.android.com/reference/android/support/constraint/Guideline.html

Upvotes: 1

Related Questions