App Inventor team announced their initial, alpha, version of the App Inventor Java Bridge. Here is an excerpt from AI team:
“The App Inventor Java Bridge helps make a transition from developing Android applications with App Inventor for Android, to developing with Java and the Android SDK. It lets you incorporate App Inventor components into apps that you create in Java with the standard Android SDK tools. If you’ve been using App Inventor and know some Java, then the Java Bridge is a good way to get started with the SDK, because building with App Inventor components hides many of the complexities of the Android framework. As you gain experience, you can switch over to regular SDK development and create apps that harness the full power of Android.”
In this example, we try to create equivelant of PaintPot. In this example, I only cover Part II of PaintPot.
The AI Components are available in libSimpleAndroidRuntime.jar which is available for download HERE. This library file is required so that you one would have access to same components that they are used to in App Inventor when using the Block Editor.
To build app using the bridge, include libSimpleAndroidRuntime.jar in your library list.
Some of main sections when using AI Bridge are as follows:
You’ll need the following public method
This is equivalent to a “main” method for App Inventor apps
EventDispatcher.registerEventForDelegation(this, “AppInventorInJava”, “Touched”)
Above register for events. The second argument can be any string. The third argument must exactly match the name of the event that you want to handle for that component (e.g. Click, Touched). When the event happens, dispatchEvent will be called with these arguments.
public boolean dispatchEvent(Component component, String id, String eventName, Object args)
Above is the event dispatcher for the app which should override the method for the Form superclass
In this method, you can check to see what event was triggered (e.g. Click event). You capture the event and based on that you can perform desired action. For example, if we have two AI Buttons (btnBlue and btnRed), to check to see if btnBlue.Click was triggered or btnRed.Click was triggered, you code the following:
if (component.equals(btnBlue) && eventName.equals(“Click”))
//do some action
else if (component.equals(btnRed) && eventName.equals(“Click”))
//do some action
You can download this example from HERE which includes comments to highlight important sections.