You are on page 1of 54

by Lars Vogel

Tutorial
2.8k
Android Development Tutorial
Based on Android 4.0
Lars Vogel
Version 10.3
Copyright 2009, 2010, 2011, 2012 Lars Vogel
17.04.2012
Revision History
Revision 0.1 04.07.2009
Created
Revision 0.2 - 10.3 07.07.2009 - 17.04.2012
bug fixing and enhancements
Development with Android and Eclipse
This tutorial describes how to create Android applications with Eclipse. It is based on Eclipse 3.7 (Indigo),
Java 1.6 and Android 4.0 (Ice Cream Sandwich).
Table of Contents
1. What is Android?
1.1. Android Operation System
1.2. Google Play (Android Market)
1.3. Security and permissions
2. Android components
2.1. Activity
2.2. Views and ViewGroups
2.3. Intents
2.4. Services
2.5. ContentProvider
2.6. BroadcastReceiver
2.7. (HomeScreen) Widgets
2.8. Other
3. Android Development Tools
3.1. What are the Android Development Tools?
3.2. Dalvik Virtual Machine
3.3. How to develop Android Applications
3.4. Resource editors
3.5. Logging
4. Android Application Architecture
4.1. AndroidManifest.xml
4.2. R.java and Resources
4.3. Assets
4.4. Activities and Layouts
4.5. Reference to resources in XML files
4.6. Activities and Lifecycle
4.7. Configuration Change
4.8. Context
5. Installation
5.1. Eclipse
5.2. Pre-requisites for using a 64bit Linux
5.3. Install ADT Plug-ins and Android SDK
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 1 of 54
5.6. Android Source Code
6. Android virtual device - Emulator
6.1. What is the Android Emulator?
6.2. Google vs. Android AVD
6.3. Emulator Shortcuts
6.4. Performance
6.5. Hardware button
7. Tutorial: Create and run Android Virtual Device
8. Error handling and typical problems
8.1. Workspace
8.2. Clean Project
8.3. Problems with Android Debug Bridge (adb)
8.4. LogCat
8.5. Android editor not opened
8.6. Emulator does not start
8.7. Timeout during deployment
8.8. Installation failed due to insufficient storage
8.9. Debug Certificate expired
8.10. Error message for @override
8.11. Missing Imports
8.12. Eclipse Tips
9. Conventions for the tutorials
9.1. API version, package and application name
9.2. Warning messages for Strings
10. Tutorial: Your first Android project
10.1. Install the demo application
10.2. Create Project
10.3. Modifying resources
10.4. Create attributes
10.5. Add Views
10.6. Edit View properties
10.7. Change the Activity source code
10.8. Start Project
11. Starting an installed application
12. OptionMenu and ActionBar
12.1. ActionBar
12.2. OptionsMenu
12.3. Creating the menu
12.4. Reacting to menu entry selection
12.5. Using the home icon
12.6. ActionBar tabs
12.7. Custom Views in the ActionBar
12.8. Contextual action mode
12.9. Context menus
13. Tutorial: ActionBar
13.1. Project
13.2. Add a menu XML resource
14. Tutorial: Using the contextual action mode
15. Layout Manager and ViewGroups
15.1. Available Layout Manager
15.2. LinearLayout
15.3. RelativeLayout
15.4. Gridlayout
15.5. ScrollView
16. Tutorial: ScrollView
17. Drawables
17.1. Drawables
17.2. Shape Drawables
17.3. State Drawables
17.4. Transition Drawables
17.5. 9 Patch Drawables
18. Styles and Themes
18.1. Styles
18.2. Attributes
18.3. Themes
18.4. Android Design Page
19. Supporting different screen sizes
19.1. Using device independent pixel
19.2. Using resource qualifiers
20. Fragments
20.1. Fragments Overview
20.2. When to use Fragments
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 2 of 54
21.2. Create project
21.3. Create layouts for portrait mode
21.4. Create Fragment classes
21.5. Create layouts for landscape mode
21.6. Activities
21.7. Run
22. ActionBar navigation with Fragments
23. DDMS perspective and important views
23.1. DDMS - Dalvik Debug Monitor Server
23.2. LogCat View
23.3. File explorer
24. Shell
24.1. Android Debugging Bridge - Shell
24.2. Uninstall an application via adb
24.3. Emulator Console via telnet
25. Deployment
25.1. Overview
25.2. Deployment via Eclipse
25.3.
25.4. Via external sources
25.5. Google Play (Market)
26. Thank you
27. Questions and Discussion
28. Links and Literature
28.1. Source Code
28.2. Android Resources
28.3. vogella Resources
1. What is Android?
1.1. Android Operation System
Android is an operating system based on Linux with a Java programming interface.
The Android Software Development Kit (Android SDK) provides all necessary tools to develop Android
applications. This includes a compiler, debugger and a device emulator, as well as its own virtual
machine to run Android programs.
Android is currently primarily developed by Google.
Android allows background processing, provides a rich user interface library, supports 2-D and 3-D
graphics using the OpenGL libraries, access to the file system and provides an embedded SQLite
database.
Android applications consist of different components and can re-use components of other applications.
This leads to the concept of a task in Android; an application can re-use other Android components to
archive a task.
For example you can write an application which use the Android Gallery application to pick a photo.
1.2. Google Play (Android Market)
Google offers the "Google Play" service in which programmers can offer their Android application to
Android users. Google phones include the "Google Play" application which allows to install applications.
Google Play also offers an update service, e.g. if a programmer uploads a new version of his application
to Google Play, this service will notify existing users that an update is avaiable and allow to install it.
Google Play used to be called "Android Market".
1.3. Security and permissions
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 3 of 54
every Android application. Each application file is private to this generated user, e.g. other applications
cannot access these files.
In addition each Android application will be started in its own process.
Therefore by means of the underlying Linux operating system, every Android application is isolated from
other running applications.
If data should be shared, the application must do this explicitly, e.g. via a Service or a
ContentProvider.
Android also contains a permission system. Android predefines permissions for certain tasks but every
application can define additional permissions.
An Android application declare its required permissions in its AndroidManifest.xml configuration file.For
example an application may declare that it requires Internet
Permissions have different levels. Some permissions are automatically granted by the Android system,
some are automatically rejected.
In most cases the requested permissions will be presented to the user before installation of the
application. The user needs to decided if these permissions should be given to the application.
If the user denies a permission required by the application, this application cannot be installed. The
check of the permission is only performed during installation, permissions cannot be denied or granted
after the installation.
While not all users pay attention to the required permissions during installation some some users do and
they write bad reviews on Google Play.
2. Android components
The following gives a short overview of the most important Android components.
2.1. Activity
Activity represents the presentation layer of an Android application. A simplified description is that an
Activity represents a screen in your Android application. This is slightly incorrect as Activities can
be displayed as Dialogs or can be transparent.
An Android application can have several Activities.
2.2. Views and ViewGroups
Views are user interface widgets, e.g. buttons or text fields. The base class for all Views is
android.view.View. Views often have attributes which can be used to change their appearance and
behavior.
A ViewGroup is responsible for arranging other Views. A ViewGroup is also called layout manager. The
base class for a layout manager is android.view.ViewGroups which extends extends View.
ViewGroups can be nestled to create complex layouts. You should not nestle ViewGroups too deeply
as this has a negative impact on the performance.
2.3. Intents
Intents are asynchronous messages which allow the application to request functionality from other
components of the Android system, e.g. from Services or Activities. An application can call a
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 4 of 54
certain Intent (implicit Intents ). For example the application could implement sharing of data via an
Intent and all components which allow sharing of data would be available for the user to select.
Applications register themselves to an Intent via an IntentFilter.
Intents allow to combine loosely coupled components to perform certain tasks.
2.4. Services
Services perform background tasks without providing a user interface. They can notify the user via the
notification framework in Android.
2.5. ContentProvider
ContentProvider provides a structured interface to application data. Via a ContentProvider your
application can share data with other applications. Android contains an SQLite database which is
frequently used in conjunction with a ContentProvider to persist the data of the ContentProvider.
2.6. BroadcastReceiver
BroadcastReceiver can be registered to receive system messages and Intents. A
BroadcastReceiver will get notified by the Android system, if the specified situation happens. For
example a BroadcastReceiver could get called once the Android system completed the boot process
or if a phone call is received.
2.7. (HomeScreen) Widgets
Widgets are interactive components which are primarily used on the Android homescreen. They
typically display some kind of data and allow the user to perform actions via them. For example a
Widget could display a short summary of new emails and if the user selects an email, it could start the
email application with the selected email.
2.8. Other
Android provide many more components but the list above describes the most important ones. Other
Android components are "Live Folders" and "Live Wallpapers". Live Folders display data on the
homescreen without launching the corresponding application.
3. Android Development Tools
3.1. What are the Android Development Tools?
Google provides the Android Development Tools (ADT) to develop Android applications with Eclipse. ADT
is a set of components (plug-ins) which extend the Eclipse IDE with Android development capabilities.
ADT contains all required functionalities to create, compile, debug and deploy Android applications from
the Eclipse IDE and from the command line. Other IDE's, e.g. IntellJ, are also reusing components of
ADT.
ADT also provides an Android device emulator, so that Android applications can be tested without a real
Android phone.
3.2. Dalvik Virtual Machine
The Android system uses a special virtual machine, i.e. the Dalvik Virtual Machine to run Java based
applications. Dalvik uses an own bytecode format which is different from Java bytecode.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 5 of 54
bytecode format.
3.3. How to develop Android Applications
Android applications are primarily written in the Java programming language. The Java source files are
converted to Java class files by the Java compiler.
Android provides a tool called "dx"" which converts Java class files into a dex (Dalvik Executable) file. All
class files of one application are placed in one compressed .dex file. During this conversion process
redundant information in the class files are optimized in the .dex file. For example if the same String is
found in different class files, the .dex file contains only once reference of this String.
These dex files are therefore much smaller in size than the corresponding class files.
The .dex file and the resources of an Android project, e.g. the images and XML files, are packed into an
.apk (Android Package) file. The program aapt (Android Asset Packaging Tool) performs this
packaging.
The resulting .apk file contains all necessary data to run the Android application and can be deployed to
an Android device via the "adb" tool.
The Android Development Tools (ADT) allows that all these steps are performed transparently to the
user; either within Eclipse or via the command line.
If you use the ADT tooling you press a button or run a script and the whole Android application (.apk file)
will be created and deployed.
3.4. Resource editors
The ADT allows the developer to define certain artifacts, e.g. Strings and layout files, in two ways: via a
rich editor, and directly via XML. This is done via multi-page editors in Eclipse. In these editors you can
switch between both representations by clicking on the tab on the lower part of the screen.
For example if you open the "res/layout/main.xml" file in the Package Explorer, you can switch between
the two representations as depicted in the following screenshot.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 6 of 54
3.5. Logging
Android uses the android.util.Log class for logging with the Log.i(), Log.w() and Log.e() and
Log.wtf() methods for logging. This list is sorted by severence.
The first parameter of these method is the category and the second is the message.
Typically you create a Constants class in your Android application and provide your log flag as a public
static final field.
package package package package de.voge11a.ando1d.f1sf
pub11c pub11c pub11c pub11c c1ass c1ass c1ass c1ass Consfanfs {
pub11c pub11c pub11c pub11c sfaf1c sfaf1c sfaf1c sfaf1c f1na1 f1na1 f1na1 f1na1 5f1ng LOG = "de.voge11a.ando1d.f1sf"
}
Android advises that a deployed application should not contain logging code. The Android development
tools provide the BuildConfig.DEBUG flag for this purpose. This flag will be automatically set to false,
if you export the Android application for deployment. During development it will be set to true, therefore
allow you to see your logging statements during development.
The following example show how to write an error log message. This message is visible in the "LogCat"
view in Eclipse.
1f 1f 1f 1f {8u11dConf1g.DL8uG} {
Log.e{Consfanfs.LOG, "onCeafe ca11ed"}
}
4. Android Application Architecture
4.1. AndroidManifest.xml
The components and settings of an Android application are described in the file
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 7 of 54
declared in this file.
It must also contain the required permissions for the application. For example if the application requires
network access it must be specified here.
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<man1fesf xm1ns:ando1d="hffp://schemas.ando1d.com/apk/es/ando1d"
package="de.voge11a.ando1d.fempeafue"
ando1d:ves1onCode="1"
ando1d:ves1onName="1.0">
<app11caf1on ando1d:1con="0daWab1e/1con" ando1d:1abe1="0sf1ng/appname">
<acf1v1fy ando1d:name=".Convef"
ando1d:1abe1="0sf1ng/appname">
<1nfenf-f11fe>
<acf1on ando1d:name="ando1d.1nfenf.acf1on.MAlN" />
<cafegoy ando1d:name="ando1d.1nfenf.cafegoy.LAuNChLk" />
</1nfenf-f11fe>
</acf1v1fy>
</app11caf1on>
<uses-sdk ando1d:m1n5dkves1on="9" />
</man1fesf>
The package attribute defines the base package for the Java objects referred to in this file. If a Java
object lies within a different package, it must be declared with the full qualified package name.
Google Play requires that every Android application uses its own unique package. Therefore it is a good
habit to use your reverse domain name as package name. This will avoid collisions with other Android
applications.
android:versionName and android:versionCode specify the version of your application.
versionName is what the user sees and can be any String.
versionCode must be an integer. The Android Market determine based on the versionCode, if it
should perform an update of the applications for the existing installations. You typically start with "1" and
increase this value by one, if you roll-out a new version of your application.
The tag <activity> defines an Activity, in this example pointing to the Convert class in the
de.vogella.android.temperature package. An intent filter is registered for this class which defines
that this Activity is started once the application starts (action
android:name="android.intent.action.MAIN" ). The category definition category
android:name="android.intent.category.LAUNCHER" defines that this application is added to
the application directory on the Android device.
The @string/app_name value refers to resource files which contain the actual value of the application
name. The usage of resource file makes it easy to provide different resources, e.g. strings, colors, icons,
for different devices and makes it easy to translate applications.
The "uses-sdk" part of the "AndroidManifest.xml" file defines the minimal SDK version for which your
application is valid. This will prevent your application being installed on devices with older SDK versions.
4.2. R.java and Resources
The " gen " directory in an Android project contains generated values. R.java is a generated class
which contains references to certain resources of the project.
These resources must be defined in the "res" directory and can be XML files, icons or pictures. You can
for example define values, menus, layouts or animations via XML files.
If you create a new resource, the corresponding reference is automatically created in R.java via the
Eclipse ADT tools. These references are static int values and define ID's for the resources.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 8 of 54
For example to access a String with the R.string.yourString ID, you would use the
getString(R.string.yourString)) method.
R.java is automatically created by the Eclipse development environment, manual changes are not
necessary and will be overridden by the tooling.
4.3. Assets
While the res directory contains structured values which are known to the Android platform, the assets
directory can be used to store any kind of data. You access this data via the AssetsManager which you
can access the getAssets() method.
AssetsManager allows to read an assets as InputStream with the open() method.
// Gef fhe AssefManage
AssefManage manage = gefAssefs{}
// kead a 81fmap fom Assefs
fy fy fy fy {
lnpuf5feam open = manage.open{"1ogo.png"}
81fmap b1fmap = 81fmapIacfoy.decode5feam{open}
// Ass1gn fhe b1fmap fo an lmagev1eW 1n fh1s 1ayouf
lmagev1eW v1eW = {lmagev1eW} f1ndv1eW8yld{k.1d.1magev1eW1}
v1eW.seflmage81fmap{b1fmap}
} cafch cafch cafch cafch {lOLxcepf1on e} {
e.p1nf5fack1ace{}
}
4.4. Activities and Layouts
The user interface for Activities is defined via layouts. The layout defines the included Views
(widgets) and their properties.
A layout can be defined via Java code or via XML. In most cases the layout is defined as an XML file.
XML based layouts are defined via a resource file in the /res/layout folder. This file specifies the
ViewGroups, Views, their relationship and their attributes for this specific layout.
If a View needs to be accessed via Java code, you have to give the View a unique ID via the
android:id attribute. To assign a new ID to a View use @+id/yourvalue. The following shows an
example in which a Button gets the "button1" ID assigned.
<8uffon
ando1d:1d="0+1d/buffon1"
ando1d:1ayoufW1dfh="Wapconfenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:fexf="5hoW Pefeences" >
</8uffon>
By conversion this will create and assign a new yourvalue ID to the corresponding View. In your Java
code you can later access a View via the method findViewById(R.id.yourvalue).
Defining layouts via XML is usually the preferred way as this separates the programming logic from the
layout definition. It also allows the definition of different layouts for different devices. You can also mix
both approaches.
4.5. Reference to resources in XML files
In your XML files, for example your layout files, you can refer to other resources via the @ sign.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 9 of 54
@color/your_id. Or if you defined a "hello" string in an XML resource, you could access it via
@string/hello.
4.6. Activities and Lifecycle
The Android system controls the lifecycle of your application. At any time the Android system may stop or
destroy your application, e.g. because of an incoming call. The Android system defines a lifecycle for
Activities via predefined methods. The most important methods are:
onSaveInstanceState() - called if the Activity is stopped. Used to save data so that the
Activity can restore its states if re-started
onPause() - always called if the Activity ends, can be used to release resource or save data
onResume() - called if the Activity is re-started, can be used to initialize fields
4.7. Configuration Change
An Activity will also be restarted, if a so called "configuration change" happens. A configuration
change happens if an event is triggered which may be relevant for the application. For example if the
user changes the orientation of the device (vertically or horizontally). Android assumes that an Activity
might want to use different resources for these orientations and restarts the Activity.
In the emulator you can simulate the change of the orientation via CNTR+F11.
You can avoid a restart of your application for certain configuration changes via the configChanges
attribute on your Activity definition in your AndroidManifest.xml. The following Activity will not
be restarted in case of orientation changes or position of the physical keyboard (hidden / visible).
<acf1v1fy ando1d:name=".Pogess1esfAcf1v1fy"
ando1d:1abe1="0sf1ng/appname"
ando1d:conf1gChanges="o1enfaf1on|keyboadh1dden|keyboad">
</acf1v1fy>
4.8. Context
The class android.content.Context provides the connections to the Android system. It is the
interface to global information about the application environment. Context also provides access to
Android Services, e.g. the Location Service. Activities and Services extend the Context class
and can therefore be used as Context.
5. Installation
5.1. Eclipse
The following assumes that you have already Java and Eclipse installed and know how to use Eclipse.
For an introduction into Eclipse please see the following tutorial: Eclipse IDE Tutorial.
The tutorial above also describes how to install new components into Eclipse. This is required to install
the Android Development Tools. You find the necessary steps described in the following section of the
tutorial: Eclipse Update Manager.
The author of this text has also published a Kindle book on the usage of the Eclipse IDE, which can be
found here: Eclipse IDE Book for Kindle.
5.2. Pre-requisites for using a 64bit Linux
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 10 of 54
installed. For Ubuntu you can do this via the following command.
apf-gef 1nsfa11 1a32-11bs
Please check your distribution documentation, if you are using a different flavor of Linux.
5.3. Install ADT Plug-ins and Android SDK
Use the Eclipse update manager to install all available components for the Android Development Tools
(ADT) from the URL https://dl-ssl.google.com/android/eclipse/.
After the new Android development components are installed, you will be prompted to install the Android
SDK. You can use the following wizard or go to the next section to learn how to do it manually.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 11 of 54
5.4. Manual installation of the Android SDK
After the installation of the ADT the Eclipse tooling allows to download the Android SDK automatically.
Alternatively you can also manually download the Android SDK from the Android SDK download page.
The download contains a zip file, which you can extract to any place in your file system, e.g. on my Linux
system I placed it under "/home/vogella/android-sdks". Avoid using spaces in the path name, otherwise
you may experience problems with the usage of the Android SDK.
You also have to define the location of the Android SDK in the Eclipse Preferences. In Eclipse open the
Preferences dialog via the menu Windows Preferences. Select Android and enter the installation path
of the Android SDK.
5.5. Install a specific Android version
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 12 of 54
SDK Manager from the Eclipse menu.
The dialog allows you to install new packages and also allows you to delete them.
Select "Available packages" and open the "Third Party Add-ons". Select the Google API 15 (Android
4.0.3) version of the SDK and press "Install".
Press the "Install" button and confirm the license for all packages. After the installation completes,
restart Eclipse.
5.6. Android Source Code
The following step is optional.
5.6.1. As of Android 4.0
During Android development it is very useful to have the Android source code available.
As of Android 4.0 the Android development tools provides also the source code. You can download it via
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 13 of 54
The sources are downloaded to the source directory located in "path_to_android_sdk/sources
/android-xx". xx is the API level of Android, e.g. 15 for the Android 4.0.3 version.
To connect the sources with the android.jar file in your Android project, right click on your android.jar in
the Eclipse Package Explorer and select Properties Java Source Attachment. Type in the source
directory name and press OK.
Afterwards you can browse through the source code.
5.6.2. Prior to Android 4.0
For earlier versions Haris Peco maintains plugins, which provide the Android Source code code. Use the
Eclipse update manager to install the Android Source plugin from the following update site: "http://adt-
addons.googlecode.com/svn/trunk/source/com.android.ide.eclipse.source.update".
More details can be found on the project website.
6. Android virtual device - Emulator
6.1. What is the Android Emulator?
The Android Development Tools (ADT) includes an emulator to run an Android system. The emulator
behaves like a real Android device (in most cases) and allows you to test your application without having
a real device.
You can configure the version of the Android system you would like to run, the size of the SD card, the
screen resolution and other relevant settings. You can define several of them with different
configurations.
These devices are called "Android Virtual Device" (AVD) and you can start several in parallel. Starting a
new emulator is very slow, because the file system of the new AVD needs to get prepared.
The ADT allows to deploy and run your Android program on the AVD.
6.2. Google vs. Android AVD
During the creation of an AVD you decide if you want an Android device or a Google device.
An AVD created for Android will contain the programs from the Android Open Source Project. An AVD
created for the Google API's will also contain several Google applications, most notable the Google
Maps application.
If you want to use functionality which is only provided via the Google API's, e.g. Cloud2DeviceMessaging
or Google Maps you must run this application on an AVD with Google API's.
6.3. Emulator Shortcuts
The following shortcuts are useful for working with the emulator.
Alt+Enter Maximizes the emulator. Nice for demos.
Ctrl+F11 changes the orientation of the emulator.
F8 Turns network on / off.
6.4. Performance
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 14 of 54
use a small resolution for your emulator, as for example HVGA.
Also if you have sufficient memory on your computer, add at least 1 GB of memory to your emulator. This
is the value "Device ram size" during the creation of the AVD.
Also set the flag "Enabled" for Snapshots. This will save the state of the emulator and will let it start
much faster.
6.5. Hardware button
Android 4.0 introduced that devices do not have to have hardware button anymore. If you want to create
such an AVD, add the "Hardware Back/Home keys" property to the device configuration and set it to
"false".
7. Tutorial: Create and run Android Virtual Device
To define an Android Virtual Device (ADV) open the "AVD Manager" via Windows AVD Manager and
press "New".
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 15 of 54
Enter the following.
We can also select the box "Enabled" for Snapshots. This will make the second start of the virtual device
much faster.
At the end press the button "Create AVD". This will create the AVD configuration and display it under the
"Virtual devices".
To test if your setup is correct, select your device and press "Start".
After (a long time) your AVD starts. You are able to use it via the mouse and via the virtual keyboard of
the emulator.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 16 of 54
8. Error handling and typical problems
Things are not always working as they should. This section gives an overview over typical problems and
how to solve them.
8.1. Workspace
Never use a workspace in Eclipse which has spaces in the directory path name, this may lead to several
strange problems.
8.2. Clean Project
Several users report that they get the following errors:
Project ... is missing required source folder: 'gen' 1.
The project could not be built until build path errors are resolved. 2.
Unable to open class file R.java. 3.
To solve any of these errors, go to the project menu and select Project Clean.
8.3. Problems with Android Debug Bridge (adb)
The communication with the emulator or your Android device might have problems. This communication
is handled by the Android Debug Bridge (adb).
Eclipse allows to reset the adb in case this causes problems. Select therefore the DDMS perspective via
Window Open Perspective Other DDMS
To restart the adb, select the "Reset adb" in the Device View.
8.4. LogCat
The "LogCat" View shows you the log messages of your Android device and helps you to analyze
problems. For example Java exceptions in your program would be shown here. To open this view, select
Window Show View Other Android LogCat.
8.5. Android editor not opened
Android provides nice editors to edit Android resource files, unfortunately these editor are not always
automatically used due to bugs in the ADT. If that happens, you can open this editor manually.
Right-click on your menu file and select Open with Android Menu Editor.
8.6. Emulator does not start
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 17 of 54
the path name.
8.7. Timeout during deployment
If you face timeout issues during deployment you can increase the default timeout in the Eclipse
preferences. Select Window Preferences Android DDMS and increase the "ADB connection
timeout (in ms)" value.
8.8. Installation failed due to insufficient storage
Sometimes the emulator will refuse to install an application with the error message:
INSTALL_FAILED_INSUFFICIENT_STORAGE.
An Android virtual device provides per default only 64M for the storaging Android applications. You can
clean your installed application by re-starting the emulator and selecting the "Wipe user data" flag.
Alternatively you can set the data partition size. If you press edit on the AVD, you can set the "Ideal size
of data partition" property via the "New" button.
8.9. Debug Certificate expired
If you get the error message "Debug Certificate expired" switch to the folder which contains the Android
AVD, e.g. ".android" under Linux and delete the "debug.keystore" file. This file is only valid for a year and
if not present, Eclipse will regenerate the password.
8.10. Error message for @override
The @override annotation was introduced in Java 1.6. If you receive an error message for @override,
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 18 of 54
Compiler Compiler compliance level and select "1.6" in the drop-down box.
8.11. Missing Imports
Java requires that classes which are not part of the standard Java Language are either fully qualified or
declared via imports.
If you see an error message with "XX cannot be resolved to a variable", right-click in your Editor and
select Source Organize Imports to important required packages.
8.12. Eclipse Tips
To work more efficiently with Eclipse, select Window Preferences Java Editor Save Actions
and select that the source code should be formated and that the imports should be organized at every
save action.
9. Conventions for the tutorials
9.1. API version, package and application name
The tutorials of this document have been developed and tested with Android 4.0.4, API Level 15. Please
use this version for all tutorials in this book. Higher versions of the API level should also work. A lower
version of the Android API might also work, but if you face issues, try the recommended version.
The base package for the projects is always the same as the project name, e.g. if you are asked to
create a project "de.vogella.android.example.test", then the corresponding package name is
"de.vogella.android.example.test".
The application name, which must be entered on the Android project generation wizard, will not be
predefined. Choose a name you like.
9.2. Warning messages for Strings
The Android development tools show warnings, if you use hard-coded strings, for example in layout files.
For real applications you should use String resource files. To simplify the creation of the examples, we
use Strings directly. Please ignore the corresponding warnings.
10. Tutorial: Your first Android project
10.1. Install the demo application
This application is also available on the Android Marketplace under Android Temperature converter .
Alternatively you can also scan the following barcode with your Android smartphone to install it via the
Google Play application.
10.2. Create Project
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 19 of 54
"de.vogella.android.temperature". Enter the following.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 20 of 54
Press "Finish". This should create the following directory structure.
10.3. Modifying resources
As described in the Android Development Tools (ADT) chapter, ADT provides specialized editors for
resources files, e.g. layout files. These editors allow to switch between the XML representation of the file
and a richer user interface via tabs on the bottom of the editor.
The following description uses the rich user interface to build layout files. For validation purposes, the
resulting XML is also included in the description.
10.4. Create attributes
Android allows you to create static attributes, e.g. Strings or colors. These attributes can for example be
used in your XML layout files or referred to via Java source code.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 21 of 54
the name and "#3399CC" as the value.
Add the following "String" attributes. String attributes allow the developer to translate the application at a
later point.
Table 1. String Attributes
Name Value
celsius to Celsius
fahrenheit to Fahrenheit
calc Calculate
Switch to the XML representation and validate that the values are correct.
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<esouces>
<sf1ng name="he11o">he11o Wo1d, Convef!</sf1ng>
<sf1ng name="appname">1empeafue Convefe</sf1ng>
<co1o name="myCo1o">#3399CC</co1o>
<sf1ng name="myC11ckhand1e">myC11ckhand1e</sf1ng>
<sf1ng name="ce1s1us">fo Ce1s1us</sf1ng>
<sf1ng name="fahenhe1f">fo Iahenhe1f</sf1ng>
<sf1ng name="ca1c">Ca1cu1afe</sf1ng>
</esouces>
10.5. Add Views
Select "res/layout/main.xml" and open the Android editor via a double-click. This editor allows you to
create the layout via drag and drop or via the XML source code. You can switch between both
representations via the tabs at the bottom of the editor. For changing the position and grouping elements
you can use the Eclipse "Outline" view.
The following shows a screenshot of the "Palette" view from which you can drag and drop new user
interface components into your layout. Please note that the "Palette" view changes frequently so your
view might be a bit different.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 22 of 54
You will now create your new layout.
Right-click on the existing text object Hello World, Hello! in the layout. Select "Delete" from the popup
menu to remove the text object. Then, from the Palette view, select Text Fields and locate "Plain Text".
Drag this onto the layout to create a text input field. All object types in the section "Text Fields derive
from the class "EditText", they just specify via an additional attribute which text type can be used.
Afterwards select the Palette section "Form Widgets" and drag a RadioGroup object onto the layout.
The number of radio buttons added to the radio button group depends on your version of Eclipse. Make
sure there are two radio buttons by deleting or adding radio buttons to the group.
From the Palette section Form Widgets, drag a Button object onto the layout.
The result should look like the following.
Switch to "main.xml" and verify that your XML looks like the following.
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<L1neaLayouf xm1ns:ando1d="hffp://schemas.ando1d.com/apk/es/ando1d"
ando1d:1ayoufW1dfh="f111paenf"
ando1d:1ayoufhe1ghf="f111paenf"
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 23 of 54
<Ld1f1exf
ando1d:1d="0+1d/ed1f1exf1"
ando1d:1ayoufW1dfh="mafchpaenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:fexf="Ld1f1exf" >
</Ld1f1exf>
<kad1oGoup
ando1d:1d="0+1d/ad1oGoup1"
ando1d:1ayoufW1dfh="mafchpaenf"
ando1d:1ayoufhe1ghf="Wapconfenf" >
<kad1o8uffon
ando1d:1d="0+1d/ad1o0"
ando1d:1ayoufW1dfh="Wapconfenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:checked="fue"
ando1d:fexf="kad1o8uffon" >
</kad1o8uffon>
<kad1o8uffon
ando1d:1d="0+1d/ad1o1"
ando1d:1ayoufW1dfh="Wapconfenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:fexf="kad1o8uffon" >
</kad1o8uffon>
</kad1oGoup>
<8uffon
ando1d:1d="0+1d/buffon1"
ando1d:1ayoufW1dfh="Wapconfenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:fexf="8uffon" >
</8uffon>
</L1neaLayouf>
10.6. Edit View properties
If you select a user interface component (an instance of View ), you can change its properties via the
Eclipse "Properties" view. Most of the properties can be changed via the right mouse menu. You can also
edit properties of fields directly in XML. Changing properties in the XML file is much faster, if you know
what you want to change. But the right mouse functionality is nice, if you are searching for a certain
property.
Open your file "main.xml". The EditText control shows currently a default text. We want to delete this
initial text in the XML code. Switch to the XML tab called "main.xml" and delete the
android:text="EditText" property from the EditText part. Switch back to the "Graphical Layout" tab
and check that the text is removed.
Use the right mouse click on the first radio button to assign the "celsius" String attribute to its "text"
property. Assign the "fahrenheit" string attribute to the second radio button.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 24 of 54
From now on, I assume you are able to use the properties menu on user interface components. You can
always either edit the XML file or modify the properties via right mouse click.
Set the property "Checked" to true for the first RadioButton.
Assign "calc" to the text property of your button and assign "myClickHandler" to the "onClick" property.
Set the "Input type" property to "numberSigned" and "numberDecimal" on your EditText.
All your user interface components are contained in a LinearLayout. We want to assign a background
color to this LinearLayout. Right-click on an empty space in Graphical Layout mode, then select Other
Properties All by Name Background. Select Color and then select "myColor" "in the list which is
displayed.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 25 of 54
Switch to the "main.xml" tab and verify that the XML is correct.
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<L1neaLayouf xm1ns:ando1d="hffp://schemas.ando1d.com/apk/es/ando1d"
ando1d:1ayoufW1dfh="f111paenf"
ando1d:1ayoufhe1ghf="f111paenf"
ando1d:backgound="0co1o/myCo1o"
ando1d:o1enfaf1on="vef1ca1" >
<Ld1f1exf
ando1d:1d="0+1d/ed1f1exf1"
ando1d:1ayoufW1dfh="mafchpaenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:1npuf1ype="numbeDec1ma1|numbe51gned" >
</Ld1f1exf>
<kad1oGoup
ando1d:1d="0+1d/ad1oGoup1"
ando1d:1ayoufW1dfh="mafchpaenf"
ando1d:1ayoufhe1ghf="Wapconfenf" >
<kad1o8uffon
ando1d:1d="0+1d/ad1o0"
ando1d:1ayoufW1dfh="Wapconfenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:checked="fue"
ando1d:fexf="0sf1ng/ce1s1us" >
</kad1o8uffon>
<kad1o8uffon
ando1d:1d="0+1d/ad1o1"
ando1d:1ayoufW1dfh="Wapconfenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:fexf="0sf1ng/fahenhe1f" >
</kad1o8uffon>
</kad1oGoup>
<8uffon
ando1d:1d="0+1d/buffon1"
ando1d:1ayoufW1dfh="Wapconfenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:onC11ck="myC11ckhand1e"
ando1d:fexf="0sf1ng/ca1c" >
</8uffon>
</L1neaLayouf>
10.7. Change the Activity source code
During the generation of your new Android project you specified that an Activity called
ConvertActivity should be created. The project wizard created the corresponding Java class.
Change your code in ConvertActivity.java to the following. Note that the myClickHandler will be
called based on the OnClick property of your button.
package package package package de.voge11a.ando1d.fempeafue
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 26 of 54
1mpof 1mpof 1mpof 1mpof ando1d.os.8und1e
1mpof 1mpof 1mpof 1mpof ando1d.v1eW.v1eW
1mpof 1mpof 1mpof 1mpof ando1d.W1dgef.Ld1f1exf
1mpof 1mpof 1mpof 1mpof ando1d.W1dgef.kad1o8uffon
1mpof 1mpof 1mpof 1mpof ando1d.W1dgef.1oasf
pub11c pub11c pub11c pub11c c1ass c1ass c1ass c1ass ConvefAcf1v1fy exfends exfends exfends exfends Acf1v1fy {
p1vafe p1vafe p1vafe p1vafe Ld1f1exf fexf
0Ove1de
pub11c pub11c pub11c pub11c vo1d vo1d vo1d vo1d onCeafe{8und1e savedlnsfance5fafe} {
supe supe supe supe.onCeafe{savedlnsfance5fafe}
sefConfenfv1eW{k.1ayouf.ma1n}
fexf = {Ld1f1exf} f1ndv1eW8yld{k.1d.ed1f1exf1}
}
// 1h1s mefhod 1s ca11ed af buffon c11ck because We ass1gned fhe name fo fhe
// "On C11ck popefy" of fhe buffon
pub11c pub11c pub11c pub11c vo1d vo1d vo1d vo1d myC11ckhand1e{v1eW v1eW} {
sW1fch sW1fch sW1fch sW1fch {v1eW.gefld{}} {
case case case case k.1d.buffon1:
kad1o8uffon ce1s1us8uffon = {kad1o8uffon} f1ndv1eW8yld{k.1d.ad1o0}
kad1o8uffon fahenhe1f8uffon = {kad1o8uffon} f1ndv1eW8yld{k.1d.ad1o1}
1f 1f 1f 1f {fexf.gef1exf{}.1engfh{} == 0} {
1oasf.make1exf{fh1s fh1s fh1s fh1s, "P1ease enfe a va11d numbe",
1oasf.LLNG1hLONG}.shoW{}
efun efun efun efun
}
f1oaf f1oaf f1oaf f1oaf 1npufva1ue = I1oaf.paseI1oaf{fexf.gef1exf{}.fo5f1ng{}}
1f 1f 1f 1f {ce1s1us8uffon.1sChecked{}} {
fexf.sef1exf{5f1ng
.va1ueOf{convefIahenhe1f1oCe1s1us{1npufva1ue}}}
ce1s1us8uffon.sefChecked{fa1se}
fahenhe1f8uffon.sefChecked{fue}
} e1se e1se e1se e1se {
fexf.sef1exf{5f1ng
.va1ueOf{convefCe1s1us1oIahenhe1f{1npufva1ue}}}
fahenhe1f8uffon.sefChecked{fa1se}
ce1s1us8uffon.sefChecked{fue}
}
beak beak beak beak
}
}
// Convefs fo ce1s1us
p1vafe p1vafe p1vafe p1vafe f1oaf f1oaf f1oaf f1oaf convefIahenhe1f1oCe1s1us{f1oaf f1oaf f1oaf f1oaf fahenhe1f} {
efun efun efun efun {{fahenhe1f - 32} " 5 / 9}
}
// Convefs fo fahenhe1f
p1vafe p1vafe p1vafe p1vafe f1oaf f1oaf f1oaf f1oaf convefCe1s1us1oIahenhe1f{f1oaf f1oaf f1oaf f1oaf ce1s1us} {
efun efun efun efun {{ce1s1us " 9} / 5} + 32
}
}
10.8. Start Project
To start the Android Application, select your project, right click on it, and select Run-As Android
Application. If an emulator is not yet running, it will be started. Be patient, the emulator starts up very
slowly.
You should get the following result.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 27 of 54
Type in a number, select your conversion and press the button. The result should be displayed and the
other option should get selected.
11. Starting an installed application
After you run your application on the virtual device, you can start it again on the device. If you press the
"Home" button you can select your application.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 28 of 54
12. OptionMenu and ActionBar
12.1. ActionBar
The ActionBar is located at the top of the Activity that may display the Activity title, navigation
modes, and other interactive items.
The following picture show the ActionBar of a typical Google Application with interactive items and a
nagivation bar.
12.2. OptionsMenu
The application can also open a menu which shows actions via a popup menu. This OptionsMenu is
only available if the phone has a hardware "Options" button. Even if the hardware button is available, it is
recommended to use the ActionBar, which is available for phones as of Android 4.0.
The following picture highlights the hardware button and the resulting menu as popup.
Download Google Chrome Searching is fast and easy with Google's web browser. www.Google.com/Chrome
Hire Android Developers Hire us for unmatched cost & time to market. Free Quote within a day. www.evontech.com
Android eBird App Enter your Sightings in the Field The Power of eBird in your Pocket! market.android.com
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 29 of 54
One of the reasons why the ActionBar is superior to the OptionsMenu, if that it is clearly visible, while
the OptionsMenu is only shown on request and the user may not recognize that options are available.
12.3. Creating the menu
The OptionsMenu and the ActionBar is filled by the onCreateOptionsMenu() method of your
Activity.
In the onCreateOptionsMenu() method you can create the menu entries. You can add menu entries
via code or via the inflation of an existing XML resources.
The MenuInflator class allows to inflate menu entries defined in XML to the menu. MenuInflator
can get accessed via the getMenuInflator() method in your Activity.
The onCreateOptionsMenu() method is only called once. If you want to influence the menu later you
have to use the onPrepareOptionsMenu() method. onPrepareOptionsMenu() is not called for
entries in the ActionBar for these entries you have to use the invalidateOptionsMenu() method.
12.4. Reacting to menu entry selection
If a menu entry is selected then then onOptionsItemSelected() method is called. As parameter you
receive the menu entry which was selected so that you can react differently to different menu entries.
12.5. Using the home icon
The ActionBar also shows an icon of your application. You can also add an action to this icon. If you
select this icon the onOptionsItemSelected() method will be called with the value
android.R.id.home. The recommendation is to return to the main Activity in your program.
// lf home 1con 1s c11cked efun fo ma1n Acf1v1fy
case case case case ando1d.k.1d.home:
lnfenf 1nfenf = neW neW neW neW lnfenf{fh1s fh1s fh1s fh1s, Ovev1eWAcf1v1fy.c1ass c1ass c1ass c1ass}
1nfenf.addI1ags{lnfenf.ILAGAC1lvl1YCLLAk1OP}
sfafAcf1v1fy{1nfenf}
beak beak beak beak
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 30 of 54
It is also possible to add tabs to the ActionBar which can be used for navigation. Typically Fragments
are used for this purpose. We demonstrate this in the Fragments chapter.
12.7. Custom Views in the ActionBar
You can also add a custom View to the ActionBar. The following code snippet demonstrates that.
0Ove1de
pub11c pub11c pub11c pub11c vo1d vo1d vo1d vo1d onCeafe{8und1e savedlnsfance5fafe} {
supe supe supe supe.onCeafe{savedlnsfance5fafe}
sefConfenfv1eW{k.1ayouf.ma1n}
Acf1on8a acf1on8a = gefAcf1on8a{}
// add fhe cusfom v1eW fo fhe acf1on ba
acf1on8a.sefCusfomv1eW{k.1ayouf.acf1onbav1eW}
acf1on8a.sefD1sp1ayOpf1ons{Acf1on8a.Dl5PLAY5hOWCu51OM
| Acf1on8a.Dl5PLAY5hOWhOML}
}
12.8. Contextual action mode
A contextual action mode activates a temporary ActionBar that overlays the application ActionBar for
the duration of a particular sub-task.
The contextual action mode is typically activated by selecting an item or by long clicking on it.
To implemented this, call the startActionMode() method on a View or on your Activity. This
method gets an ActionMode.Callback object which is responsible for the lifecycle of the contextual
ActionBar.
12.9. Context menus
You can also assign a context menu to a View. A context menu is also activated if the user "long
presses" the view.
If possible the contextual action mode should be preferred over a context menu.
A context menu for a view is registered via the registerForContextMenu(view) method. The
onCreateContextMenu() method is called every time a context menu is activated as the context menu
is discarded after its usage. The Android platform may also add options to your View, e.g. EditText
provides context options to select text, etc.
13. Tutorial: ActionBar
13.1. Project
This chapter will demonstrate how to create items in the ActionBar and react to the selection of the
user.
Create a project called "de.vogella.android.socialapp" with the Activity called "OverviewActivity".
13.2. Add a menu XML resource
Select your project, right click on it and select New Other Android Android XML File to create a
new XML resource.
Select the option "Menu", enter as File "mainmenu.xml" and press the button "Finish".
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 31 of 54
This will create a new file "mainmenu.xml" in the folder "res/menu" of your project. Open this file and
select the "Layout" tab of the Android editor.
Press Add and select "Item". Maintain a entry similar to the following screenshot. Via the "ifRoom"
attribute you define that the menu entry is displayed in the ActionBar if there is sufficient space
available.
Add a similar entry to the menu with the ID set to "@+id/menuitem2", the Title set to "Test". Also set the
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 32 of 54
The resulting XML will look like the following.
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<menu xm1ns:ando1d="hffp://schemas.ando1d.com/apk/es/ando1d" >
<1fem
ando1d:1d="0+1d/menu1fem1"
ando1d:shoWAsAcf1on="1fkoom"
ando1d:f1f1e="Pefs">
</1fem>
<1fem
ando1d:1d="0+1d/menu1fem2"
ando1d:shoWAsAcf1on="1fkoom"
ando1d:f1f1e="1esf">
</1fem>
</menu>
Change your Activity class "OverviewActivity" to the following.
package package package package de.voge11a.ando1d.soc1a1app
1mpof 1mpof 1mpof 1mpof ando1d.app.Acf1v1fy
1mpof 1mpof 1mpof 1mpof ando1d.os.8und1e
1mpof 1mpof 1mpof 1mpof ando1d.v1eW.Menu
1mpof 1mpof 1mpof 1mpof ando1d.v1eW.Menulnf1afe
1mpof 1mpof 1mpof 1mpof ando1d.v1eW.Menulfem
1mpof 1mpof 1mpof 1mpof ando1d.W1dgef.1oasf
pub11c pub11c pub11c pub11c c1ass c1ass c1ass c1ass Ovev1eWAcf1v1fy exfends exfends exfends exfends Acf1v1fy {
0Ove1de
pub11c pub11c pub11c pub11c vo1d vo1d vo1d vo1d onCeafe{8und1e savedlnsfance5fafe} {
supe supe supe supe.onCeafe{savedlnsfance5fafe}
sefConfenfv1eW{k.1ayouf.ma1n}
}
0Ove1de
pub11c pub11c pub11c pub11c boo1ean boo1ean boo1ean boo1ean onCeafeOpf1onsMenu{Menu menu} {
Menulnf1afe 1nf1afe = gefMenulnf1afe{}
1nf1afe.1nf1afe{k.menu.ma1nmenu, menu}
efun efun efun efun fue
}
0Ove1de
pub11c pub11c pub11c pub11c boo1ean boo1ean boo1ean boo1ean onOpf1onslfem5e1ecfed{Menulfem 1fem} {
sW1fch sW1fch sW1fch sW1fch {1fem.geflfemld{}} {
case case case case k.1d.menu1fem1:
1oasf.make1exf{fh1s fh1s fh1s fh1s, "Menu lfem 1 se1ecfed", 1oasf.LLNG1h5hOk1}
.shoW{}
beak beak beak beak
case case case case k.1d.menu1fem2:
1oasf.make1exf{fh1s fh1s fh1s fh1s, "Menu 1fem 2 se1ecfed", 1oasf.LLNG1h5hOk1}
.shoW{}
beak beak beak beak
defau1f defau1f defau1f defau1f:
beak beak beak beak
}
efun efun efun efun fue
}
}
Run your application. As there is enough space in the ActionBar otherwise you may see the Overflow
menu or you have to use the "Option" menu button on your phone. If you select one item, you should
see a small info message.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 33 of 54
14. Tutorial: Using the contextual action mode
Add a EditText element your your "main.xml" layout file.
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<L1neaLayouf xm1ns:ando1d="hffp://schemas.ando1d.com/apk/es/ando1d"
ando1d:1ayoufW1dfh="f111paenf"
ando1d:1ayoufhe1ghf="f111paenf"
ando1d:o1enfaf1on="vef1ca1" >
<Ld1f1exf
ando1d:1d="0+1d/myv1eW"
ando1d:1ayoufW1dfh="mafchpaenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:ems="10" >
<equesfIocus />
</Ld1f1exf>
</L1neaLayouf>
Create a new menu XML resource with the file name "contextual.xml"
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<menu xm1ns:ando1d="hffp://schemas.ando1d.com/apk/es/ando1d" >
<1fem
ando1d:1d="0+1d/foasf"
ando1d:f1f1e="1oasf">
</1fem>
</menu>
Change your Activity to the following.
package package package package de.voge11a.ando1d.soc1a1app
1mpof 1mpof 1mpof 1mpof ando1d.app.Acf1v1fy
1mpof 1mpof 1mpof 1mpof ando1d.os.8und1e
1mpof 1mpof 1mpof 1mpof ando1d.v1eW.Menu
1mpof 1mpof 1mpof 1mpof ando1d.v1eW.Menulnf1afe
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 34 of 54
pub11c pub11c pub11c pub11c c1ass c1ass c1ass c1ass Ovev1eWAcf1v1fy exfends exfends exfends exfends Acf1v1fy {
pofecfed pofecfed pofecfed pofecfed Obecf mAcf1onMode
0Ove1de
pub11c pub11c pub11c pub11c vo1d vo1d vo1d vo1d onCeafe{8und1e savedlnsfance5fafe} {
supe supe supe supe.onCeafe{savedlnsfance5fafe}
sefConfenfv1eW{k.1ayouf.ma1n}
// Def1ne fhe confexfua1 acf1on mode
v1eW v1eW = f1ndv1eW8yld{k.1d.myv1eW}
v1eW.sefOnLongC11ckL1sfene{neW neW neW neW v1eW.OnLongC11ckL1sfene{} {
// Ca11ed When fhe use 1ong-c11cks on somev1eW
pub11c pub11c pub11c pub11c boo1ean boo1ean boo1ean boo1ean onLongC11ck{v1eW v1eW} {
1f 1f 1f 1f {mAcf1onMode != nu11} {
efun efun efun efun fa1se
}
// 5faf fhe CA8 us1ng fhe Acf1onMode.Ca11back def1ned above
mAcf1onMode = Ovev1eWAcf1v1fy.fh1s fh1s fh1s fh1s
.sfafAcf1onMode{mAcf1onModeCa11back}
v1eW.sef5e1ecfed{fue}
efun efun efun efun fue
}
}}
}
0Ove1de
pub11c pub11c pub11c pub11c boo1ean boo1ean boo1ean boo1ean onCeafeOpf1onsMenu{Menu menu} {
Menulnf1afe 1nf1afe = gefMenulnf1afe{}
1nf1afe.1nf1afe{k.menu.ma1nmenu, menu}
efun efun efun efun fue
}
0Ove1de
pub11c pub11c pub11c pub11c boo1ean boo1ean boo1ean boo1ean onOpf1onslfem5e1ecfed{Menulfem 1fem} {
1oasf.make1exf{fh1s fh1s fh1s fh1s, "Jusf a fesf", 1oasf.LLNG1h5hOk1}.shoW{}
efun efun efun efun fue
}
p1vafe p1vafe p1vafe p1vafe Acf1onMode.Ca11back mAcf1onModeCa11back = neW neW neW neW Acf1onMode.Ca11back{} {
// Ca11ed When fhe acf1on mode 1s ceafed sfafAcf1onMode{} Was ca11ed
pub11c pub11c pub11c pub11c boo1ean boo1ean boo1ean boo1ean onCeafeAcf1onMode{Acf1onMode mode, Menu menu} {
// lnf1afe a menu esouce pov1d1ng confexf menu 1fems
Menulnf1afe 1nf1afe = mode.gefMenulnf1afe{}
// Assumes fhaf you have "confexua1.xm1" menu esouces
1nf1afe.1nf1afe{k.menu.confexfua1, menu}
efun efun efun efun fue
}
// Ca11ed each f1me fhe acf1on mode 1s shoWn. A1Ways ca11ed affe
// onCeafeAcf1onMode, buf
// may be ca11ed mu1f1p1e f1mes 1f fhe mode 1s 1nva11dafed.
pub11c pub11c pub11c pub11c boo1ean boo1ean boo1ean boo1ean onPepaeAcf1onMode{Acf1onMode mode, Menu menu} {
efun efun efun efun fa1se // kefun fa1se 1f nofh1ng 1s done
}
// Ca11ed When fhe use se1ecfs a confexfua1 menu 1fem
pub11c pub11c pub11c pub11c boo1ean boo1ean boo1ean boo1ean onAcf1onlfemC11cked{Acf1onMode mode, Menulfem 1fem} {
sW1fch sW1fch sW1fch sW1fch {1fem.geflfemld{}} {
case case case case k.1d.foasf:
1oasf.make1exf{Ovev1eWAcf1v1fy.fh1s fh1s fh1s fh1s, "5e1ecfed menu",
1oasf.LLNG1hLONG}.shoW{}
mode.f1n1sh{} // Acf1on p1cked, so c1ose fhe CA8
efun efun efun efun fue
defau1f defau1f defau1f defau1f:
efun efun efun efun fa1se
}
}
// Ca11ed When fhe use ex1fs fhe acf1on mode
pub11c pub11c pub11c pub11c vo1d vo1d vo1d vo1d onDesfoyAcf1onMode{Acf1onMode mode} {
mAcf1onMode = nu11
}
}
}
If you run this example and long press the EditText widget, your contextual ActionBar is displayed.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 35 of 54
15. Layout Manager and ViewGroups
15.1. Available Layout Manager
A layout manager is a subclass of ViewGroup and is responsible for the layout of itself and its child
Views. Android supports different default layout managers.
As of Android 4.0 the most relevant layout manager are LinearLayout, FrameLayout,
RelativeLayout and GridLayout.
All layouts allow the developer to define attributes. Children can also define attributes which may be
evaluated by their parent layout.
AbsoluteLayoutLayout is deprecated and TableLayout can be implemented more effectively via
GridLayout
15.2. LinearLayout
LinearLayout puts all its child elements into a single column or row depending on the
android:orientation attribute. Possible values for this attribute are horizontal and vertical,
horizontal is the default value.
LinearLayout can be nested to achieve more complex layouts.
15.3. RelativeLayout
RelativeLayout allow to position the widget relative to each other. This allows for complex layouts.
A simple usage for RelativeLayout is if you want to center a single component. Just add one
component to the RelativeLayout and set the android:layout_centerInParent attribute to true.
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<ke1af1veLayouf xm1ns:ando1d="hffp://schemas.ando1d.com/apk/es/ando1d"
ando1d:1ayoufW1dfh="mafchpaenf"
ando1d:1ayoufhe1ghf="mafchpaenf"
ando1d:o1enfaf1on="vef1ca1" >
<Pogess8a
ando1d:1d="0+1d/pogess8a1"
sfy1e="?ando1d:aff/pogess8a5fy1eLage"
ando1d:1ayoufW1dfh="Wapconfenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:1ayoufcenfelnPaenf="fue"
/>
</ke1af1veLayouf>
15.4. Gridlayout
GridLayout was introduced with Android 4.0. This layout allows you to organize a view into a Grid.
GridLayout separates its drawing area into: rows, columns, and cells.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 36 of 54
should be placed and how many columns and rows it should use. If not specified GridLayout uses
defaults, e.g. one column, one row and the position of a View depends on the order of the declaration of
the Views.
15.5. ScrollView
ScrollViews can be used to contain one view that might be to big to fit on one screen. If the view is to big
the ScrollView will display a scroll bar to scroll the context. Of course this view can be a layout which can
then contain other elements.
16. Tutorial: ScrollView
Create an android project "de.vogella.android.scrollview" with the activity "ScrollView". Create the
following layout and class.
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<5co11v1eW xm1ns:ando1d="hffp://schemas.ando1d.com/apk/es/ando1d"
ando1d:1ayoufW1dfh="f111paenf"
ando1d:1ayoufhe1ghf="f111paenf"
ando1d:f111v1eWpof="fue"
ando1d:o1enfaf1on="vef1ca1" >
<L1neaLayouf
ando1d:1d="0+1d/L1neaLayouf01"
ando1d:1ayoufW1dfh="f111paenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:o1enfaf1on="vef1ca1" >
<1exfv1eW
ando1d:1d="0+1d/1exfv1eW01"
ando1d:1ayoufW1dfh="Wapconfenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:padd1ngLeff="d1p"
ando1d:padd1ngk1ghf="d1p"
ando1d:padd1ng1op="d1p"
ando1d:fexf="1h1s 1s a heade"
ando1d:fexfAppeaance="?ando1d:aff/fexfAppeaanceLage" >
</1exfv1eW>
<1exfv1eW
ando1d:1d="0+1d/1exfv1eW02"
ando1d:1ayoufW1dfh="Wapconfenf"
ando1d:1ayoufhe1ghf="f111paenf"
ando1d:1ayoufWe1ghf="1.0"
ando1d:fexf="0+1d/1exfv1eW02" >
</1exfv1eW>
<L1neaLayouf
ando1d:1d="0+1d/L1neaLayouf02"
ando1d:1ayoufW1dfh="Wapconfenf"
ando1d:1ayoufhe1ghf="Wapconfenf" >
<8uffon
ando1d:1d="0+1d/8uffon01"
ando1d:1ayoufW1dfh="Wapconfenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:1ayoufWe1ghf="1.0"
ando1d:fexf="5ubm1f" >
</8uffon>
<8uffon
ando1d:1d="0+1d/8uffon02"
ando1d:1ayoufW1dfh="Wapconfenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:1ayoufWe1ghf="1.0"
ando1d:fexf="Cance1" >
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 37 of 54
</L1neaLayouf>
</5co11v1eW>
package package package package de.voge11a.ando1d.sco11v1eW
1mpof 1mpof 1mpof 1mpof ando1d.app.Acf1v1fy
1mpof 1mpof 1mpof 1mpof ando1d.os.8und1e
1mpof 1mpof 1mpof 1mpof ando1d.v1eW.v1eW
1mpof 1mpof 1mpof 1mpof ando1d.W1dgef.1exfv1eW
pub11c pub11c pub11c pub11c c1ass c1ass c1ass c1ass 5co11v1eW exfends exfends exfends exfends Acf1v1fy {

/"" Ca11ed When fhe acf1v1fy 1s f1sf ceafed. "/
0Ove1de
pub11c pub11c pub11c pub11c vo1d vo1d vo1d vo1d onCeafe{8und1e savedlnsfance5fafe} {
supe supe supe supe.onCeafe{savedlnsfance5fafe}
sefConfenfv1eW{k.1ayouf.ma1n}
1exfv1eW v1eW = {1exfv1eW} f1ndv1eW8yld{k.1d.1exfv1eW02}
5f1ng s=""
fo fo fo fo {1nf 1nf 1nf 1nf 1=0 1 < 100 1++} {
s += "voge11a.com "
}
v1eW.sef1exf{s}
}
}
The attribute "android:fillViewport="true"" ensures that the scrollview is set to the full screen even if the
elements are smaller then one screen and the "layout_weight" tell the android system that these
elements should be extended.
17. Drawables
17.1. Drawables
A instance of a Drawable resource is a general concept for a graphic which can be drawn. The simplest
case is a bitmap file, which would be represented in Android via a BitmapDrawable class. Bitmaps are
typically stored in one of the "res/drawable" folders. The Android project creation wizard creates several
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 38 of 54
devices. If you only provide
In additional to graphical files, Android supports XML drawables and 9-patch graphics. XML drawables
are used to describe shapes (color, border, gradient), State and Transitions and more.
9-patch graphics are used to define which part of a graphic should be increased if the View which uses
this graphic is larger then the graphic.
17.2. Shape Drawables
Shape Drawables are XML files which allow to define a geometric object with colors, borders and
gradients which can get assigned to Views. The advantage of using XML Shape Drawables is that they
automatically adjust to the correct size.
The following listing shows an example of a Shape Drawable.
<?xm1 ves1on="1.0" encod1ng="u1I-"?>
<shape
xm1ns:ando1d="hffp://schemas.ando1d.com/apk/es/ando1d"
ando1d:shape="ecfang1e">
<sfoke
ando1d:W1dfh="2dp"
ando1d:co1o="#IIIIIIII" />
<gad1enf
ando1d:endCo1o="#DD888888"
ando1d:sfafCo1o="#DD777777"
ando1d:ang1e="90" />
<cones
ando1d:boffomk1ghfkad1us="7dp"
ando1d:boffomLeffkad1us="7dp"
ando1d:fopLeffkad1us="7dp"
ando1d:fopk1ghfkad1us="7dp" />
</shape>
You could for example assign that drawable to the background property of your layout.
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<L1neaLayouf xm1ns:ando1d="hffp://schemas.ando1d.com/apk/es/ando1d"
ando1d:1ayoufW1dfh="f111paenf"
ando1d:1ayoufhe1ghf="f111paenf"
ando1d:backgound="0daWab1e/myshape"
ando1d:o1enfaf1on="vef1ca1" >
<Ld1f1exf
ando1d:1d="0+1d/ed1f1exf1"
ando1d:1ayoufW1dfh="mafchpaenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
>
</Ld1f1exf>
<kad1oGoup
ando1d:1d="0+1d/ad1oGoup1"
ando1d:1ayoufW1dfh="mafchpaenf"
ando1d:1ayoufhe1ghf="Wapconfenf" >
<kad1o8uffon
ando1d:1d="0+1d/ad1o0"
ando1d:1ayoufW1dfh="Wapconfenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:checked="fue"
ando1d:fexf="0sf1ng/ce1s1us" >
</kad1o8uffon>
<kad1o8uffon
ando1d:1d="0+1d/ad1o1"
ando1d:1ayoufW1dfh="Wapconfenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:fexf="0sf1ng/fahenhe1f" >
</kad1o8uffon>
</kad1oGoup>
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 39 of 54
ando1d:1ayoufW1dfh="Wapconfenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:fexf="0sf1ng/ca1c"
ando1d:onC11ck="myC11ckhand1e">
</8uffon>
</L1neaLayouf>
17.3. State Drawables
State drawables allow to define states. For each state a different drawable can get assigned to the View.
For example the following defines different drawables for a button depending on its state.
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<se1ecfo xm1ns:ando1d="hffp://schemas.ando1d.com/apk/es/ando1d">
<1fem ando1d:daWab1e="0daWab1e/buffonpessed"
ando1d:sfafepessed="fue" />
<1fem ando1d:daWab1e="0daWab1e/buffonchecked"
ando1d:sfafechecked="fue" />
<1fem ando1d:daWab1e="0daWab1e/buffondefau1f" />
</se1ecfo>
17.4. Transition Drawables
Transition Drawables allow to define transitions which can be triggered in the coding.
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<fans1f1on xm1ns:ando1d="hffp://schemas.ando1d.com/apk/es/ando1d">
<1fem ando1d:daWab1e="0daWab1e/f1sf1mage" />
<1fem ando1d:daWab1e="0daWab1e/second1mage" />
</fans1f1on>
f1na1 f1na1 f1na1 f1na1 lmagev1eW 1mage = {lmagev1eW} f1ndv1eW8yld{k.1d.1mage}
f1na1 f1na1 f1na1 f1na1 1ogg1e8uffon buffon = {1ogg1e8uffon} f1ndv1eW8yld{k.1d.buffon}
buffon.sefOnC11ckL1sfene{neW neW neW neW OnC11ckL1sfene{} {
0Ove1de
pub11c pub11c pub11c pub11c vo1d vo1d vo1d vo1d onC11ck{f1na1 f1na1 f1na1 f1na1 v1eW v} {
1ans1f1onDaWab1e daWab1e = {1ans1f1onDaWab1e} 1mage.gefDaWab1e{}
1f 1f 1f 1f {buffon.1sChecked{}} {
daWab1e.sfaf1ans1f1on{500}
} e1se e1se e1se e1se {
daWab1e.evese1ans1f1on{500}
}
}
}}
17.5. 9 Patch Drawables
9 Patch drawables are drawables which have a one pixel additional border. On the top and left you
define the area which should grow if the drawable is to small for the View.
On the right and bottom side you define where a text should be placed if this Drawable is used on a
View which can write text on it, e.g. a Button.
The ADT supplies the "draw9patch" program in the "android-sdk/tools" installation folder, which makes it
easy to create 9 patch drawables.
18. Styles and Themes
18.1. Styles
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 40 of 54
assigned to Views.
You can define styles in XML and assign them to these elements. This way you only have to set common
attributes once and can later change the look in one central place.
To define a style, save an XML file in the /res/values" directory of your project. The root node of the XML
file must be <resources> .
The following "styles.xml" XML file would be created in the "/res/xml" folder.
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<esouces>
<sfy1e name="fexf">
<1fem name="ando1d:padd1ng">4d1p</1fem>
<1fem name="ando1d:fexfAppeaance">?ando1d:aff/fexfAppeaanceLage</1fem>
<1fem name="ando1d:fexfCo1o">#000000</1fem>
</sfy1e>
<sfy1e name="1ayouf">
<1fem name="ando1d:backgound">#C0C0C0</1fem>
</sfy1e>
</esouces>
You assign the style attribute to your elements, for example to the text elements via style=@style/text.
18.2. Attributes
You can also refer to individual attributes of Android styles. For example the following will define button
with the Android 4.0 button style.
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<L1neaLayouf xm1ns:ando1d="hffp://schemas.ando1d.com/apk/es/ando1d"
ando1d:1ayoufW1dfh="f111paenf"
ando1d:1ayoufhe1ghf="f111paenf"
ando1d:o1enfaf1on="vef1ca1" >
<L1neaLayouf
sfy1e="?ando1d:aff/buffon8a5fy1e"
ando1d:1ayoufW1dfh="mafchpaenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:o1enfaf1on="ho1zonfa1" >
<8uffon
ando1d:1d="0+1d/8uffon01"
sfy1e="?ando1d:aff/buffon8a8uffon5fy1e"
ando1d:1ayoufW1dfh="0dp"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:1ayoufWe1ghf="1"
ando1d:fexf="5hoW" />
<8uffon
ando1d:1d="0+1d/8uffon02"
sfy1e="?ando1d:aff/buffon8a8uffon5fy1e"
ando1d:1ayoufW1dfh="0dp"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:1ayoufWe1ghf="1"
ando1d:fexf="Change" />
</L1neaLayouf>
<Ld1f1exf
ando1d:1d="0+1d/myv1eW"
ando1d:1ayoufW1dfh="mafchpaenf"
ando1d:1ayoufhe1ghf="Wapconfenf"
ando1d:ems="10" >
<equesfIocus />
</Ld1f1exf>
</L1neaLayouf>
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 41 of 54
18.3. Themes
A theme is a style applied to an entire Activity or application, rather than an individual View (as in the
example above).
The next example show how to define your own Theme while extending a platform theme.
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<esouces>
<sfy1e name="My1heme" paenf="ando1d:1heme.L1ghf">
<1fem name="ando1d:W1ndoWNo11f1e">fue</1fem>
<1fem name="ando1d:W1ndoW8ackgound">0co1o/fans1ucenfed</1fem>
<1fem name="ando1d:11sfv1eW5fy1e">0sfy1e/MyL1sfv1eW</1fem>
</sfy1e>
<sfy1e name="MyL1sfv1eW" paenf="0ando1d:sfy1e/W1dgef.L1sfv1eW">
<1fem name="ando1d:11sf5e1ecfo">0daWab1e/1cmenuhome</1fem>
</sfy1e>
</esouces>
18.4. Android Design Page
How to design your Android application is described on the Android Design Pages, which can be found
here http://developer.android.com/design/index.html .
This page also contains several downloadable resources, e.g. an Icon set for the ActionBar.
19. Supporting different screen sizes
19.1. Using device independent pixel
Android devices are different in terms of resolution and in terms of density. Therefore it is recommended
never to use fixed sized dimensions.
The unit of measurement which should be used is "dp" (which is the same as "dip" but shorter). dp
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 42 of 54
of the first Android device (G1). This size is also known as mdpi (medium dots per inch).
If you specify the size in "dp", Android will automatically scale it, depending on the device. On a mdpi
device "dp" will be equal to pixel but it will be smaller on a ldpi (approx. 120dip) and larger on a hdip
(approx. 240dpi) device, so that it always occupies the same physical space.
You can use "dp" in your resources, e.g. layout files. The Android SDK expects that you specify
everything in pixels. You can therefore use the following formulator to calculate the right amount of pixels
for a dimension specified in dp.
pub11c pub11c pub11c pub11c 1nf 1nf 1nf 1nf convef1oDp{1nf 1nf 1nf 1nf 1npuf} {
// Gef fhe sceens dens1fy sca1e
f1na1 f1na1 f1na1 f1na1 f1oaf f1oaf f1oaf f1oaf sca1e = gefkesouces{}.gefD1sp1ayMef1cs{}.dens1fy
// Convef fhe dps fo p1xe1s, based on dens1fy sca1e
efun efun efun efun {1nf 1nf 1nf 1nf} {1npuf " sca1e + 0.5f}
}
19.2. Using resource qualifiers
Android also allows to use resource qualifiers to specify that certain resources should only be used for
certain resolutions or configurations. For example your can provide a special "main.xml" layout file for a
device in landscape mode, if you place such a file in the "res/layout-land" folder.
The different resource qualifiers are described on the following webpage: Providing Resources
20. Fragments
20.1. Fragments Overview
Fragment components allow you to organize your application code so that it is easier to support
different sized devices.
Fragments are components with their own lifecycle and their own user interface. They can be defined
via layout files or via coding.
Fragments always run in the context of an Activity. If an Activity is stopped its Fragments will
also be stopped; if an Activity is destroyed its Fragments will also get destroyed.
If a Fragment component is defined in an XML layout file, the android:name attribute points to the
Fragments class.
The base class for Fragments is android.app.Fragment. For special purposes you can also use
more special classes, like ListFragment or DialogFragment.
The onCreateView() method is called by Android once the Fragment should create its user interface.
Here you can inflate an layout. The onStart() method is called once the Fragment gets
visible.
Fragments can be dynamically added and removed from an Activity via Fragment transactions. This
will add the action to the history stack of the Activity, i.e. this will allow to revert the Fragment
changes in the Activity via the back button.
20.2. When to use Fragments
Fragments make it easy to re-use components in different layouts, e.g. you can build single-pane
layouts for handsets (phones) and multi-pane layouts for tablets.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 43 of 54
landscape and portrait orientation. But as tablets offer significantly more space you typically include
more views into the layout and Fragments makes that easier.
The typical example is a list of items in an activity. On a tablet you see the details immediately on the
same screen on the right hand side if you click on item. On a handset you jump to a new detail screen.
The following discussion will assume that you have two Fragments (main and detail) but you can also
have more. We will also have one main activity and one detailed activity. On a tablet the main activity
contains both Fragments in its layout, on a handheld it only contains the main fragment.
To check for an fragment you can use the FragmentManager.
Defa11Iagmenf fagmenf = {Defa11Iagmenf} gefIagmenfManage{}.
f1ndIagmenf8yld{k.1d.defa11fag}
1f 1f 1f 1f {fagmenf==nu11 || ! fagmenf.1slnLayouf{}} {
// sfaf neW Acf1v1fy
}
e1se e1se e1se e1se {
fagmenf.updafe{...}
}
To create different layouts with Fragments you can:
Use one activity, which displays two Fragments for tablets and only one on handsets devices. In
this case you would switch the Fragments in the activity whenever necessary. This requires that
the fragment is not declared in the layout file as such Fragments cannot be removed during
runtime. It also requires an update of the action bar if the action bar status depends on the
fragment.
Use separate activities to host each fragment on a handset. For example, when the tablet UI uses
two Fragments in an activity, use the same activity for handsets, but supply an alternative layout
that includes just one fragment. When you need to switch Fragments, start another activity that
hosts the other fragment.
The second approach is the most flexible and in general preferable way of using Fragments. In this
case the main activity checks if the detail fragment is available in the layout. If the detailed fragment is
there, the main activity tells the fragment that is should update itself. If the detail fragment is not
available the main activity starts the detailed activity.
It is good practice that Fragments do not manipulate each other. For this purpose a Fragment typically
implements an interface to get new data from its host Activity.
21. Fragments Tutorial
21.1. Overview
The following tutorial demonstrates how to use Fragments. The entry Activity (called MainActivity
of our application ) will use different layouts for portrait and for landscape mode.
In portrait mode MainActivity will show one Fragment with a list of names. If the user touches an item
in the list, a second Activity called DetailActivity will start and show the selected text.
In landscape mode MainActivity will show two Fragments. The first is again the Fragments which
shows the list of names. The second Fragment shows the text of the current selected item. This is
similar to the portrait mode, but the whole information will be shown on one screen.
21.2. Create project
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 44 of 54
21.3. Create layouts for portrait mode
Create or change the following layout files in the "res/layout/" folder.
First create the following file called "details.xml". This layout will be used by the DetailFragment.
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<L1neaLayouf xm1ns:ando1d="hffp://schemas.ando1d.com/apk/es/ando1d"
ando1d:1ayoufW1dfh="mafchpaenf"
ando1d:1ayoufhe1ghf="mafchpaenf"
ando1d:o1enfaf1on="vef1ca1" >
<1exfv1eW
ando1d:1d="0+1d/defa11s1exf"
ando1d:1ayoufW1dfh="Wapconfenf"
ando1d:1ayoufhe1ghf="mafchpaenf"
ando1d:1ayoufgav1fy="cenfeho1zonfa1|cenfevef1ca1"
ando1d:1ayoufmag1n1op="20d1p"
ando1d:fexf="Lage 1exf"
ando1d:fexfAppeaance="?ando1d:aff/fexfAppeaanceLage"
ando1d:fexf51ze="30d1p" />
</L1neaLayouf>
Change the existing "main.xml" file. This layout will be used by MainActivity in landscape mode and
shows two Fragments.
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<L1neaLayouf xm1ns:ando1d="hffp://schemas.ando1d.com/apk/es/ando1d"
ando1d:1ayoufW1dfh="f111paenf"
ando1d:1ayoufhe1ghf="f111paenf"
ando1d:o1enfaf1on="ho1zonfa1" >
<fagmenf
ando1d:1d="0+1d/11sfIagmenf"
ando1d:1ayoufW1dfh="150d1p"
ando1d:1ayoufhe1ghf="mafchpaenf"
ando1d:1ayoufmag1n1op="?ando1d:aff/acf1on8a51ze"
c1ass c1ass c1ass c1ass="de.voge11a.ando1d.fagmenfs.L1sfIagmenf" ></fagmenf>
<fagmenf
ando1d:1d="0+1d/defa11Iagmenf"
ando1d:1ayoufW1dfh="mafchpaenf"
ando1d:1ayoufhe1ghf="mafchpaenf"
c1ass c1ass c1ass c1ass="de.voge11a.ando1d.fagmenfs.Defa11Iagmenf" >
<!-- Pev1eW: 1ayouf=01ayouf/defa11s -->
</fagmenf>
</L1neaLayouf>
21.4. Create Fragment classes
Create now the Fragment classes. Create the ListFragment class.
package package package package de.voge11a.ando1d.fagmenfs
1mpof 1mpof 1mpof 1mpof ando1d.confenf.lnfenf
1mpof 1mpof 1mpof 1mpof ando1d.os.8und1e
1mpof 1mpof 1mpof 1mpof ando1d.v1eW.v1eW
1mpof 1mpof 1mpof 1mpof ando1d.W1dgef.AayAdapfe
1mpof 1mpof 1mpof 1mpof ando1d.W1dgef.L1sfv1eW
pub11c pub11c pub11c pub11c c1ass c1ass c1ass c1ass L1sfIagmenf exfends exfends exfends exfends ando1d.app.L1sfIagmenf {
0Ove1de
pub11c pub11c pub11c pub11c vo1d vo1d vo1d vo1d onCeafe{8und1e savedlnsfance5fafe} {
supe supe supe supe.onCeafe{savedlnsfance5fafe}
}
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 45 of 54
supe supe supe supe.onAcf1v1fyCeafed{savedlnsfance5fafe}
5f1ng|] va1ues = neW neW neW neW 5f1ng|] { "Ando1d", "1Phone", "W1ndoWsMob11e",
"81ackbey", "WebO5", "ubunfu", "W1ndoWs7", "Max O5 X",
"L1nux", "O5/2" }
AayAdapfe<5f1ng> adapfe = neW neW neW neW AayAdapfe<5f1ng>{gefAcf1v1fy{},
ando1d.k.1ayouf.s1mp1e11sf1fem1, va1ues}
sefL1sfAdapfe{adapfe}
}
0Ove1de
pub11c pub11c pub11c pub11c vo1d vo1d vo1d vo1d onL1sflfemC11ck{L1sfv1eW 1, v1eW v, 1nf 1nf 1nf 1nf pos1f1on, 1ong 1ong 1ong 1ong 1d} {
5f1ng 1fem = {5f1ng} gefL1sfAdapfe{}.geflfem{pos1f1on}
Defa11Iagmenf fagmenf = {Defa11Iagmenf} gefIagmenfManage{}
.f1ndIagmenf8yld{k.1d.defa11Iagmenf}
1f 1f 1f 1f {fagmenf != nu11 && fagmenf.1slnLayouf{}} {
fagmenf.sef1exf{1fem}
} e1se e1se e1se e1se {
lnfenf 1nfenf = neW neW neW neW lnfenf{gefAcf1v1fy{}.gefApp11caf1onConfexf{},
Defa11Acf1v1fy.c1ass c1ass c1ass c1ass}
1nfenf.pufLxfa{"va1ue", 1fem}
sfafAcf1v1fy{1nfenf}
}
}
}
Create the DetailFragment class.
package package package package de.voge11a.ando1d.fagmenfs
1mpof 1mpof 1mpof 1mpof ando1d.app.Iagmenf
1mpof 1mpof 1mpof 1mpof ando1d.os.8und1e
1mpof 1mpof 1mpof 1mpof ando1d.uf11.Log
1mpof 1mpof 1mpof 1mpof ando1d.v1eW.Layouflnf1afe
1mpof 1mpof 1mpof 1mpof ando1d.v1eW.v1eW
1mpof 1mpof 1mpof 1mpof ando1d.v1eW.v1eWGoup
1mpof 1mpof 1mpof 1mpof ando1d.W1dgef.1exfv1eW
pub11c pub11c pub11c pub11c c1ass c1ass c1ass c1ass Defa11Iagmenf exfends exfends exfends exfends Iagmenf {
0Ove1de
pub11c pub11c pub11c pub11c vo1d vo1d vo1d vo1d onCeafe{8und1e savedlnsfance5fafe} {
supe supe supe supe.onCeafe{savedlnsfance5fafe}
Log.e{"1esf", "he11o"}
}
0Ove1de
pub11c pub11c pub11c pub11c vo1d vo1d vo1d vo1d onAcf1v1fyCeafed{8und1e savedlnsfance5fafe} {
supe supe supe supe.onAcf1v1fyCeafed{savedlnsfance5fafe}
}
0Ove1de
pub11c pub11c pub11c pub11c v1eW onCeafev1eW{Layouflnf1afe 1nf1afe, v1eWGoup confa1ne,
8und1e savedlnsfance5fafe} {
v1eW v1eW = 1nf1afe.1nf1afe{k.1ayouf.defa11s, confa1ne, fa1se}
efun efun efun efun v1eW
}
pub11c pub11c pub11c pub11c vo1d vo1d vo1d vo1d sef1exf{5f1ng 1fem} {
1exfv1eW v1eW = {1exfv1eW} gefv1eW{}.f1ndv1eW8yld{k.1d.defa11s1exf}
v1eW.sef1exf{1fem}
}
}
21.5. Create layouts for landscape mode
We want that Android uses a different main.xml file in portrait model then in landscape mode.
For this reason create the "res/layout-port" folder.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 46 of 54
have a "main.xml" file in "layout-port", Android would check the "layout" folder.
Therefore create the following "main.xml" layout file in "res/layout-port".
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<L1neaLayouf xm1ns:ando1d="hffp://schemas.ando1d.com/apk/es/ando1d"
ando1d:1ayoufW1dfh="f111paenf"
ando1d:1ayoufhe1ghf="f111paenf"
ando1d:o1enfaf1on="ho1zonfa1" >
<fagmenf
ando1d:1d="0+1d/11sfIagmenf"
ando1d:1ayoufW1dfh="mafchpaenf"
ando1d:1ayoufhe1ghf="mafchpaenf"
ando1d:1ayoufmag1n1op="?ando1d:aff/acf1on8a51ze"
c1ass c1ass c1ass c1ass="de.voge11a.ando1d.fagmenfs.L1sfIagmenf" />
</L1neaLayouf>
Also create the "details_activity_layout.xml" layout file. This layout will be used in the DetailActivity
which is only used in portrait mode. Please note that we could have create this file also in the "layout"
folder, but as it is only used in portrait mode it is best practise to place it into this folder.
<?xm1 ves1on="1.0" encod1ng="uff-"?>
<L1neaLayouf xm1ns:ando1d="hffp://schemas.ando1d.com/apk/es/ando1d"
ando1d:1ayoufW1dfh="mafchpaenf"
ando1d:1ayoufhe1ghf="mafchpaenf"
ando1d:o1enfaf1on="vef1ca1" >
<fagmenf
ando1d:1d="0+1d/defa11Iagmenf"
ando1d:1ayoufW1dfh="mafchpaenf"
ando1d:1ayoufhe1ghf="mafchpaenf"
c1ass c1ass c1ass c1ass="de.voge11a.ando1d.fagmenfs.Defa11Iagmenf" />
</L1neaLayouf>
21.6. Activities
Create a new Activity called DetailActivity with the following class.
package package package package de.voge11a.ando1d.fagmenfs
1mpof 1mpof 1mpof 1mpof ando1d.app.Acf1v1fy
1mpof 1mpof 1mpof 1mpof ando1d.confenf.es.Conf1guaf1on
1mpof 1mpof 1mpof 1mpof ando1d.os.8und1e
1mpof 1mpof 1mpof 1mpof ando1d.W1dgef.1exfv1eW
pub11c pub11c pub11c pub11c c1ass c1ass c1ass c1ass Defa11Acf1v1fy exfends exfends exfends exfends Acf1v1fy {
0Ove1de
pofecfed pofecfed pofecfed pofecfed vo1d vo1d vo1d vo1d onCeafe{8und1e savedlnsfance5fafe} {
supe supe supe supe.onCeafe{savedlnsfance5fafe}
// Need fo check 1f Acf1v1fy has been sW1fched fo 1andscape mode
// lf yes, f1n1shed and go back fo fhe sfaf Acf1v1fy
1f 1f 1f 1f {gefkesouces{}.gefConf1guaf1on{}.o1enfaf1on == Conf1guaf1on.OklLN1A1lONLAND5CAP
f1n1sh{}
efun efun efun efun
}
sefConfenfv1eW{k.1ayouf.defa11sacf1v1fy1ayouf}
8und1e exfas = geflnfenf{}.gefLxfas{}
1f 1f 1f 1f {exfas != nu11} {
5f1ng s = exfas.gef5f1ng{"va1ue"}
1exfv1eW v1eW = {1exfv1eW} f1ndv1eW8yld{k.1d.defa11s1exf}
v1eW.sef1exf{s}
}
}
}
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 47 of 54
package package package package de.voge11a.ando1d.fagmenfs
1mpof 1mpof 1mpof 1mpof ando1d.app.Acf1v1fy
1mpof 1mpof 1mpof 1mpof ando1d.os.8und1e
pub11c pub11c pub11c pub11c c1ass c1ass c1ass c1ass Ma1nAcf1v1fy exfends exfends exfends exfends Acf1v1fy {
/"" Ca11ed When fhe acf1v1fy 1s f1sf ceafed. "/
0Ove1de
pub11c pub11c pub11c pub11c vo1d vo1d vo1d vo1d onCeafe{8und1e savedlnsfance5fafe} {
supe supe supe supe.onCeafe{savedlnsfance5fafe}
sefConfenfv1eW{k.1ayouf.ma1n}
}
}
21.7. Run
Run your example. If you run the application in portrait mode you should see only one Fragment. Use
Ctrl+F11 to switch the orientation. In horizontal mode you should see two Fragments. If you select an
item in portrait mode a new Activity should get started with the selected item. In horizontal mode your
second Fragment should display the select item.
22. ActionBar navigation with Fragments
Fragments can also be used in combination with the ActionBar for navigation. For this your main
Activity needs to implement a TabListener which is responsible for moving between the tabs.
The ActionBar allows to add tabs to it via the newTab() method. The following code shows such an
Activity. It uses two Fragments, called DetailFragment and ImageFragment. At this point you
should be able to create these two Fragments yourself.
package package package package de.voge11a.ando1d.fagmenf
1mpof 1mpof 1mpof 1mpof ando1d.app.Acf1on8a
1mpof 1mpof 1mpof 1mpof ando1d.app.Acf1on8a.1ab
1mpof 1mpof 1mpof 1mpof ando1d.app.Acf1on8a.1abL1sfene
1mpof 1mpof 1mpof 1mpof ando1d.app.Acf1v1fy
1mpof 1mpof 1mpof 1mpof ando1d.app.Iagmenf
1mpof 1mpof 1mpof 1mpof ando1d.app.Iagmenf1ansacf1on
1mpof 1mpof 1mpof 1mpof ando1d.os.8und1e
pub11c pub11c pub11c pub11c c1ass c1ass c1ass c1ass Ma1nAcf1v1fy exfends exfends exfends exfends Acf1v1fy {
0Ove1de
pub11c pub11c pub11c pub11c vo1d vo1d vo1d vo1d onCeafe{8und1e savedlnsfance5fafe} {
supe supe supe supe.onCeafe{savedlnsfance5fafe}
sefConfenfv1eW{k.1ayouf.ma1n}
// sefup acf1on ba fo fabs
Acf1on8a acf1on8a = gefAcf1on8a{}
acf1on8a.sefNav1gaf1onMode{Acf1on8a.NAvlGA1lONMODL1A85}
acf1on8a.sefD1sp1ay5hoW11f1eLnab1ed{fa1se}
1ab fab = acf1on8a
.neW1ab{}
.sef1exf{"I1sf fab"}
.sef1abL1sfene{
neW neW neW neW My1abL1sfene<Defa11Iagmenf>{fh1s fh1s fh1s fh1s, "af1sf",
Defa11Iagmenf.c1ass c1ass c1ass c1ass}}
acf1on8a.add1ab{fab}
fab = acf1on8a
.neW1ab{}
.sef1exf{"5econd 1ab"}
.sef1abL1sfene{
neW neW neW neW My1abL1sfene<lmageIagmenf>{fh1s fh1s fh1s fh1s, "a1bum",
lmageIagmenf.c1ass c1ass c1ass c1ass}}
acf1on8a.add1ab{fab}
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 48 of 54
pub11c pub11c pub11c pub11c sfaf1c sfaf1c sfaf1c sfaf1c c1ass c1ass c1ass c1ass My1abL1sfene<1 exfends exfends exfends exfends Iagmenf> 1mp1emenfs 1mp1emenfs 1mp1emenfs 1mp1emenfs
1abL1sfene {
p1vafe p1vafe p1vafe p1vafe Iagmenf mIagmenf
p1vafe p1vafe p1vafe p1vafe f1na1 f1na1 f1na1 f1na1 Acf1v1fy mAcf1v1fy
p1vafe p1vafe p1vafe p1vafe f1na1 f1na1 f1na1 f1na1 5f1ng m1ag
p1vafe p1vafe p1vafe p1vafe f1na1 f1na1 f1na1 f1na1 C1ass<1> mC1ass
/""
" Consfucfo used each f1me a neW fab 1s ceafed.
"
" 0paam acf1v1fy
" 1he hosf Acf1v1fy, used fo 1nsfanf1afe fhe fagmenf
" 0paam fag
" 1he 1denf1f1e fag fo fhe fagmenf
" 0paam c1z
" 1he fagmenfs C1ass, used fo 1nsfanf1afe fhe fagmenf
"/
pub11c pub11c pub11c pub11c My1abL1sfene{Acf1v1fy acf1v1fy, 5f1ng fag, C1ass<1> c1z} {
mAcf1v1fy = acf1v1fy
m1ag = fag
mC1ass = c1z
}
/" 1he fo11oW1ng ae each of fhe Acf1on8a.1abL1sfene ca11backs "/
pub11c pub11c pub11c pub11c vo1d vo1d vo1d vo1d on1ab5e1ecfed{1ab fab, Iagmenf1ansacf1on ff} {
// Check 1f fhe fagmenf 1s a1eady 1n1f1a11zed
1f 1f 1f 1f {mIagmenf == nu11} {
// lf nof, 1nsfanf1afe and add 1f fo fhe acf1v1fy
mIagmenf = Iagmenf.1nsfanf1afe{mAcf1v1fy, mC1ass.gefName{}}
ff.add{ando1d.k.1d.confenf, mIagmenf, m1ag}
} e1se e1se e1se e1se {
// lf 1f ex1sfs, s1mp1y affach 1f 1n ode fo shoW 1f
ff.sefCusfomAn1maf1ons{ando1d.k.an1mafo.fade1n,
k.an1mafo.an1maf1onfesf}
ff.affach{mIagmenf}
}
}
pub11c pub11c pub11c pub11c vo1d vo1d vo1d vo1d on1abunse1ecfed{1ab fab, Iagmenf1ansacf1on ff} {
1f 1f 1f 1f {mIagmenf != nu11} {
ff.sefCusfomAn1maf1ons{ando1d.k.an1mafo.fade1n,
k.an1mafo.fesf}
ff.defach{mIagmenf}
}
}
pub11c pub11c pub11c pub11c vo1d vo1d vo1d vo1d on1abkese1ecfed{1ab fab, Iagmenf1ansacf1on ff} {
}
}
}
23. DDMS perspective and important views
23.1. DDMS - Dalvik Debug Monitor Server
Eclipse provides a perspective for interacting with your Android (virtual) device and your Android
application program. Select Window Open Perspective Other DDMS to open this perspective. It
includes several Views which can also be used independently and allows for example the application to
place calls and send SMS to the device. It also allows the application to set the current geo position and
allows you to perform a performance trace of your application.
23.2. LogCat View
You can see the log (including System.out.print() statements) via the LogCat view.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 49 of 54
23.3. File explorer
The file explorer allows to see the files on the Android simulator.
24. Shell
24.1. Android Debugging Bridge - 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 following command "adb shell".
adb she11
You can also copy a file from and to your device via the following commands.
// Assume fhe gesfue f11e ex1sfs on you Ando1d dev1ce
adb pu11 /sdcad/gesfues ~/fesf
// NoW copy 1f back
adb push ~/fesf/gesfue /sdcad/gesfues2
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 50 of 54
system, e.g. ls, rm, mkdir, etc. The application data is stored in the directory "/data
/data/package_of_your_app".
If you have several devices running you can issue commands to one individual device.
# L1sfs a11 dev1ces
adb dev1ces
#kesu1f
L1sf of dev1ces affached
emu1afo-5554 affached
emu1afo-5555 affached
# lssue a command fo a spec1f1c dev1ce
adb -s emu1afo-5554 she11
24.2. Uninstall an application via adb
You can uninstall an android application via the shell. Switch the data/app directory (cd /data/app) and
simply delete your android application.
You can also uninstall an app via adb with the package name.
adb un1nsfa11 <packagename>
24.3. Emulator Console via telnet
Alternatively to adb you can also use telnet to connect to the device. This allows you to simulate certain
things, e.g. incoming call, change the network "stability", set your current geocodes, etc. Use "telnet
localhost 5554" to connect to your simulated device. To exit the console session, use the command "quit"
or "exit".
For example to change the power settings of your phone, to receive an sms and to get an incoming call
make the following.
# connecfs fo dev1ce
fe1nef 1oca1hosf 5554
# sef fhe poWe 1eve1
poWe sfafus fu11
poWe sfafus chag1ng
# make a ca11 fo fhe dev1ce
gsm ca11 012041293123
# send a sms fo fhe dev1ce
sms send 12345 W111 be home soon
# sef fhe geo 1ocaf1on
geo f1x 4 51
For more information on the emulator console please see Emulator Console manual
25. Deployment
25.1. Overview
In general there are you restrictions how to deploy an Android application to your device. You can use
USB, email yourself the application or use one of the many Android markets to install the application.
The following describes the most common ones.
25.2. Deployment via Eclipse
Turn on "USB Debugging" on your device in the settings. Select in the settings of your device
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 51 of 54
You may also need to install the a driver for your mobile phone. Linux and Mac OS usually work out of
the box while an Windows OS typically requires
For details please see Developing on a Device . Please note that the Android version you are
developing for must be the installed version on your phone.
To select your phone, select the "Run Configurations", select "Manual" selection and select your device.
Android application must be signed before they can get installed on an Android device. During
development Eclipse signs your application automatically with a debug key.
If you want to install your application without the Eclipse IDE you can right click on it and select Android
Tools Export Signed Application Package.
This wizard allows to use an existing key or to create a new one.
Please note that you need to use the same signature key in Google Play (Google Market) to update your
application. If you loose the key you will NOT be able to update your application ever again.
Make sure to backup your key.
25.4. Via external sources
Android allow to install applications also directly. Just click on a link which points to an .apk file, e.g. in
an email attachment or on a webpage. Android will prompt you if you want to install this application.
This requires a setting on the Android device which allows the installation of non-market application.
Typically this setting can be found under the "Security" settings.
25.5. Google Play (Market)
Google Play requires a one time fee, currently 25 Dollar. After that the developer can directly upload his
application and the required icons, under Google Play Publishing .
Google performs some automatic scanning of applications, but no approval process is in place. All
application, which do not contain malware, will be published. Usually a few minutes after upload, the
application is available.
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 52 of 54
26. Thank you
Please help me to support this article:

27. Questions and Discussion
Before posting questions, please see the vogella FAQ. If you have questions or find an error in this
article please use the www.vogella.com Google Group. I have created a short list how to create good
questions which might also help you.
28. Links and Literature
28.1. Source Code
Source Code of Examples
28.2. Android Resources
Android 2 (German Book)
Android ListView and ListActivity
Android SQlite Database
Android Widgets
Android Live Wallpaper
Android Services
Android Location API and Google Maps
Android Intents
Android and Networking
Android Homepage
Android Developer Homepage
Android Issues / Bugs
Android Google Groups
Android Live Folder
28.3. vogella Resources
Eclipse RCP Training (German) Eclipse RCP Training with Lars Vogel
Android Tutorial Introduction to Android Programming
GWT Tutorial Program in Java and compile to JavaScript and HTML
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 53 of 54
JUnit Tutorial Test your application
Git Tutorial Put everything you have under distributed version control system
Home
Tutorials Trainings Books Connect
http://www.vogella.com/articles/Android/article.html
GhnapikA 54 of 54