Running the demo application on Android
The Starter Class for the Android application is called MainActivity.java.
The following listing is MainActivity.java from demo-android:
package com.packtpub.libgdx.demo;
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android
.AndroidApplicationConfiguration;
public class MainActivity extends AndroidApplication {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg =
new AndroidApplicationConfiguration();
cfg.useGL20 = false;
initialize(new MyDemo(), cfg);
}
}
In the preceding code listing, you can see the MainActivity class that inherits
from the AndroidApplication class. This is how Libgdx encapsulates tasks
such as creating a so-called activity that registers handlers to process touch
input, read sensor data, and much more. What is left to do for you is to create
an instance of a class that implements the ApplicationListener interface.
In this case, it is an instance of the MyDemo class. The instances of MyDemo
and AndroidApplicationConfiguration are passed as arguments to the
initialize() method. The configuration is set to not request OpenGL ES 2.0
support on an Android device.
If you are interested in the latest development of Android hardware statistics,
be sure to check out Dashboards on the official Android Developers website:
http://developer.android.com/about/dashboards/index.html#OpenGL.
Additionally, on Android, you will have to take care of a manifest file that defines
a huge list of parameters to configure the application. If you are not yet familiar
with Android's manifest file, you should take some time to read the official
documentation at http://developer.android.com/guide/topics/manifest/
manifest-intro.html.
The following listing is AndroidManifest.xml from demo-android:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.packtpub.libgdx.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="5"
android:targetSdkVersion="17" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges=
"keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The following short (and incomplete) list is meant to give you a quick idea of what
could be defined in the manifest file:
• minSdkVersion: This is the minimum API Level required for the application
to run. Devices running with lower API Levels will not be able to run this
application; or if left undeclared, an API Level of 1 is assumed, which may
cause your app to crash at runtime when trying to access unavailable APIs.
• targetSdkVersion: This is the API Level the application targets. This is
used for forward compatibility, where later API Levels may change the
behavior of the API that might break old applications. This specification
does not prevent the application from running on devices with lower API
Levels down to minSdkVersion. If left undeclared, its value is set equal to
minSdkVersion.
Additionally, on Android, you will have to take care of a manifest file that defines
a huge list of parameters to configure the application. If you are not yet familiar
with Android's manifest file, you should take some time to read the official
documentation at http://developer.android.com/guide/topics/manifest/
manifest-intro.html.
The following listing is AndroidManifest.xml from demo-android:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.packtpub.libgdx.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="5"
android:targetSdkVersion="17" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges=
"keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The following short (and incomplete) list is meant to give you a quick idea of what
could be defined in the manifest file:
• minSdkVersion: This is the minimum API Level required for the application
to run. Devices running with lower API Levels will not be able to run this
application; or if left undeclared, an API Level of 1 is assumed, which may
cause your app to crash at runtime when trying to access unavailable APIs.
• targetSdkVersion: This is the API Level the application targets. This is
used for forward compatibility, where later API Levels may change the
behavior of the API that might break old applications. This specification
does not prevent the application from running on devices with lower API
Levels down to minSdkVersion. If left undeclared, its value is set equal to
minSdkVersion.
• icon: This is the application's icon.
• name: This is the main class of the application (or the main activity). Note that
in terms of Libgdx, this will be the Starter Class for Android.
• label: This is the application's name shown next to the application icon and
in the title bar.
• screenOrientation: This defines the display orientation of the application.
The usual values are portrait (tall) and landscape (wide). See the
documentation for more details.
Another crucial part of the manifest file is the correct definition of the permissions
that the application should request when a user wants to install it on a device.
Now, right-click on the demo-android project in Project Explorer in Eclipse and
navigate to Run As | Android Application.
The Android application should now be installed as an application icon and should
be happily running on your Android device. The following photograph is of the
application running on an HTC Desire HD
The Starter Class for the Android application is called MainActivity.java.
The following listing is MainActivity.java from demo-android:
package com.packtpub.libgdx.demo;
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android
.AndroidApplicationConfiguration;
public class MainActivity extends AndroidApplication {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg =
new AndroidApplicationConfiguration();
cfg.useGL20 = false;
initialize(new MyDemo(), cfg);
}
}
In the preceding code listing, you can see the MainActivity class that inherits
from the AndroidApplication class. This is how Libgdx encapsulates tasks
such as creating a so-called activity that registers handlers to process touch
input, read sensor data, and much more. What is left to do for you is to create
an instance of a class that implements the ApplicationListener interface.
In this case, it is an instance of the MyDemo class. The instances of MyDemo
and AndroidApplicationConfiguration are passed as arguments to the
initialize() method. The configuration is set to not request OpenGL ES 2.0
support on an Android device.
If you are interested in the latest development of Android hardware statistics,
be sure to check out Dashboards on the official Android Developers website:
http://developer.android.com/about/dashboards/index.html#OpenGL.
Additionally, on Android, you will have to take care of a manifest file that defines
a huge list of parameters to configure the application. If you are not yet familiar
with Android's manifest file, you should take some time to read the official
documentation at http://developer.android.com/guide/topics/manifest/
manifest-intro.html.
The following listing is AndroidManifest.xml from demo-android:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.packtpub.libgdx.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="5"
android:targetSdkVersion="17" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges=
"keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The following short (and incomplete) list is meant to give you a quick idea of what
could be defined in the manifest file:
• minSdkVersion: This is the minimum API Level required for the application
to run. Devices running with lower API Levels will not be able to run this
application; or if left undeclared, an API Level of 1 is assumed, which may
cause your app to crash at runtime when trying to access unavailable APIs.
• targetSdkVersion: This is the API Level the application targets. This is
used for forward compatibility, where later API Levels may change the
behavior of the API that might break old applications. This specification
does not prevent the application from running on devices with lower API
Levels down to minSdkVersion. If left undeclared, its value is set equal to
minSdkVersion.
Additionally, on Android, you will have to take care of a manifest file that defines
a huge list of parameters to configure the application. If you are not yet familiar
with Android's manifest file, you should take some time to read the official
documentation at http://developer.android.com/guide/topics/manifest/
manifest-intro.html.
The following listing is AndroidManifest.xml from demo-android:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.packtpub.libgdx.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="5"
android:targetSdkVersion="17" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges=
"keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The following short (and incomplete) list is meant to give you a quick idea of what
could be defined in the manifest file:
• minSdkVersion: This is the minimum API Level required for the application
to run. Devices running with lower API Levels will not be able to run this
application; or if left undeclared, an API Level of 1 is assumed, which may
cause your app to crash at runtime when trying to access unavailable APIs.
• targetSdkVersion: This is the API Level the application targets. This is
used for forward compatibility, where later API Levels may change the
behavior of the API that might break old applications. This specification
does not prevent the application from running on devices with lower API
Levels down to minSdkVersion. If left undeclared, its value is set equal to
minSdkVersion.
• icon: This is the application's icon.
• name: This is the main class of the application (or the main activity). Note that
in terms of Libgdx, this will be the Starter Class for Android.
• label: This is the application's name shown next to the application icon and
in the title bar.
• screenOrientation: This defines the display orientation of the application.
The usual values are portrait (tall) and landscape (wide). See the
documentation for more details.
Another crucial part of the manifest file is the correct definition of the permissions
that the application should request when a user wants to install it on a device.
Now, right-click on the demo-android project in Project Explorer in Eclipse and
navigate to Run As | Android Application.
The Android application should now be installed as an application icon and should
be happily running on your Android device. The following photograph is of the
application running on an HTC Desire HD