CodingDays
CodingDays

Reputation: 131

Can you nest constraint layouts?

Can I have a child layout of a constraint layout that is also a constraint layout? Seems ok when inside Toolbar, but when I just add directly a constraint layout whose parent is constraint layout I get error message about constraintlayout class not found etc. I tried putting the constaint layout inside a FrameLayout and inside a LinearLayout but this did not get rid of the error? So is it possible and if so how can I do it?

Upvotes: 2

Views: 2981

Answers (2)

Tanveer singh Bhatia
Tanveer singh Bhatia

Reputation: 69

You can surely nest ConstraintLayouts, One of the main advantage of constraintLayout is that it provides you your desired layout with least nesting in comparison to other layouts.You must check your code and syntax you used, It should not be a problem because of nesting of ConstraintLayout.

Here is my code, I have used ConstraintLayout inside constraintLayout without any error:

<?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">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="376dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/button3"
            android:layout_width="171dp"
            android:layout_height="78dp"
            android:layout_marginBottom="92dp"
            android:text="Button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.566"
            app:layout_constraintStart_toStartOf="parent" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="155dp"
            android:layout_height="59dp"
            android:layout_marginTop="68dp"
            android:text="Test"
            android:textSize="50dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.593"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

Upvotes: 1

Chisko
Chisko

Reputation: 3107

Yes, you can nest ConstraintLayouts, I just did without any problems. I suggest you double check your syntax. ClassNotFoundException while nesting really doesn't make any sense.

Upvotes: 1

Related Questions