0

I tried to inflate this constrains layout into another constrains layout.

but when I run it i got only get the pink card the four ImageView doesn't appear inside the card .

here is the main XML file which I need to inflate another layout inside it.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:background="@mipmap/wall"
tools:context=".StepThreeActivity">

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/constraintLayout2"
    android:layout_width="374dp"
    android:layout_height="635dp"
    android:background="@drawable/card"
    android:backgroundTint="@color/white"
    android:elevation="@dimen/cardview_default_elevation"
    android:padding="13dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">


    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginBottom="11dp"
        android:backgroundTint="@color/Blue"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/back" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/forward"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="11dp"
        android:backgroundTint="@color/Blue"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/forward" />



</androidx.constraintlayout.widget.ConstraintLayout>



  </androidx.constraintlayout.widget.ConstraintLayout>

here is java class related to the previous main XML

public class StepThreeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_step_three);

    initialization();
    characterInitialization();


    LayoutInflater inflater =
            (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      inflater.inflate(R.layout.card1,
              constraintLayout, true);  
}

here is card1.xml code

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout2c"
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="wrap_content"
android:layout_height="wrap_content">


<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="347dp"
    android:layout_height="366dp"
    android:layout_marginTop="16dp"
    android:background="@drawable/card"
    android:backgroundTint="#FBD5D8"
    android:elevation="@dimen/cardview_default_elevation"
    android:padding="13dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="111dp"
        android:layout_height="149dp"
        android:layout_marginStart="40dp"
        android:layout_marginTop="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@mipmap/a1" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="106dp"
        android:layout_height="150dp"
        android:layout_marginStart="172dp"
        android:layout_marginTop="168dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@mipmap/a2" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="112dp"
        android:layout_height="152dp"
        android:layout_marginStart="40dp"
        android:layout_marginTop="164dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@mipmap/a3" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="115dp"
        android:layout_height="153dp"
        android:layout_marginStart="172dp"
        android:layout_marginTop="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@mipmap/a4" />
</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
Zain
  • 37,492
  • 7
  • 60
  • 84
rana1m
  • 73
  • 1
  • 1
  • 7

2 Answers2

0

Do you really need to inflate layout in this way?

Can't you just include this layout

<include layout="@layout/card1 ... />

whole xml file:

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/constraintLayout2"
    android:layout_width="374dp"
    android:layout_height="635dp"
    android:background="@drawable/card"
    android:backgroundTint="@color/white"
    android:elevation="@dimen/cardview_default_elevation"
    android:padding="13dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <include layout="@layout/card1 ... />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginBottom="11dp"
        android:backgroundTint="@color/Blue"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/back" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/forward"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="11dp"
        android:backgroundTint="@color/Blue"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/forward" />
     </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
  • I need to inflate different layout for specific button, EX: the user click button one I will inflate card 1.xml, the user click button tow I will inflate card2.xml. have another Idea? – rana1m Feb 15 '21 at 22:39
0

The problem that the 4 card images don't appear because you actually didn't add them to the layout.

snippets from card1.xml:

tools:srcCompat="@mipmap/a1"
tools:srcCompat="@mipmap/a2"
tools:srcCompat="@mipmap/a3"
tools:srcCompat="@mipmap/a4"

When you use tools name space you are affecting the layout only in the android studio Layout Validation pane, and that won't affect it when you run your app.

For more info about tools namesapce, you can have a look at documentation.

To fix this you need to replace tools with app name space. So your card1.xml layout will be:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout2c"
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="wrap_content"
android:layout_height="wrap_content">


<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="347dp"
    android:layout_height="366dp"
    android:layout_marginTop="16dp"
    android:background="@drawable/card"
    android:backgroundTint="#FBD5D8"
    android:elevation="@dimen/cardview_default_elevation"
    android:padding="13dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="111dp"
        android:layout_height="149dp"
        android:layout_marginStart="40dp"
        android:layout_marginTop="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/a1" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="106dp"
        android:layout_height="150dp"
        android:layout_marginStart="172dp"
        android:layout_marginTop="168dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/a2" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="112dp"
        android:layout_height="152dp"
        android:layout_marginStart="40dp"
        android:layout_marginTop="164dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/a3" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="115dp"
        android:layout_height="153dp"
        android:layout_marginStart="172dp"
        android:layout_marginTop="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/a4" />
</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
Zain
  • 37,492
  • 7
  • 60
  • 84