In this video I will finish up converting my App Inventor app into regular Java. If you missed the first video you must watch it first.
Not only will we finish the app, but I’ll also show how to install Ai2LiveComplete, which is a version of App Inventor that adds a ton of additional functionality and also doesn’t require an internet connection. The full list of what we will learn and the code follows the video below.
If you like videos like this, it helps my Google search rank when you share on Google Plus with a click here
Here is the Bridge API I mentioned in the video.
What is Covered?
Hello Zombie Main Activity
package com.newthinktank.hellozombie; //All the libraries needed for all the App Inventor components import com.google.devtools.simple.runtime.components.Component; import com.google.devtools.simple.runtime.components.HandlesEventDispatching; import com.google.devtools.simple.runtime.components.android.AccelerometerSensor; import com.google.devtools.simple.runtime.components.android.ActivityStarter; import com.google.devtools.simple.runtime.components.android.Button; import com.google.devtools.simple.runtime.components.android.Form; import com.google.devtools.simple.runtime.components.android.HorizontalArrangement; import com.google.devtools.simple.runtime.components.android.Label; import com.google.devtools.simple.runtime.components.android.LocationSensor; import com.google.devtools.simple.runtime.components.android.Notifier; import com.google.devtools.simple.runtime.components.android.Sound; import com.google.devtools.simple.runtime.components.android.TextToSpeech; import com.google.devtools.simple.runtime.events.EventDispatcher; public class MainActivity extends Form implements HandlesEventDispatching { private Label touchZombieLabel; private HorizontalArrangement horizontalArrangement1; private Button zombieButton; private Sound zombieMoanSound; private Sound zombieAttackSound; private AccelerometerSensor accelerometerSensor1; private LocationSensor locationSensor1; private Notifier notifier1; private ActivityStarter activityStarter1; private TextToSpeech textToSpeech1; private int zombieImageHeight = 712; private int zombieImageWidth = 855; private String latitude, longitude; private String gpsProviderName; void $define() { //set up screen orientation (landscape or unspecified) this.ScreenOrientation("portrait"); // Set the screen name title this.Title("Hello Zombie"); // Set the screens background color and background image this.BackgroundColor(COLOR_NONE); this.BackgroundImage("SplatterBackground.png"); this.Scrollable(false); // Initialize text to speech capability textToSpeech1 = new TextToSpeech(this); // Create the label and put it on the screen in the center touchZombieLabel = new Label(this); touchZombieLabel.Text("Touch the Zombie"); touchZombieLabel.FontSize(20.0f); touchZombieLabel.Width(LENGTH_FILL_PARENT); touchZombieLabel.TextAlignment(Component.ALIGNMENT_CENTER); touchZombieLabel.FontBold(true); // Create a horizontal arrangement and define the height and width to fill screen horizontalArrangement1 = new HorizontalArrangement(this); horizontalArrangement1.Width(LENGTH_FILL_PARENT); horizontalArrangement1.Height(LENGTH_FILL_PARENT); // Put the zombie image on a button and put the button in the horizontal arrangement zombieButton = new Button(horizontalArrangement1); zombieButton.Image("Zombie.png"); zombieButton.Width(zombieImageHeight); zombieButton.Height(zombieImageWidth); zombieButton.TextAlignment(Component.ALIGNMENT_CENTER); // Initialize Zombie attack sound zombieAttackSound = new Sound(this); zombieAttackSound.Source("ZombieAttack.wav"); // Make zombie moan when it is clicked zombieMoanSound = new Sound(this); zombieMoanSound.Source("ZombieMoan.wav"); EventDispatcher.registerEventForDelegation(this, "ButtonClick", "Click"); // Initialize our location sensor locationSensor1 = new LocationSensor(this); // Gets us the best available provider of location data locationSensor1.RefreshProvider(); // Enable the notifier notifier1 = new Notifier(this); // initialize the activity starter activityStarter1 = new ActivityStarter(this); // initialize accelerometer accelerometerSensor1 = new AccelerometerSensor(this); // Register the event that catches the shaking event EventDispatcher.registerEventForDelegation( this, "ShakingPhone", "Shaking" ); } @Override public boolean dispatchEvent(Component component, String id, String eventName, Object[] args) { if (component.equals(zombieButton) && eventName.equals("Click")) { // Make the phone vibrate and play sound zombieMoanSound.Vibrate(500); zombieMoanSound.Play(); // Check if provider is gps and if not show alert to turn on gps gpsProviderName = locationSensor1.ProviderName(); if(!gpsProviderName.equals("gps")){ // Alert the user that they must enable GPS notifier1.ShowAlert("Enable GPS in Settings"); // Open the settings where the user enables GPS activityStarter1.Action("android.settings.LOCATION_SOURCE_SETTINGS"); activityStarter1.StartActivity(); } latitude = Double.toString(locationSensor1.Latitude()); longitude = Double.toString(locationSensor1.Longitude()); // Convert text provided to speech textToSpeech1.Speak("Zombies latitude is " + latitude + " Zombies longitude is " + longitude); return true; } else if( component.equals(accelerometerSensor1) && eventName.equals("Shaking")){ zombieAttackSound.Play(); return true; } return false; } }
Hello Zombie Android Manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.newthinktank.hellozombie" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_LOCATION"></uses-permission> <uses-permission android:name="android.permission.VIBRATE"></uses-permission> <application android:allowBackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > <activity android:name="com.newthinktank.hellozombie.MainActivity" android:label="@string/app_name" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Hi..
when I open WinStartAIServer on windows, it closed immediately. How to fix that?
That almost always means your path isn’t set up. I cover how to fix that in the video.
I’m also having the same problem plus i have java 1.8 do i have to downgrade to run it
Yes use Java 7 instead
Very nice tutorial, as usual. I am trying to access Fusion Tables in a similar method but the documentation is very limited. How can i know if I constructed the right query and how do I read back the data received from the fusion table? Please help, Moshe
Sorry that was one of the only things I skipped. Here is a nice tutorial http://appinventor.mit.edu/explore/ai2/pizzaparty.html
Yes, the tutorial works fine but I am trying to convert to Java and have to learn some things in the hard way. Thanks anyway, you have excellent tutorials and I am waiting to learn more
I have a Java tutorial here. I’ll also review Java again in my new Android tutorial.
Hi Derek,
I tried to run your zombie code under my enviroment, but I have one error message from the AndroidManifest.xml file.
Here is the line that eclipse complains
android:theme=”@style/Theme.NoTitleBar.Fullscreen” >
It said the can’t find Theme.NoTitleBar.Fullscreen in the style file.
You have upload your style file as well.
Below is the eclipse version
Android Developer Tools
Build: v22.6.2-1085508
Thanks,
Ben
Hi Ben,
Try using the following in the application tags
i hope that helps 🙂
Hi Derek,
Please ignore my previous comment. I found a typo error in my AndroidManifest.xml file.
Thank you so much for all the time and hard work to share your work, and knowledge.
I’m very appreciate a lot for your tutorial. I have learned so much stuff from your site. You have to the best Android tutorial on the youtube.
Your teaching method is step by step, and easy to follow.
Once again, thank so much for providing free education.
Best Regards,
Ben
Hi Ben,
I’m glad you fixed it. Sorry I couldn’t help. You’re very welcome 🙂 Thank you for visiting my little blog.
Hi Derek! thanks again for your tutorials! please ignore my previous message, i figured it out!
Cheers!
Great sorry I couldn’t get back to you. I have been on a long business trip
Hi Derek!
I have been trying to follow your Zombie App tutorial, but when I try to compile it, the app immediately closes with ‘Unfortunatly Zombie has stopped’.
In my Zombie Main Activity it says for the void $define() method: “The method MainActivity.$define() does not override the inherited method from Form since it is private to a different package”. I think this is the reason why my app is not working…
What did I do wrong?
Thank you for you help!
Hi Philipp, The best thing to do is to copy and paste my code. Make sure you have all the imports in your code.
I run this on my Andoird device, but the screen is black. How do I fix that, I can hear the Moan sound when I press the screen, I have added all the images in the res/raw folder (created the raw folder) and added it to the device I can see on my screen. But why it does not show up on my phone I cant figure out. I ran the simple HelloWorld app, and the screen shows the text. Just now sure why it doesnt do that on mine.
Sorry, but I’m not sure. Have you tried making an app that shows only the image to narrow it down?