This Article Describes How To Create Android Applications With Eclipse. 30.12.2009 The Article Is Based On Eclipse 3.5 and Android 2.0
This Article Describes How To Create Android Applications With Eclipse. 30.12.2009 The Article Is Based On Eclipse 3.5 and Android 2.0
Table of Contents
1. Overview
1.1. Android
1.2. Android Application
2. Installation
2.1. Eclipse
2.2. Android
2.3. Configuration
2.4. Device
3. Your first Android project
3.1. Create Project
3.2. Add UI Elements
3.3. Create and use attributes
3.4. Code your applicatioin
3.5. Define the button handler
3.6. Start Project
3.7. Using the phone menue
4. Important views
4.1. Log
4.2. File explorer
5. Networking
5.1. Networking
5.2. Proxy
5.3. Permissions
5.4. Example
6. ContentProvider
6.1. Overview
6.2. Create Contacts
6.3. Example
7. Shell
7.1. Opening the Shell
7.2. Emulator Console
7.3. Uninstall an application
8. Location API
8.1. Device with Google API
8.2. Project and Permissions
8.3. Google Map library
8.4. Layout
8.5. Activity
8.6. Run and Test
9. Thank you
10. Questions and Discussion
11. Links and Literature
11.1. Source Code
11.2. Android Resources
11.3. Other Resources
1. Overview
1.1. Android
Android is an operating system based on Linux with a Java programming interface. It provides tools, e.g. a compiler, debugger and a device emulator
as well as its own Java Virtual machine (Dalvik).
Android is created by the Open Handset Alliance which is lead by Google.
Android uses a special Java virtual machine (Dalvik) which is based on the Apache Harmony Java implementation. Dalvik uses a special Bytecode
so that you have to use the Android compiler to create this special byte-code.
Android supports 2-D and 3-D graphics using the OpenGL libraries and supports data storage in a SQLLite database.
For development Google provides the Android Development Tools (ADT) for Eclipse to develop Android applications.
2.1. Eclipse
Use the update manager of Eclipse to install all available plugins for the Android Development Tools (ADT) from the URL https://dl-
ssl.google.com/android/eclipse/ . See Using the Eclipse update manager for details on how to use the update manager and how to install new plugins.
Tip
Ehe Eclipse Android SDK does not seem to have an option to install the Android (Java) source code to make it available in Eclipse.
Please join me in starring at bug Make Android Source available in Eclipse - Bug report .
2.2. Android
Download the Android SDK from the Android homepage under Android Homepage .
The download contains a zip file which you can extract to any place in your file system, e.g. I placed it under "c:\android-sdk-windows" .
2.3. Configuration
In Eclipse open the Preferences dialog via Windows -> Preferences. Select Android and maintain the installation path of the Android SDK.
Tip
If you maintain the location the Android plugin will remind you frequently (and for every workspace). Join me in starring at Bug
3210 to get this improved.
Select now Window -> Android SDK and AVD Manager from the menu.
Select available packages and select everything expect the older version of the SDK.
Press "Install selected" and confirm the license for all package.
After the installation restart Eclipse.
2.4. Device
You need to define a device which can be used for emulation. Press the device manager button, press "New" and maintain the following.
Press "Create AVD".
To test if you setup is correct, eelect your device and press "Start".
After (a long time) your device should be started.
Tip
You can use the perspective "DDMS" to monitor your device.
3. Your first Android project
3.1. Create Project
. Select File -> New -> Other -> Android -> Android Project and create the Android project "de.vogella.android.first" Maintain the following.
Press "Finish".
This should create the following directory structure.
R.java is a generated class which contains the text and the UI elements. Please do not try to modify this class manually.
Go back to "main.xml", select the complete widget and use the Properties view to set the background to this attribute.
3.4. Code your applicatioin
Change your code in "Hello.java" to the following.
package de.vogella.android.first;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
case R.id.Button03:
text.setText("Button 3 was clicked");
break;
}
}
}
Tip
The next chapter will connect the handler methods with the buttons via XML.
3.5. Define the button handler
Open again main.xml and select your first button. Via the property view assign the method "myClickHandlerButton" to the "on Click" property of the
first button.
Tip
Be patient, the emulator is sometimes very slow.
You should get the following result.
3.7. Using the phone menue
If you press the Home button you can also select your application.
4. Important views
4.1. Log
You can see the log (including System.out.print() statements) via the LogCat view.
4.2. File explorer
The file explorer allows to see the files on the android simulator.
5. Networking
5.1. Networking
Android allows to access the network via the the java.net.URL class.
Tip
You can also read XML, e.g. RSS feeds. Unfortunately Android does not have a Stax parser included in it SDK. Vote for Android
should have a Stax parser to get support. Currently you have to use the android specific class XmlPullParser.
5.2. Proxy
To set the proxy you can use the class Settings. For example you could add the following line to your onCreate method in your activity.
Tip
It seems that DNS resolving doesn't work behind a proxy. See Bug 2764
5.3. Permissions
You also have to give your application the right to change the settings "android.permission.WRITE_SETTINGS" in "AndroidManifest.xml".
5.4. Example
Create the project "de.vogella.android.network.html". Add the following elements to your activity:
EditText with the ID "address"
TextView with the ID "pagetext"
Button with the ID "ReadWebPage"
Create the following code to read a webpage and show the HTML code in the TextView.
This example also demonstrate the usage of Android preferences to store user data. The URL which the user has typed is stored in the preferences in
the method onPause(). This method is called whenever the Activity is send into the background.
package de.vogella.android.network.html;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadPreferences();
urlText.setText(lastUrl);
}
/**
* Demonstrates loading of preferences The last value in the URL string will
* be loaded
*/
private void loadPreferences() {
SharedPreferences preferences = getSharedPreferences(PREFERENCES,
Activity.MODE_PRIVATE);
// Set this to the Google Homepage
lastUrl = preferences.getString(URL, "http://209.85.229.147");
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences preferences = getSharedPreferences(PREFERENCES,
Activity.MODE_PRIVATE);
Editor preferenceEditor = preferences.edit();
preferenceEditor.putString(URL, urlText.getText().toString());
// You have to commit otherwise the changes will not be remembered
preferenceEditor.commit();
}
catch (Exception e) {
System.out.println("Nay, did not work");
textView.setText(e.getMessage());
}
break;
}
}
Assign the handler "buttonHandler" to the button in the property "on Click". via your XML.
6. ContentProvider
6.1. Overview
ContentProvider are used to provide data from an application to another. ContentProvider do not store the data but provide the interface for other
applications to access the data.
The following example will use an existing context provider from "Contacts".
6.2. Create Contacts
Start the contacts application and create a few contacts.
6.3. Example
Create a new Android project "de.vogella.android.contentprovider" with the activity "ContactsView".
Rename the id of the the existing TextView from the example wizard to "contactview". Delete the default text. Also change the layout_height to
"fill_parent".
The resulting main.xml should look like the following.
In Application.xml add the Permission that the application can use "android.permission.READ_CONTACTS".
Change now your coding of your activity.
package de.vogella.android.contentprovider;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.widget.TextView;
7. Shell
7.1. Opening the Shell
You can access your Android emulator also via the console. Open a shell, switch to your "android-sdk" installation directory into the folder "tools".
Start the shell via the command "adb shell".
8. Location API
The location API allow you to determine your current location.
The following requires that you have installed the Googles API (see installation) and a valid Google map API key. Go to Obtaining a Maps API Key
to get one.
Tip
The maintenance of "Uses permissions" should be enhanced. Please stare at Bug: Permissions should support field assists to get this
improved.
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="Your Maps API Key"
/>
</RelativeLayout>
Tip
Replace "Your Maps API Key" with your Google API key.
8.5. Activity
Create the following activity. This activity use an LocationListner to update the map with the current location.
package de.vogella.android.locationapi;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.ZoomControls;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
mapController.setZoom(14);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new GeoUpdateHandler());
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
mapController.setCenter(point);
// setContentView(mapView);
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
8.6. Run and Test
Run and test your application. You should be able to zoon in and out. Use Emulator console to send geo-coordinates to your device for example
9. Thank you
Thank you for practicing with this tutorial.
Please note that I maintain this website in my private time. If you like the information I'm providing please help me by donating.