package com.example.recyclerviewextension; import android.content.Context; import android.graphics.Color; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import androidx.cardview.widget.CardView; import com.google.appinventor.components.annotations.*; import com.google.appinventor.components.common.ComponentCategory; import com.google.appinventor.components.runtime.AndroidNonvisibleComponent; import com.google.appinventor.components.runtime.AndroidViewComponent; import com.google.appinventor.components.runtime.ComponentContainer; import com.google.appinventor.components.runtime.EventDispatcher; import com.google.appinventor.components.runtime.util.YailList; import com.google.appinventor.components.annotations.*; import com.google.appinventor.components.common.ComponentCategory; import com.google.appinventor.components.runtime.ComponentContainer; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @DesignerComponent( version = 4, description = "Extension to create dynamic views and manage them in arrangements.", category = ComponentCategory.EXTENSION, nonVisible = true, iconName = "images/extension.png" ) @SimpleObject(external = true) @UsesLibraries(libraries = "cardview.jar,recyclerview-1.1.0.jar")//appcompat-1.3.0.jar /* */ public class RecyclerViewExtension extends AndroidNonvisibleComponent { private final Context context; private final ArrayList> viewsList; public RecyclerViewExtension(ComponentContainer container) { super(container.$form()); this.context = container.$context(); this.viewsList = new ArrayList<>(); } // Gravity Property Methods @SimpleProperty(description = "Returns the gravity value for Center.") public int GetGravityCenter() { return Gravity.CENTER; } @SimpleProperty(description = "Returns the gravity value for Left.") public int GetGravityLeft() { return Gravity.LEFT; } @SimpleProperty(description = "Returns the gravity value for Right.") public int GetGravityRight() { return Gravity.RIGHT; } @SimpleProperty(description = "Returns the gravity value for Top.") public int GetGravityTop() { return Gravity.TOP; } @SimpleProperty(description = "Returns the gravity value for Bottom.") public int GetGravityBottom() { return Gravity.BOTTOM; } // Orientation Property Methods @SimpleProperty(description = "Returns the orientation value for Horizontal.") public int GetOrientationHorizontal() { return LinearLayout.HORIZONTAL; } @SimpleProperty(description = "Returns the orientation value for Vertical.") public int GetOrientationVertical() { return LinearLayout.VERTICAL; } // Click Events @SimpleEvent(description = "Triggered when a button is clicked. Returns the button ID.") public void OnClickButton(String id) { EventDispatcher.dispatchEvent(this, "OnClickButton", id); } @SimpleEvent(description = "Triggered when a CardView is clicked. Returns the CardView ID.") public void OnClickCardView(String id) { EventDispatcher.dispatchEvent(this, "OnClickCardView", id); } @SimpleEvent(description = "Triggered when an ImageView is clicked. Returns the ImageView ID.") public void OnClickImage(String id) { EventDispatcher.dispatchEvent(this, "OnClickImage", id); } @SimpleEvent(description = "Triggered when a layout is clicked. Returns the layout ID.") public void OnClickLayout(String id) { EventDispatcher.dispatchEvent(this, "OnClickLayout", id); } @SimpleEvent(description = "Triggered when a TextView is clicked. Returns the TextView ID.") public void OnClickText(String id) { EventDispatcher.dispatchEvent(this, "OnClickText", id); } // Utility to add OnClickListener to a view private void setClickListener(View view, final String id, final String type) { view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { switch (type) { case "Button": OnClickButton(id); break; case "CardView": OnClickCardView(id); break; case "ImageView": OnClickImage(id); break; case "Layout": OnClickLayout(id); break; case "TextView": OnClickText(id); break; } } }); } @SimpleFunction(description = "Create a CardView and return its ID.") public String CreateCardView(String id, int height, int width, float weight, int margin, int padding, int orientation, int gravity, String backgroundColor, float radius, boolean shadowEffect, YailList childViews) { try { CardView cardView = new CardView(context); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height, weight); params.setMargins(margin, margin, margin, margin); cardView.setLayoutParams(params); cardView.setCardBackgroundColor(Color.parseColor(backgroundColor)); cardView.setRadius(radius); cardView.setContentPadding(padding, padding, padding, padding); if (shadowEffect) { cardView.setCardElevation(10); } LinearLayout containerLayout = new LinearLayout(context); containerLayout.setOrientation(orientation); containerLayout.setGravity(gravity); containerLayout.setPadding(padding, padding, padding, padding); for (Object child : childViews.toArray()) { if (child instanceof Map) { @SuppressWarnings("unchecked") Map childMap = (Map) child; View view = new ArrayList<>(childMap.values()).get(0); if (view != null) { containerLayout.addView(view); } } } cardView.addView(containerLayout); setClickListener(cardView, id, "CardView"); Map viewMap = new HashMap<>(); viewMap.put(id, cardView); viewsList.add(viewMap); // Return the ID of the created view return id; } catch (Exception e) { form.dispatchErrorOccurredEvent(this, "CreateCardView", 1, e.getMessage()); return ""; } } @SimpleFunction(description = "Create a Button and return its ID.") public String CreateButton(String id, int height, int width, float weight, int margin, int padding, int gravity, String text, float fontSize, String fontColor, String backgroundColor) { try { Button button = new Button(context); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height, weight); params.setMargins(margin, margin, margin, margin); button.setLayoutParams(params); button.setText(text); button.setTextSize(fontSize); button.setTextColor(Color.parseColor(fontColor)); button.setBackgroundColor(Color.parseColor(backgroundColor)); button.setGravity(gravity); setClickListener(button, id, "Button"); Map viewMap = new HashMap<>(); viewMap.put(id, button); viewsList.add(viewMap); // Return the ID of the created view return id; } catch (Exception e) { form.dispatchErrorOccurredEvent(this, "CreateButton", 5, e.getMessage()); return ""; } } // Function to create a TextView @SimpleFunction(description = "Create a TextView and store it in the YailList.") public String CreateTextView(String id, int height, int width, float weight, int margin, int padding, int gravity, String text, int maxLines, float fontSize, String fontColor, String style) { try { TextView textView = new TextView(context); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height, weight); params.setMargins(margin, margin, margin, margin); textView.setLayoutParams(params); textView.setText(text); textView.setMaxLines(maxLines); textView.setTextSize(fontSize); textView.setTextColor(Color.parseColor(fontColor)); textView.setGravity(gravity); setClickListener(textView, id, "TextView"); Map viewMap = new HashMap<>(); viewMap.put(id, textView); viewsList.add(viewMap); return id; } catch (Exception e) { form.dispatchErrorOccurredEvent(this, "CreateButton", 5, e.getMessage()); return ""; } } // Function to create an ImageView @SimpleFunction(description = "Create an ImageView and store it in the YailList.") public String CreateImage(String id, int height, int width, float weight, int margin, int padding, String assetsFileName) { try { ImageView imageView = new ImageView(context); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height, weight); params.setMargins(margin, margin, margin, margin); imageView.setLayoutParams(params); setClickListener(imageView, id, "ImageView"); Map viewMap = new HashMap<>(); viewMap.put(id, imageView); viewsList.add(viewMap); return id; } catch (Exception e) { form.dispatchErrorOccurredEvent(this, "CreateButton", 5, e.getMessage()); return ""; } } @SimpleFunction(description = "Show all views from the YailList in an arrangement.") public void ShowOnLayout(AndroidViewComponent layout, String backgroundColor, YailList viewIds) { try { ViewGroup arrangementView = (ViewGroup) layout.getView(); arrangementView.setBackgroundColor(Color.parseColor(backgroundColor)); for (Object idObj : viewIds.toArray()) { String id = idObj.toString(); for (Map viewMap : viewsList) { if (viewMap.containsKey(id)) { arrangementView.addView(viewMap.get(id)); break; } } } } catch (Exception e) { form.dispatchErrorOccurredEvent(this, "ShowOnLayout", 6, e.getMessage()); } } // Function to get all views @SimpleFunction(description = "Get all stored views as a YailList.") public YailList GetAllViews() { return YailList.makeList(viewsList); } }