Extension Blocks Demo

Published on March 4, 2022

🏷️ Tags: #extensions

This workshop will give you hints of how to build the blocks of your extension, such as events, functions and properties.

Before you start, make sure you imported the following classes.

import com.google.appinventor.components.annotations.DesignerComponent;

import com.google.appinventor.components.annotations.DesignerProperty;

import com.google.appinventor.components.annotations.PropertyCategory;

import com.google.appinventor.components.annotations.SimpleFunction;

import com.google.appinventor.components.annotations.SimpleEvent;

import com.google.appinventor.components.annotations.SimpleObject;

import com.google.appinventor.components.annotations.SimpleProperty;

import com.google.appinventor.components.common.ComponentCategory;

import com.google.appinventor.components.common.PropertyTypeConstants;

import com.google.appinventor.components.runtime.*;

import com.google.appinventor.components.annotations.UsesLibraries;

import com.google.appinventor.components.annotations.UsesPermissions;

import com.google.appinventor.components.runtime.EventDispatcher;

A. Event Blocks.

Without parameters:

@SimpleEvent (description = "Write a description for your event.")

public void YourEventName() {

EventDispatcher.dispatchEvent(this, "YourEventName")

}

With 1+ parameters:

@SimpleEvent (description = "Write a description for your event.")

public void YourEventName(String parameter1, int parameter2) {

// String and int are the types for the parameters. A string is a piece of text, and int is an integer.

EventDispatcher.dispatchEvent(this, "YourEventName", parameter1, parameter2)

}

If you want to fire the event (call the event) anytime with given parameters, use:

YourEventName("something", 123);

Otherwise, if you want to fire an event that has no parameters, simply use:

YourEventName();

B. Method Blocks That Do Something.

Without parameters and do something:

@SimpleFunction (description = "What does this block do in a brief sentence?")

public void MyMethodName(){

// Do something here

}

With 1+ parameters and do something:

@SimpleFunction (description = "What does this block do in a brief sentence?")

public void MyMethodName(String parameter1, int parameter2){

// parameter1 only accepts strings and parameter2 only accepts integers

// Do something here. Refer to the parameters as parameter1 or parameter2

}

C. Method Blocks That Return Something.

Without parameters and return something:

@SimpleFunction (description = "What does this block do in a brief sentence?")

public String MyMethodName(){ // this specifies the return type of the method. This returns a string.

return "something"; // this returns the string something.

}

With 1+ parameters and returns something:

@SimpleFunction (description = "What does this block do in a brief sentence?")

public String MyMethodName(String parameter1, int parameter2){

// parameter1 only accepts strings and parameter2 only accepts integers

return parameter1; // this returns the parameter1

}

D. Property Blocks.

Set something:

@SimpleProperty

public void MyPropertyName(boolean myPropertyName) { // this only accepts boolean values

// do something or set something here

}

Return something:

@SimpleProperty(category = PropertyCategory.BEHAVIOR, // you can choose either for APPEARANCE or BEHAVIOR, or ignore the category.

description = "What does this property set or return in one brief sentence?")

public boolean MyPropertyName() {

return myPropertyName;

}

Add this after public class YourExtensionName:

private boolean myPropertyName;

If you would like to make your extension editable both in the Designer and the Blocks editor, use:

@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN, // accepts a boolean

defaultValue = "true") // although this is a boolean, we set this to "true" to make this checked

@SimpleProperty

public void MyPropertyName(boolean myPropertyName) { // this only accepts boolean values

// do something or set something here

}