Get Started with Java Bridge (Android Studio)
To introduce you to programming with the Java Bridge library, you’ll create a very simple app: when the user clicks on a button, it changes color to red (Wahoooooo!). The sample demonstrates the structure you’ll need in your code to build Android apps with the Java Bridge library.
These instructions are for building an app using Android Studio. If you are using Eclipse, see these instructions.
1.Create a new Android Application Project. Choose File | New Project and name your application "RedClick". Click Next, then leave the default minimum API version of 8 (Anroid version 2.2 Froyo). Ignore the other items and click Next. on the next screen, choose a Blank Activity for the app and on the final screen just click Finish.
2.Run the App you have so far. If there are no errors, you should now have the app that Android Studio sets you up with, one that displays “hello world”. You can run the app by selecting the project and clicking the green icon in the top menu. If your Android device is plugged in to your computer with USB,you can run it on that. If not, you can run it in the emulator. To test on the emulator, you may need to setup an Android Virtual Device (AVD). Click herefor some instructions to setup your AVD.
3.Import the Java Bridge Library. Download the java bridge library to your computer. You need to copy it into the libs directory of your Android Studio project. To do this, first choose the Project view for your project. You can do this by clicking on the "Android" above the left panel that shows your project items, and choosing "Project" Then click on the "app" folder and you'll see a folder named libs. Open a file manager, find the .jar file you just downloaded, and drag it into the libs folder. Then right-click (or control-click) on the .jar file and choose "Add as Library". Your app can now use the Java Bridge library and all the App Inventor components.
4.Import Java sample code: ClickRed. Download this file to your computer, (you may have to copy the code in the file and paste it into a new edit file). Then open the folder src/main/java/com.example.yourname.redclick in your project, and drag Screen1.java into it. This Screen1.java code will serve as a replacement for MainActivity.java. It should be fine, but you may need to change the package specification at the top of the file. Copy the package specification from MainActivity.Java, e.g., “package com.example.clickred;” and paste it to replace the package specification in the sample file.
5.Modify the Android Manifest File. Your Eclipse Android project has a manifest file which specifies various things about your app. Go back to the Android View in your left-panel, then choose the manifests directory and click on the AndroidManifest file to open it. You want to change it so that the Screen1 class you just imported is used as the initial Activity (screen) instead of MainActivity. In the file, you’ll see the following:
<activity android:name=".MainActivity"
Replace “.MainActivity” with “.Screen1” (leave the initial dot), so it appears as:
<activity android:name=".Screen1"
6.Rerun the app. If all is well, the app should now appear with a single button that when clicked, turns red. You've completed your first Android app!
Exercise
Modify the app so that:
- the button starts out showing a '1'.
- Each time the button is clicked, the number on it is incremented (1 to 2, 2 to 3, etc.).
You'll need to convert String values to numbers and then back to String. Here are example calls to Java conversion methods:
int score = Integer.parseInt("323");
String s = String.valueOf(73);