Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add constraints to <include> inside ConstraintLayout

Please take a look at my xml:

<android.support.constraint.ConstraintLayout
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize">

    <RelativeLayout
        android:id="@+id/layout_info"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <include
        layout="@layout/page"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>

</android.support.constraint.ConstraintLayout>

I'm not able to add constraint to the <include>. The app namespace doesn't work (it works for the RelativeLayout) and auto fill doesn't show the constraint attributes. I want the included layout's height to be the remaining space in the ConstraintLayout, but how do I do it without constraints! Please help.

like image 453
Nithin Avatar asked Sep 14 '17 05:09

Nithin


People also ask

How many constraint handles are available to us as part of the ConstraintLayout?

The TextView above has three types of handles: Resize handle - It's present on the four corners and is used to resize the view, but keeping its constraints intact. Side handle - It's the circular handle present on the centre of each side. It's used to set the top, left, bottom and right constraints of the view.

Can we use RelativeLayout inside ConstraintLayout?

You can't use relative layout directly inside constraint layout. Intention of ConstraintLayout is to optimize and flatten the view hierarchy of your layouts by applying some rules to each view to avoid nesting.

Which is better RelativeLayout or ConstraintLayout?

If you have the choice start with ConstraintLayout, but if you already have your app in RelativeLayout, stay with it. That's all I have been following. RelativeLayout is very limited in functionality and many complex layouts can't be made using it, especially when ratios are involved.

Is ConstraintLayout faster than Linearlayout?

More complex layout but results are the same, flat Constraint Layout is slower than nested Linear Layout.


2 Answers

<include layout="@layout/layout_no_friends_avalable"
                 android:id="@+id/inc_no_friend"
                 android:layout_width="match_parent"
                 android:layout_height="0dp"
                 app:layout_constraintBottom_toBottomOf="parent"
                 app:layout_constraintEnd_toEndOf="parent"
                 app:layout_constraintTop_toBottomOf="@+id/toolbar_main"
        />

When adding constraints in <include> tag we must use android:layout_width and android:layout_height both attributes.

like image 59
aswanth bonthala Avatar answered Sep 25 '22 00:09

aswanth bonthala


Try this

 <android.support.constraint.ConstraintLayout
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize">

    <RelativeLayout
        android:id="@+id/layout_info"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <include
         layout="@layout/page"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintLeft_toLeftOf="parent"
         app:layout_constraintRight_toRightOf="parent"
         app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>
like image 33
AskNilesh Avatar answered Sep 24 '22 00:09

AskNilesh