Java Bridge

The Java Bridge is Java code equivalents for all the app inventor components. It is a high-level Android library for rapid development of apps.

The Java Bridge was originally published by the App Inventor team. We will be using a version updated by M. Hossein Amerkashi. Its called the sdkBridger:
Usage
You can use this library by downloading the jar file and then adding it into a libs directory within an Android project. Be sure and select the jar file after you add it and choose Build Path | Add to Build Path.

When using the Java Bridge, your main class is of type Form instead of Activity. Form is a subclass of Activity defined in the library.

All forms must have a $define method. This function will be called when the app begins, i.e. when the screen (form) initializes. You'll create your UI objects here. Note that with the Java Bridge library, you cannot use the Eclipse Android GUI builder to create an xml file. You also should remove the OnCreate method.

With the sdkBridger, the components are not marked final so you can subclass them. This helps with defining event-handlers, and you don't need to register listeners or handle parameters at a low-level. For instance, you can subclass the Canvas class, then override the method "Touched" with its x and y parameters.

Samples
Check out these samples using the Java Bridge 1.0.1


Original Java Bridge
The Google App Inventor team published the original Java Bridge. Here is there original instructions on how to install it and use it:
Java Bridge instructions. Here is the Source code. You can learn about Android by exploring how the Google team built this library. The source code uses some annotations having to do with the Simple language. Here is a spec

Samples
The AppToMarket site, also developed by M. Hossein Amerkashi, has a lot of java bridge samples, maybe one for each tutorial. Here is his code.google site:

Understanding Java Bridge Samples (from Amerkashi's site)

The samples from appToMarket use the Form, which is a subclass of Activity, and also implement an interface called HandlesEventDispatching.

All forms must have a $define method. This function will be called when the app begins, i.e. when the screen (form) initializes. You'll create your UI objects here,

You can setup event-handlers in two ways. One is to subclass each component and override the event you want to catch. So for a Canvas, you'll create MyCanvas and override, say, the Touched event method.

You can also use a lower level method to register for all events you want to handle with code in your Form class like the following:

    EventDispatcher.registerEventForDelegation(this, "AppInventorInJava", "Click");

Do this for each event you want to deal with. Then define the dispatchEvent method, which you must have as a HandlesEventDispatching implementer. Inside it, check the component for each event and dispatch events accordingly to functions you create.



Comments