• Save
How to create an application in Android 4.x
Upcoming SlideShare
Loading in...5
×
 

How to create an application in Android 4.x

on

  • 468 views

Introduction to the basic aspects of application development in Android. The basic components, e.g., Activity and View, are introduced along with the different configuration files, e.g., ...

Introduction to the basic aspects of application development in Android. The basic components, e.g., Activity and View, are introduced along with the different configuration files, e.g., AndroidManifest.xml.

The source code is available at https://github.com/iliocatallo/takenotes

Statistics

Views

Total Views
468
Views on SlideShare
467
Embed Views
1

Actions

Likes
0
Downloads
0
Comments
0

1 Embed 1

http://www.slideee.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

How to create an application in Android 4.x How to create an application in Android 4.x Presentation Transcript

  • How to create an application in Android 4.x Ilio Catallo, Eleonora Ciceri – Politecnico di Milano ilio.catallo@polimi.it, eleonora.ciceri@polimi.it
  • Outline ¤  Android application structure ¤  Resources ¤  Activity and Views 2
  • Android application structure 3
  • Android application structure ¤  Programming an Android application involves: ¤  Writing the business logic code ¤  Providing the resources required for the user interface ¤  User interface definition (via XML) ¤  Icons ¤  Localized strings ¤  Providing the multimedia content (i.e., assets), which will be used by the application ¤  Video / photo collections 4
  • Android Project File Structure 5 src/ Source code that you write for your app gen/ Source code that is auto-generated by the ADT libs/ Precompiled third-party libraries (JAR archives) that you want to use in your app res/ GUI layouts, icons, menus and so forth assets/ Other media that you want to use in your app (e.g., videos, sounds, large images that are not used directly in the GUI layouts)
  • Application configuration ¤  Each Android application includes a manifest file (AndroidManifest.xml), which describes ¤  each single component ¤  the interaction between different components ¤  Specifically, the manifest defines: ¤  the application structure and metadata ¤  the application components ¤  the application requirements ¤  The manifest is stored in the root of the project hierarchy 6
  • Manifest structure: manifest node 7 uses-sdk application activity service provider receiver intent-filter ¤  <manifest> is the root node of the AndroidManifest.xml file <manifest ! xmlns:android=! “http://schemas.android.com/apk/res/android”! package="it.polimi.mad”! android:versionCode="1"! android:versionName="0.9 Beta" ! android:installLocation="preferExternal”>! ...! </manifest>!
  • Manifest structure: manifest node 8 uses-sdk application activity service provider receiver intent-filter ¤  Attributes ¤  versionCode: an integer representing the version of the application code ¤  versionName: a string representing the release version of the application code, as shown to users ¤  installLocation: the default install location for the application
  • Manifest structure: uses-sdk node 9 uses-sdk application activity service provider receiver intent-filter ¤  <uses-sdk> lets you express an application’s compatibility with one or more versions of the Android platform ¤  This tag specifies the version of the APIs, NOT the SDK <uses-sdk android:minSdkVersion=“6”! android:targetSdkVersion=”15” />!
  • Manifest structure: uses-sdk node 10 uses-sdk application activity service provider receiver intent-filter ¤  Attributes ¤  android:minSdkVersion: an integer designating the minimum API Level required for the application to run ¤  android:targetSdkVersion: an integer designating the API Level the application targets
  • Manifest structure: application node 11 uses-sdk application activity service provider receiver intent-filter ¤  <application> defines the application metadata ¤  Example: icon, title ¤  It acts as a container for activities, services, content providers and broadcast receivers <application android:icon="@drawable/icon” ! android:name=  ”.MyApp" ! android:debuggable="true">! ...! </application>!
  • Manifest structure: application node 12 uses-sdk application activity service provider receiver intent-filter ¤  <application> defines the application metadata ¤  Example: icon, title ¤  It acts as a container for activities, services, content providers and broadcast receivers <application android:icon="@drawable/icon” ! android:name=  ”.MyApp" ! android:debuggable="true">! ...! </application>! . is used as a shorthand for the application’s package name
  • Manifest structure: application node 13 uses-sdk application activity service provider receiver intent-filter ¤  Attributes ¤  android:icon: reference to a resource containing the application icon image ¤  android:name: the fully qualified name for the class inheriting from Application* ¤  android:debuggable: whether or not the application can be debugged * The subclass is optional. In the absence of a subclass, an instance of the base Application class is used
  • Manifest structure: activity node 14 uses-sdk application activity service provider receiver intent-filter ¤  <activity> is required for every Activity within the application <activity android:name=".MyActivity” ! android:label="@string/activity_name"> ! ...! </activity>!
  • Manifest structure: activity node ¤  Attributes ¤  android:name: the name of the class that implements the activity (should be a fully qualified class name) ¤  android:label: a user-readable label for the activity, displayed when the activity is represented to the user (often along with the activity icon) 15 uses-sdk application activity service provider receiver intent-filter
  • Manifest structure: intent-filter node 16 uses-sdk application activity service provider receiver intent-filter ¤  <intent-filter> specifies the types of intents that an activity, service, or broadcast receiver can respond to <intent-filter>! <action android:name="android.! intent.action.MAIN”/>! <category android:name="android.! intent.category.LAUNCHER”/> ! </intent-filter>!
  • Manifest structure: intent-filter node 17 uses-sdk application activity service provider receiver intent-filter ¤  Sub-nodes ¤  action: the name of the action ¤  category: adds a category name to an intent filter. ¤  Some standard actions and categories are defined in the Intent class ¤  ACTION_MAIN: starts up as the initial activity of a task (no data input and no returned output) ¤  CATEGORY_LAUNCHER: the activity must be invoked by the launcher
  • Manifest structure: service node 18 uses-sdk application activity service provider receiver intent-filter ¤  <service> declares a Service class implementing long-running background operations <service android:name=".MyService"> ! ...! </service>!
  • Manifest structure: service node 19 uses-sdk application activity service provider receiver intent-filter ¤  Attributes ¤  android:name: qualified name of the class implementing the service
  • Manifest structure: provider node 20 uses-sdk application activity service provider receiver intent-filter ¤  <provider> declares a content provider component, supplying access to data managed by the application <provider android:name=".MyContentProvider" ! android:authorities=“it.polimi.! mad.contentprovider"/>!
  • Manifest structure: provider node 21 uses-sdk application activity service provider receiver intent-filter ¤  Attributes ¤  android:name: the fully qualified name of the class that implements the content provider ¤  android:authorities: a list of one or more URIs that identify data offered by the content provider ¤  To avoid conflicts with content providers in other apps, the URIs should use a Java-style package naming convention
  • Manifest structure: receiver node 22 uses-sdk application activity service provider receiver intent-filter ¤  <receiver> declares a broadcast receiver as one of the application's components ¤  Broadcast receivers enable applications to receive intents that are broadcast by the system or by other applications <receiver android:name=".MyIntentReceiver"> ! <intent-filter>! <action android:name=”it.polimi.! mad.mybroadcastaction”/> ! </intent-filter>! </receiver>!
  • Manifest structure: receiver node 23 uses-sdk application activity service provider receiver intent-filter ¤  Attributes ¤  android:name: the fully qualified name of the class that implements the broadcast receiver
  • TakeNotesv1: manifest.xml! <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="it.polimi.mad.takenotesv1"
 android:versionCode="1"
 android:versionName="1.0" >
 
 <uses-sdk android:minSdkVersion="8"
 ! android:targetSdkVersion="17" />
 
 <application android:allowBackup="true"
 ! android:icon="@drawable/ic_launcher"
 android:label="@string/app_name" >
 <activity
 android:name="it.polimi.mad.takenotesv1.ToDoListActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>
 
 </manifest>! 24 Android 4.2 (Jelly bean) Android 2.2 (Froyo)
  • TakeNotesv1: manifest.xml! <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="it.polimi.mad.takenotesv1"
 android:versionCode="1"
 android:versionName="1.0" >
 
 <uses-sdk android:minSdkVersion="8"
 ! android:targetSdkVersion="17" />
 
 <application android:allowBackup="true"
 ! android:icon="@drawable/ic_launcher"
 android:label="@string/app_name" >
 <activity
 android:name="it.polimi.mad.takenotesv1.ToDoListActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>
 
 </manifest>! 25 Resource URI (we will see in a while what it means…)
  • TakeNotesv1: manifest.xml! 26 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="it.polimi.mad.takenotesv1"
 android:versionCode="1"
 android:versionName="1.0" >
 
 <uses-sdk android:minSdkVersion="8"
 ! android:targetSdkVersion="17" />
 
 <application android:allowBackup="true"
 ! android:icon="@drawable/ic_launcher"
 android:label="@string/app_name" >
 <activity
 android:name="it.polimi.mad.takenotesv1.ToDoListActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>
 
 </manifest>! One single activity, implemented by this class •  We will see in a while its meaning •  For now, consider what you see in our application as implemented by a single class
  • Resources 27
  • Motivation ¤  It’s always good practice to keep non-code resources, e.g., images and string constants, external to your code ¤  Pros ¤  By externalizing resources, you make them easier to maintain, update and manage ¤  Example: support a new screen size ¤  Since resources are externalized, it is possible to alter the code without modifying the resources (and viceversa) ¤  Android automatically selects the correct resources without you having to write code ¤  Example: select the proper language 28
  • Creating resources ¤  Application resources are stored under the res/ folder: ¤  Grouped by resource type ¤  Stored in subfolders (one for each resource type) ¤  When your application is built, these resources will be compiled and compressed as efficiently as possible and included in your application package 29
  • Creating resources ¤  Android supports the externalization of: ¤  Simple values, such as strings and colors ¤  Complex values ¤  Images (drawables) ¤  Animations ¤  UI elements ¤  Themes ¤  Menus ¤  Layouts 30
  • Creating resources ¤  Android supports the externalization of: ¤  Simple values, such as strings and colors ¤  Complex values ¤  Images (drawables) ¤  Animations ¤  UI elements ¤  Themes ¤  Menus ¤  Layouts 31
  • Simple values: Strings ¤  String resources are specified with the string tag ¤  Font formatting: it is possible to format strings by using HTML tags (e.g., <b> or <i>) ¤  A string array defines an array of strings 32 <string name="stop_message">Stop</string>! <string-array name="string-array”>! <item>Item1</item>! <item>Item2</item>! </string-array>
  • Simple values: color ¤  Use the color tag to define a new color resource ¤  A color is specified using a # symbol followed by the alpha channel ¤  Examples: ¤  #RRGGBB (RGB values) ¤  #AARRGGBB (RGB values + alpha channel) 33 <color name="opaque-blue">#CD43A2</color>
  • Dealing with different screen sizes ¤  Units of measurements: ¤  You must avoid using absolute pixels to define distances or sizes, as different screens have different pixel densities 34 dp! Density-independent pixel. 1 dp is equivalent to one pixel on a 160 dpi screen sp! Scale-independent pixel. An sp is the same base unit as dp, but it is (possibly) scaled by the user’s preferred text size. It should be used when defining text size pt! Point. A point is defined to be 1/72 of an inch px! Pixel. Corresponds to actual pixels on the screen
  • Dealingwith differentscreensizes 35 ¤  Screen dimension ¤  4 inches (diagonally) ¤  Screen width ¤  2.04 inches ¤  Resolution ¤  480(width) x 800(height) ¤  Pixel density ¤  480/2.04 = 235 dpi
  • Dealing with different screen sizes ¤  Android defines and recognizes four screen densities ¤  Low density: ldpi, 120 dpi ¤  Medium density: mdpi, 160 dpi ¤  High density: hdpi, 240 dpi ¤  Extra high density: xhdpi, 320 dpi ¤  In the Android terminology, screen densities are called density buckets 36
  • Drawables ¤  The drawable resources are stored as individual files in the res/drawable/ folder ¤  Bitmap image assets are stored in the appropriate subfolder, i.e., -ldpi, -mdpi, -hdpi, -xhdpi! ¤  The preferred format for a bitmap resource is PNG, but JPG and GIF images are also supported 37
  • Provide alternative bitmaps ¤  You should always provide bitmap resources tailored to each of the available density bucket ¤  To do so, generate the images using the following scales: ¤  Extra-high density (xhdpi): 2.0 ¤  High density (hdpi): 1.5 ¤  Normal density (mdpi): 1.0 ¤  Low density (ldpi): 0.75 ¤  Example: the same image must be generated at 200x200 for xhdpi, 150x150 for hdpi, 100x100 for mdpi, and 75x75 for ldpi 38
  • TakeNotes v1: strings.xml! ¤  Located in res/values/strings.xml! 39 <resources>
 <string name="app_name">TakeNotesV1</string>
 <string name="action_settings">Settings</string>
 </resources>! [...] android:label="@string/app_name" [...]! ¤  If you remember, they were used in manifest.xml:
  • Activities and Views 40
  • Activities and Views ¤  The activity is a single, focused thing that the user can do ¤  Each activity is associated with a window in which to draw the user interface ¤  The view is the basic building block for user interface components ¤  Responsible for drawing and event handling ¤  Examples: button, textbox 41
  • Applicationbuilding blocks:UIcomponents 42 View View The Activity is the task that can be performed by interacting with the user interface
  • Activity lifecycle ¤  The state of each Activity is determined by its position on the Activity stack ¤  The Activity stack is a last-in-first-out collection of all the currently running activities ¤  When a new Activity starts, it becomes active and is moved to the top of the stack ¤  If the user navigates back (using the Back button) or the foreground Activity is closed, the next Activity down on the stack moves up and becomes active 43
  • Activity lifecycle ¤  As Activities are created and destroyed, they transition through four possible states: ¤  Active ¤  Paused ¤  Stopped ¤  Inactive 44 FrontBack
  • Activity lifecycle ¤  Active Activity ¤  Visible, focused, foreground activity that is receiving user input ¤  At the top of the stack ¤  Kept alive at all costs! ¤  If it does not have the resources it needs, other Activities will be killed 45 FrontBack
  • Activity lifecycle ¤  Paused Activity ¤  Visible but not focused ¤  This state is reached if a transparent or non-full- screen Activity is active in front of it ¤  Treated as if it were active, but without user input events 46 FrontBack
  • Activity lifecycle ¤  Stopped Activity ¤  Not visible ¤  It remains in memory, retaining all state information ¤  It is a candidate for termination when the system requires memory elsewhere 47 FrontBack
  • Activity lifecycle ¤  Inactive Activity ¤  Removed from the Activity stack ¤  Needs to be restarted before it can be displayed and used 48 FrontBack
  • ActivityLifecycle 49
  • TakeNotes v1: ToDoListActivity.java! ¤  Created as a default (blank) activity ¤  In Eclipse: File > New > Other… > Android > Android Activity 50 public class ToDoListActivity extends Activity {
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_to_do_list);
 }
 
 }! The file res/layout/activity_to_do_list.xml defines the structure of the activity that is loaded when the activity is created
  • User interface: View and ViewGroup (1) ¤  Each Activity in the app is associated with a user interface (UI) ¤  User interfaces are built using View’s and ViewGroup’s: ¤  A View is a widget that has an appearance on the screen ¤  A ViewGroup is a special View that contains other Views in order to define the layout of the interface ¤  In other words, each ViewGroup is an invisible container that organizes child Views, and Views are any widget that draws some part of the UI 51
  • User interface: View and ViewGroup (2) ¤  The UI for each component of the app is defined using a hierarchy of View and ViewGroup objects 52
  • User interface layout ¤  Each subclass of ViewGroup defines the layout, i.e., the arrangement, of its child views on the screen 53 A LinearLayout organizes its children into a single horizontal or vertical row A RelativeLayout enables you to specify the location of child objects relative to each other or to the parent
  • User interface: View and ViewGroup (3) ¤  Android provides a collection of View and ViewGroup subclasses that cover most common situations: ¤  Views: input controls, e.g., TextView class, Button class, CheckBox class ¤  ViewGroup: layout models, e.g., LinearLayout class, RelativeLayout class ¤  However, if your app has unique need that is not covered by the built-in views, it is possible to create a custom View by extending the View class 54
  • LinearLayout ViewGroup ¤  The LinearLayout arranges views in a single column or a single row ¤  Attributes ¤  layout_width: specifies the width of the ViewGroup ¤  layout_height: specifies the height of the ViewGroup ¤  orientation: vertical or horizontal ¤  Default values for attributes ¤  fill_parent: makes the component expand to match the size of its parent view ¤  wrap_content: the width or height of the view is set to the minimum size necessary to fit the content within that view 55
  • ScrollView ViewGroup ¤  The ScrollView enables users to scroll through a list of views that occupy more space than the physical display ¤  you should place one child in it containing the entire contents to scroll ¤  A child that is often used is LinearLayout, which in turn contains some other views ¤  By doing so, the content of the LinearLayout will be scrollable 56
  • TakeNotes v1: activity_to_do_list.xml! 57 Add a static checkbox to the checkbox list A new string in the resource file specifies the content of the default checkbox: <string name="first_checkbox_text">! Conclude the implementation of TakeNotes! </string>! <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 tools:context=".ToDoListActivity">
 
 <LinearLayout android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical">
 
 <CheckBox android:id="@+id/checkBox1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="32dp"
 android:text="@string/first_checkbox_text"/>
 
 </LinearLayout>
 </ScrollView>!
  • TakeNotes v1: the result 58 Resource @drawable/ic_launcher Resource @string/app_name View Checkbox Resource @string/first_checkbox_text Activity ToDoListActivity.java
  • References 59
  • References ¤  Reto Meier, Professional Android 4 Application development 3rd Ed., Wrox ¤  Android Training, Supporting different screen sizes http://developer.android.com/training/multiscreen/screensizes.html ¤  Android Training, Supporting different densities http://developer.android.com/training/multiscreen/ screendensities.html ¤  Android API Guides, UI Overview http://developer.android.com/guide/topics/ui/overview.html ¤  Android API Guides, Layouts http://developer.android.com/guide/topics/ui/declaring- layout.html 60