Androidx CardView in Sketchware

To add multiple CardViews with text contents on a page, as shown in image below, follow the steps given below.

1. In VIEW area of main.xml add a ScrollView with padding 0. Inside ScrollView add a LinearV linear1. Inside linear1 add 9 LinearH.

2. Switch On AppCompat and design.


3. Create a Custom View text.xml. In text.xml add a TextView textview1.

4. Add a String List mylist.

5. In onCreate event:
a. Add 9 items to the String List.

b. Then add following codes in add source directly block
// Create a loop from 0 to to child count of linear1
for (int i = 0; i < linear1.getChildCount(); i++){
// For each i in loop create a CardView, set it's radius and elevation, and add it to the child at 1 of linear1.
androidx.cardview.widget.CardView card = new androidx.cardview.widget.CardView(this);
card.setRadius(30);
card.setCardElevation(20);
((ViewGroup)linear1.getChildAt(i)).addView(card);
// Get Custom View and the TextView in Custom View. Set text of TextView by getting data from String List
View customview = getLayoutInflater().inflate(R.layout.text, null);
TextView textview1 = customview.findViewById(R.id.textview1);
textview1.setText(mylist.get(i));
// Add Custom View to the CardView

card.addView(customview);
}

6. Save and run the project.


Method two: Multiple CardViews without adding multiple LinearH.

1. In VIEW area of main.xml add a ScrollView with padding 0. Inside ScrollView add a LinearV linear1.

2. Switch On AppCompat and design.


3. Create a Custom View text.xml. In text.xml add a TextView textview1.

4. Add a String List mylist.

5. In onCreate event:
a. Add items to the String List.

b. Then add following codes in add source directly block
// Create a loop from 0 to length of list.
for (int i = 0; i < mylist.size(); i++){
// Create a CardView, set it's radius and elevation.
androidx.cardview.widget.CardView card = new androidx.cardview.widget.CardView(this);
card.setRadius(30);
card.setCardElevation(20);
((ViewGroup)linear1.getChildAt(i)).addView(card);
// Create new LayoutParams, set it's margins and set it as LayoutParams of CardView.
androidx.cardview.widget.CardView.LayoutParams lp = new androidx.cardview.widget.CardView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(8,8,8,8);
card.setLayoutParams(lp);
// Add CardView to linear1.

((ViewGroup)linear1).addView(card);
// Get Custom View and the TextView in Custom View. Set text of TextView by getting data from String List
View customview = getLayoutInflater().inflate(R.layout.text, null);
TextView textview1 = customview.findViewById(R.id.textview1);
textview1.setText(mylist.get(i));
// Add Custom View to the CardView.
card.addView(customview);
}

6. Save and run the project.