Android workshop handout

Uploaded on


More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Android Developers – WinnersGoogle’s Android mobile phone software platform is the next big opportunity for application software developers.With the existing mobile development build on proprietary operating systems that restrict the development anddeployment of third-party applications, Android offers an open alternative. Android developers are free to writeapplications that enjoys full advantage of powerful mobile hardware and distribute them in an open market.Built on an open source framework, and featuring powerful SDK libraries and a much open philosophy, Android hasopened mobile phone development to thousands of developers who haven’t had access to tools for building mobileapplications. Experienced mobile developers can now expand into the Android platform, leveraging the uniquefeatures to enhance existing products or create innovative ones.Using the Android Market for distribution, developers can take advantage of an open marketplace, with no reviewprocess, for distributing free and paid apps to all compatible Android devices.What are the problems that Android improves?Fragmentation: Till now there are a lot of phones available which can run applications, but each brand has adifferent application environment. This is mostly true in case of Linux based phones, where each handset vendor hashad to assemble plenty of pieces of third-party software to create a viable mobile phone platform. Java wassupposed to help this situation. Unfortunately, almost every handset that supports J2ME also supports vendor-proprietary extensions that limit the portability of applications.Proprietary Software Stacks: Most existing smartphones such as Nokia’s Series 60 with Symbian OS, or Microsoft’sWindows Mobile use proprietary & relatively closed software stacks. Modifications to these stacks (example - addinga device driver) have to be done either by the stack owner or by the handset manufacturer. The stacks are not opensource, so changing anything in the stack is difficult. Most Linux-based phones to date have an open source kernel,but keep other details of the software stack (application framework, multimedia framework, applications)proprietary.Closed Networks: Mobile operators often lock the handsets so applications cannot be added. The operators claimthis is needed to preserve the integrity of their mobile networks. It also suits the operator’s business model. Androidincludes an open catalog of applications, Android Market, which users can download over the air to their Androidphones. It also allows direct loading of applications via USB connection.Android gives developers a way to develop unique, creative applications and get those applications in the hands ofcustomers.Questions? Visit for 24x7 Free Support Page 1
  • 2. Component LifeCycle Component Services Component – Content Providers Producing an Android AppQuestions? Visit for 24x7 Free Support Page 2
  • 3. Resources Types:  Animation Resources (/anim or /drawable)  Color State List Resource (/color)  Drawable Resources(/drawable)  Layout Resource(/layout)  Menu Resource(/menu)  String Resources(/values)  Style Resource(/values)  More Resource Types(/values)Questions? Visit for 24x7 Free Support Page 3
  • 4. Screen CharacteristicScreen characteristic Qualifier Description small Resources for small size screens. normal Resources for normal size screens. (This is the baseline size.)Size large Resources for large size screens. xlarge Resources for extra large size screens. ldpi Resources for low-density (ldpi) screens (~120dpi). Resources for medium-density (mdpi) screens (~160dpi). mdpi (This is the baseline density.) hdpi Resources for high-density (hdpi) screens (~240dpi). xhdpi Resources for extra high-density (xhdpi) screens (~320dpi).Density Resources for all densities. These are density-independent nodpi resources. The system does not scale resources tagged with this qualifier, regardless of the current screens density. Resources for screens somewhere between mdpi and hdpi; tvdpi approximately 213dpi. This is not considered a "primary" density group. Resources for screens in the landscape orientation (wide land aspect ratio).Orientation Resources for screens in the portrait orientation (tall aspect port ratio). Resources for screens that have a significantly taller or wider long aspect ratio (when in portrait or landscape orientation, respectively) than the baseline screen configuration.Aspect ratio Resources for use screens that have an aspect ratio that is notlong similar to the baseline screen configuration.Questions? Visit for 24x7 Free Support Page 4
  • 5. Components of an Android ApplicationActivities: Activities are pieces of executable code that come and go in time, instantiated by either the user or theoperating system and running as long as they are needed. They can interact with the user and request data orservices from other activities or services via queries or Intents.Services: They are executable pieces of code that usually run in the background from the time of their instantiationuntil the mobile handset is shut down. They generally don’t expose a user interface.Broadcast and Intent Receivers: These respond to requests for service from another application. A BroadcastReceiver Broadcast Receiver responds to a system-wide announcement of an event. These announcements can comefrom Android itself (e.g., battery low) or from any program running on the system. An Activity or Service providesother applications with access to its functionality by executing an Intent Receiver, a small piece of executable codethat responds to requests for data or services from other activities.Content Providers: These are created to share data with other activities or services. A content provider uses astandard interface in the form of a URI to fulfil requests for data from other applications that may not even knowwhich content provider they are using.Android Activity LifecycleThe mechanisms are evident in the Android Activity Lifecycle, which defines the states or events that an activity goesthrough from the time it is created until it finishes running.To understand these concepts, you should Google about:onCreate, onStart, onResume, onPause, onStop and onDestroyIt is important to take advantage of these methods to provide the best user experience possible.Questions? Visit for 24x7 Free Support Page 5
  • 6. Hello, World As a developer, you know that the first impression of a development framework is how easy it is to write "Hello, World." Well, on Android, its pretty easy. Its particularly easy if youre using Eclipse as your IDE, because weve provided a great plugin that handles your project creation and management to greatly speed-up your development cycles. Install a Platform To run the Hello World application, you need to install at least one Android platform in your SDK environment. If you have not already performed this step, you need to do it now. To install a platform in Eclipse:1. In the Android SDK and AVD Manager, choose Available Packages in the left panel.2. Click the repository site checkbox to display the components available for installation.3. Select at least one platform to install, and click Install Selected. If you arent sure which platform to install, use the latest version. Create an AVD In this tutorial, you will run your application in the Android Emulator. Before you can launch the emulator, you must create an Android Virtual Device (AVD). An AVD defines the system image and device settings used by the emulator. To create an AVD:1. In Eclipse, choose Window > Android SDK and AVD Manager.2. Select VirtualDevices in the left panel.3. Click New. The Create New AVD dialog appears.4. Type the name of the AVD, such as "my_avd".5. Choose a target. The target is the platform (that is, the version of the Android SDK, such as 2.1) you want to run on the emulator. You can ignore the rest of the fields for now.6. Click Create AVD. Create a New Android Project After youve created an AVD, the next step is to start a new Android project in Eclipse.1. From Eclipse, select File > New > Project. If the ADT Plugin for Eclipse has been successfully installed, the resulting dialog should have a folder labeled "Android" which should contain "Android Project". (After you create one or more Android projects, an entry for "Android XML File" will also be available.)2. Select "Android Project" and click Next. Questions? Visit for 24x7 Free Support Page 6
  • 7. 3. Fill in the project details with the following values:o Project name: HelloAndroido Application name: Hello, Androido Package name: com.example.helloandroid (or your own private namespace)o Create Activity: HelloAndroid Click Finish. Here is a description of each field: Project Name This is the Eclipse Project name — the name of the directory that will contain the project files. Application Name This is the human-readable title for your application — the name that will appear on the Android device. Package Name This is the package namespace (following the same rules as for packages in the Java programming language) that you want all your source code to reside under. This also sets the package name under which the stub Activity will be generated. Your package name must be unique across all packages installed on the Android system; for this reason, its important to use a standard domain-style package for your applications. The example above uses the "com.example" namespace, which is a namespace reserved for example documentation — when you develop your own applications, you should use a namespace thats appropriate to your organization or entity. Create Activity This is the name for the class stub that will be generated by the plugin. This will be a subclass of Androids Activity class. An Activity is simply a class that can run and do work. It can create a UI if it Questions? Visit for 24x7 Free Support Page 7
  • 8. chooses, but it doesnt need to. As the checkbox suggests, this is optional, but an Activity is almost always used as the basis for an application.Min SDK Version This value specifies the minimum API Level required by your application.Other fields: The checkbox for "Use default location" allows you to change the location on disk where the projects fileswill be generated and stored. "Build Target" is the platform target that your application will be compiled against (thisshould be selected automatically, based on your Min SDK Version).Notice that the "Build Target" youve selected uses the Android 1.1 platform. This means that your application will becompiled against the Android 1.1 platform library. If you recall, the AVD created above runs on the Android 1.5platform. These dont have to match; Android applications are forward-compatible, so an application built against the1.1 platform library will run normally on the 1.5 platform. The reverse is not true.Your Android project is now ready. It should be visible in the Package Explorer on the left. Openthe file, located inside HelloAndroid > src > com.example.helloandroid). It should look like this: package com.example.helloandroid; import; import android.os.Bundle; public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }Notice that the class is based on the Activity class. An Activity is a single application entity that is used to performactions. An application may have many separate activities, but the user interacts with them one at a time.The onCreate() method will be called by the Android system when your Activity starts — it is where you shouldperform all initialization and UI setup. An activity is not required to have a user interface, but usually will.Now lets modify some code!Construct the UITake a look at the revised code below and then make the same changes to your HelloAndroid class. The bold itemsare lines that have been added. package com.example.helloandroid; import; import android.os.Bundle; import android.widget.TextView; public class HelloAndroid extends Activity {Questions? Visit for 24x7 Free Support Page 8
  • 9. /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello, Android"); setContentView(tv); } } An Android user interface is composed of hierarchies of objects called Views. A View is a drawable object used as an element in your UI layout, such as a button, image, or (in this case) a text label. Each of these objects is a subclass of the View class and the subclass that handles text is TextView. In this change, you create a TextView with the class constructor, which accepts an Android Context instance as its parameter. A Context is a handle to the system; it provides services like resolving resources, obtaining access to databases and preferences, and so on. The Activity class inherits from Context, and because your HelloAndroid class is a subclass of Activity, it is also a Context. So, you can pass this as your Context reference to the TextView. Next, you define the text content with setText(). Finally, you pass the TextView to setContentView() in order to display it as the content for the Activity UI. If your Activity doesnt call this method, then no UI is present and the system will display a blank screen. There it is — "Hello, World" in Android! The next step, of course, is to see it running. Run the Application The Eclipse plugin makes it easy to run your applications:1. Select Run > Run.2. Select "Android Application". The Eclipse plugin automatically creates a new run configuration for your project and then launches the Android Emulator. Depending on your environment, the Android emulator might take several minutes to boot fully, so please be patient. You should now see something like this: The "Hello, Android" you see in the grey bar is actually the application title. The Eclipse plugin creates this automatically (the string is defined in the res/values/strings.xml file and referenced by your AndroidManifest.xml file). The text below the title is the actual text that you have created in the TextView object. That concludes the basic "Hello World" tutorial, but you should continue reading for some more valuable information about developing Android applications. Questions? Visit for 24x7 Free Support Page 9
  • 10. NOTES________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________Questions? Visit for 24x7 Free Support Page 10