Convert App Inventor to Java 2

Convert App Inventor to Java 2In 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? 

  • Finish the Android Java App
  • How to Catch Events
  • How to Make a Phone Vibrate
  • Get Location Data
  • Open Android Settings from your App
  • Open other Activities from your App
  • Convert Text to Speech
  • Catch when a User Shakes their Phone
  • How to Get Permission to use Features
  • How to Setup the Manifest File
  • How to Use all the Other App Inventor Components not Covered Here
  • How to Install Ai2LiveComplete

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>

18 Responses to “Convert App Inventor to Java 2”

  1. Roni Stiawan says:

    Hi..
    when I open WinStartAIServer on windows, it closed immediately. How to fix that?

  2. Moshe says:

    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

  3. Benjamin Dinh says:

    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

  4. Benjamin Dinh says:

    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

  5. David says:

    Hi Derek! thanks again for your tutorials! please ignore my previous message, i figured it out!

    Cheers!

  6. Philipp says:

    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!

  7. Zana says:

    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.

Leave a Reply

Your email address will not be published.

Google+