Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inflating ConstraintLayout in another ConstraintLayout

I want to put a ConstraintLayout programmaticly in another ConstraintLayout. But it does not show up, if I inflate it the way I did before with a RelativeLayout.

Here's the code inner file inner_area.xml:

<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:id="@+id/innerArea"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
    android:id="@+id/grade_one"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginTop="0dp"
    android:background="@drawable/onegreen"
    app:layout_constraintBottom_toTopOf="@+id/h1"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/v1"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    tools:layout_conversion_absoluteHeight="20dp"
    tools:layout_conversion_absoluteWidth="30dp"
    tools:layout_conversion_absoluteX="0dp"
    tools:layout_conversion_absoluteY="0dp" />

<ImageView
    android:id="@+id/grade_three"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginTop="0dp"
    android:background="@drawable/threelila"
    app:layout_constraintBottom_toTopOf="@+id/h2"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/v1"
    app:layout_constraintTop_toTopOf="@+id/h1"
    app:layout_constraintVertical_bias="0.0"
    tools:layout_conversion_absoluteHeight="20dp"
    tools:layout_conversion_absoluteWidth="30dp"
    tools:layout_conversion_absoluteX="0dp"
    tools:layout_conversion_absoluteY="20dp" />
    .....
 </android.support.constraint.ConstraintLayout>

Outer LayoutFile parent.xml:

   <?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:orientation="vertical"
android:id="@+id/parentView"
android:background="@color/primary">

</android.support.constraint.ConstraintLayout>

Javacode:

    ViewGroup parentView = rootView.findViewById(R.id.parentView);
    final ConstraintLayout innerArea = (ConstraintLayout) inflater.inflate(R.layout.inner_area, container, false);
    parentView.addView(innerArea);

But when I change in the inner.xml the layout_width or layout_height of the innerArea to wrap_content or match_parent, nothing happens. But when I change to 2000dp the inner layout shows up.

What am I doing wrong or missing?

like image 989
ksome Avatar asked Oct 08 '17 11:10

ksome


2 Answers

Thanks to Cheticamp for pointing me in the right direction. I was inflating the ConstraintLayout with the root layout and not with the Constraint Layout it is supposed to be in. So the correct Javacode is:

    parentView = rootView.findViewById(R.id.parentView);
    ViewGroup parentView = rootView.findViewById(R.id.parentView);
    final ConstraintLayout innerArea = (ConstraintLayout) 
    inflater.inflate(R.layout.inner_area, parentView, false);
    parentView.addView(innerArea);
like image 165
ksome Avatar answered Nov 14 '22 22:11

ksome


I seem to be very late to the party but I tried lots of things to solve this problem but nothing worked.

I ended up just putting the inner constraint layout inside another layout (a relative layout in my case) which solved the problem for me.

like image 21
Finurligt Avatar answered Nov 14 '22 22:11

Finurligt