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.

Comments

  1. View customview = getLayoutInflater().inflate(R.layout.text, null);

    Text cannot be resolved it is not a field error is occurring. Please help me

    ReplyDelete
    Replies
    1. Create a Custom View text.xml

      Delete
    2. Thank you Sir. It worked. I realize it was a silly mistake

      Delete
  2. Man , you are really a synonym for "Sketchware Help" but ,i see, a problem in my blog application made with Sketchware. So could you please help me out!
    My email :- mohammadaarizimam@gmail.com
    and i use Telegram as Mohammad Aariz Imam
    If you could please , then i will be so greatful to you

    ReplyDelete
  3. Please about notification in skeatchware can you remake the tutos cause with the new skeatchware version, Android.support.v4.app.NotificationCompat.Builder(MainActivity.this, "id1") cannot be resolved to a type

    ReplyDelete
  4. Please, Sanjeev help me with how to build the moreblck "selected CheckBox..... to answer " in quiz app with json

    ReplyDelete
  5. Androidx cannot be resolved to a type. Please help.

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple car racing android game in Sketchware

Creating a Drawing View in Sketchware

Enable Fullscreen for Youtube videos in WebView

How to enable upload from webview in Sketchware?

List of Calendar Format symbols valid in Sketchware