0

I want to add CardView programmatically.

Here is my Main Activity XML Layout (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/linearLayout1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical">
</LinearLayout>

Here is my CardViewTemplate (card_view_template.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cardViewTemplate"
    android:layout_width="160dp"
    android:layout_height="190dp"
    android:layout_margin="10dp"
    android:clickable="true"
    android:foreground="?android:selectableItemBackground">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="This is a Card" />

</androidx.cardview.widget.CardView>

Here is my Java Code (MainActivity.java)

LayoutInflater inflater = getLayoutInflater();
ViewGroup parent = findViewById(R.id.linearLayout1);
inflater.inflate(R.layout.card_view_template, parent);

See Output

Everything works fine till here.

Now I want to add Card at certain position in my activity_main.xml as I am using multiple CardViews, I want to add Cards at certain position. Hence, instead of the above code, I tried this:

LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.card_view_template, null);
ViewGroup parent = findViewById(R.id.linearLayout1);
parent.addView(view, 0);

But it does not inflate properly. Only the Text is visible, The Card does not seem to appear. See Output Here

Junaid Pathan
  • 3,850
  • 1
  • 25
  • 47

2 Answers2

2

When dynamically adding views, we shouldn't inflate the View with null ViewGroup parent.

In this View view = inflater.inflate(R.layout.card_view_template, null); here parent is specified as null, this caused the problem.

Specify the parent to which the View will be attached to and only set attach to parent as false. This way Parent will be specified but not attached.

Hence first declare parent (root) and then create View and specify the parent (root) and set attach to parent (root) false

This is the correct statement View view = inflater.inflate(R.layout.card_view_template, parent, false);

Hence the complete code will be:

LayoutInflater inflater = getLayoutInflater();
ViewGroup parent = findViewById(R.id.linearLayout1);
View view = inflater.inflate(R.layout.card_view_template, parent, false);
parent.addView(view, 0);
Junaid Pathan
  • 3,850
  • 1
  • 25
  • 47
0

i suggest that you use directly the linear layout ,like:

LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.card_view_template, null);
LinearLayout LL = findViewById(R.id.linearLayout1);
LL.addView(view,0);
  • this is the exact same code as the question but you renamed parent to LL – Quinn Nov 05 '19 at 21:18
  • @karounka pedro kilombo, Not working, still the same problem. The Card is not appearing. Only the text is appearing. as shown in the output screenshot: "https://i.stack.imgur.com/fEq4A.jpg" – Junaid Pathan Nov 05 '19 at 21:20