Can I Use a Screen as an Activity?

I am working on an extension and need to switch activities using a class name. I want to know if it’s possible to use a screen in Kodular as an Android activity. How can I activate a screen by its class name? Can this be done using intents? If so, what is the correct approach?

Also, how to disable the default transition/Animation after starting the screen.

Any guidance or example blocks would be really helpful!

@Still-learning
Can u help me in this case

Yes you can launch them with intents.
This is how you get a class for a particular screen.

  private Class getClass(String screenName){
    try {
      return Class.forName(form.getPackageName()+"."+screenName);
    } catch (ClassNotFoundException e) {
      throw new YailRuntimeError(screenName+ "not found","Screen not found");
    }
  }

You can then launch it with intent like this:

context.startActivity(new Intent(form, class));

May be you can try this too

package com.Jai.ScreenSwitcher;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
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.ComponentContainer;

@DesignerComponent(
        version = 1,
        description = "An extension to switch screens using class names in Kodular.",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,
        iconName = "https://your-icon-url.com/icon.png")
@SimpleObject(external = true)
public class ScreenSwitcher extends AndroidNonvisibleComponent {

    private final Context context;

    public ScreenSwitcher(ComponentContainer container) {
        super(container.$form());
        this.context = container.$context();
    }

    @SimpleFunction(description = "Opens a screen by its class name and removes the transition animation.")
    public void OpenScreen(String screenName) {
        try {
            Intent intent = new Intent();
            String packageName = context.getPackageName();
            intent.setClassName(packageName, packageName + "." + screenName);
            context.startActivity(intent);

            // Disable transition animation
            if (context instanceof Activity) {
                ((Activity) context).overridePendingTransition(0, 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Both @iamwsumit and @Still-learning thanks
Both suggest help me a lot.:heart:

1 Like

@iamwsumit
@Still-learning
One more thing Can we modify layout xml file by using java code
suppose ,add new properity like android:text="Hello, I am a TextView" />
how can we do that by java

Yes you can, you must inflate that view(XML layout) first then you can modify that view or can get its children with java code.

Can u show a java code example

Check whether this will work for you or not

Method to modify the text

@SimpleFunction(description = "Sets the text of a TextView")
public void SetTextViewText(AndroidViewComponent component, String text) {
    View view = component.getView();
    
    if (view instanceof TextView) {
        TextView textView = (TextView) view;
        textView.setText(text);
    } else {
        // Handle error - component is not a TextView
        throw new IllegalArgumentException("Component is not a TextView");
    }
}

Method to modify textview properties

@SimpleFunction(description = "Sets the text size of a TextView")
public void SetTextViewSize(AndroidViewComponent component, int size) {
    View view = component.getView();
    
    if (view instanceof TextView) {
        TextView textView = (TextView) view;
        textView.setTextSize(size);
    } else {
        throw new IllegalArgumentException("Component is not a TextView");
    }
}

@SimpleFunction(description = "Sets the text color of a TextView")
public void SetTextViewColor(AndroidViewComponent component, int color) {
    View view = component.getView();
    
    if (view instanceof TextView) {
        TextView textView = (TextView) view;
        textView.setTextColor(color);
    } else {
        throw new IllegalArgumentException("Component is not a TextView");
    }
}

Method to add new textview in the layout

@SimpleFunction(description = "Adds a new TextView to a layout component")
public void AddTextViewToLayout(AndroidViewComponent layoutComponent, String text) {
    View view = layoutComponent.getView();
    
    if (view instanceof ViewGroup) {
        ViewGroup layout = (ViewGroup) view;
        
        // Create a new TextView
        TextView newTextView = new TextView(context);
        newTextView.setText(text);
        
        // Set layout parameters
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        );
        
        // Add the TextView to the layout
        layout.addView(newTextView, params);
    } else {
        throw new IllegalArgumentException("Component is not a layout");
    }
}

These three logic only i have used in my DynamicCalendar extension

Complex one. Find textview by ID

@SimpleFunction(description = "Sets the text of a TextView with the given ID")
public void SetTextViewTextById(AndroidViewComponent component, int viewId, String text) {
    View parentView = component.getView();
    
    // Find the TextView by ID in the component's view hierarchy
    TextView textView = (TextView) parentView.findViewById(viewId);
    
    if (textView != null) {
        // Set the text property
        textView.setText(text);
    } else {
        throw new IllegalArgumentException("TextView with ID " + viewId + " not found");
    }
}

1 Like