Intro to Android Programming

2,623
-1

Published on

An introduction to Android programming, using the hello world example.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,623
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
173
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Intro to Android Programming

  1. 1. Introduction to Android Programming 0 to “Hello World” in 60 minutes or less Silicon Valley China Wireless Group Peter van der Linden [email_address] Android Technology Evangelist Developer Community Technical Services Motorola Mobility
  2. 2. Content <ul><li>Moore’s Law and mobile devices </li></ul><ul><li>Android Software Development Checklist </li></ul><ul><li>Ingredients of an App </li></ul><ul><li>GUI basics </li></ul><ul><li>Getting into code </li></ul><ul><li>Running your code </li></ul><ul><li>Q&A </li></ul>These slides are online at: http://www.slideshare.net account: pvdl01
  3. 3. Moore’s Law and Mobile Devices <ul><li>“ the number of transistors on an IC doubles approx every two years” </li></ul><ul><ul><li>Electronics Magazine, 19 April 1965 </li></ul></ul><ul><li>It’s the law - trend has continued for 45 years, and keeps going </li></ul><ul><li>Trend is predictive of cpu Hz, storage size & speed, power consumption, network speed, etc etc </li></ul><ul><li>Partly self-fulfilling, as it guides hw roadmaps </li></ul>
  4. 4. Smart Phone Family Tree PDA cell phone smart phone tablet netbook laptop Moore’s Law Moore’s law Actual Family Tree
  5. 5. My Phone has 802.11 b/g/n Wifi 1 GHz CPU Moore’s Law This is not a toy. This is equivalent to a Windows XP PC 512 MB RAM bluetooth DVD quality video player Music quality audio USB port
  6. 6. My Droid X Phone ALSO has accelerometer Digital compass Moore’s Law Today’s Windows PCs don’t have these … HD video capture Voice phone with noise-canceling hw 8 MP camera Orientation sensors GPS receiver
  7. 7. Smart Phone Family Tree smart phone laptop Moore’s Law A more insightful way to think of it! = + more hw
  8. 8. Implications of HW capabilities Moore’s Law <ul><li>Expect the same characteristics as desktop apps </li></ul><ul><li>Large and powerful API to choose from </li></ul><ul><li>Development time and cost </li></ul><ul><li>Plus need for great app graphics, icons, UI design </li></ul><ul><li>The app sales channel problem is solved for you </li></ul>
  9. 9. 2. Android Software Checklist <ul><li>Embedded software development expertise </li></ul><ul><li>Java Desktop SE expertise (who has Java cert?) </li></ul><ul><li>Eclipse IDE expertise </li></ul><ul><li>XML expertise </li></ul><ul><li>SQL database programming expertise </li></ul><ul><li>Open GL programming expertise </li></ul>The more of these you have, the easier your learning curve will be
  10. 10. 2. Android Software Checklist <ul><li>Who has programmed in Java? </li></ul><ul><li>Who has used any IDE? </li></ul>That’s enough to get started!
  11. 11. The SDK tool chain 5. Java runtime SE ver 6 4. Eclipse 3.5 3. Eclipse ADT plug-in 2. Android SDK 1. Android Platform(s) Download 1,2,3, from developer.android.com 4 from Eclipse.org 5 from java.com
  12. 12. One stop shopping for Tools <ul><li>http:// developer.motorola.com/docstools/motodevstudio/download </li></ul><ul><li>If you need to install Java runtime, it will tell you </li></ul><ul><li>Versions for Windows, MacOS, Linux, 32 and 64 bit </li></ul><ul><li>IDE for all Android on all platforms from all Manufacturers </li></ul>MOTODEV Studio
  13. 13. Compile time tools Java JRE Eclipse 3.5 Eclipse ADT plug-in Android SDK <ul><li>Your code lives in Eclipse </li></ul><ul><li>Your code is compiled by the SDK </li></ul><ul><li>The Eclipse ADT plugin helps your code work with the Android SDK </li></ul><ul><li>The Android SDK contains the Android libraries and tools </li></ul>
  14. 14. Runtime Development Tools Eclipse 3.5 Android SDK Android Platform <ul><li>You tell Eclipse to run your code </li></ul><ul><li>Eclipse tells the emulator in the Android SDK </li></ul><ul><li>The emulator contains the Android platform libraries. </li></ul><ul><li>Emulator is an app on your PC that emulates Android hw </li></ul><ul><li>Or use a real Android device over USB connection </li></ul>Emu
  15. 15. 3. Ingredients of an App <ul><li>Source code for every Android app has: </li></ul>Java “ glue” XML, icons Media, data files, etc AndroidManifest.xml describes the app overall, features used, version, etc
  16. 16. <ul><li>Create app in Eclipse, by File > New > Android … </li></ul>Ingredients of an App, II
  17. 17. <ul><li>These folders and files are created, to start you off </li></ul>Ingredients of an App, III Java “ glue” code XML, icons, etc Manifest.xml
  18. 18. Ingredients of an App, IV <ul><li>When you create a new app in Eclipse, you get: </li></ul><ul><li>A complete set of files and folders, that form “Hello World”, </li></ul><ul><li>Including </li></ul><ul><li>A Java source file </li></ul><ul><li>An XML file (defines layout of components on the screen) </li></ul><ul><li>Some generated “glue” code to make XML names visible to Java </li></ul><ul><li>Some icons </li></ul><ul><li>An overall AndroidManifest.xml file describing the app to the Android system </li></ul>
  19. 19. 4. GUI Basics <ul><li>Modern GUIs have common elements: </li></ul><ul><li>Components (buttons, dialogs, progress bars, canvas, etc) </li></ul><ul><li>Components are grouped and laid out by a layout manager </li></ul><ul><li>LinearLayout </li></ul><ul><li>Button </li></ul><ul><li>TextView </li></ul>Press You can have text in a TextView Another Button
  20. 20. 4. GUI Basics <ul><li>Modern GUIs have common elements: </li></ul><ul><li>Components generate events from user input. </li></ul><ul><li>You write & register callback routines to handle events </li></ul><ul><li>Events are delivered to you on a UI thread (avoids the need for locking code in the GUI) </li></ul><ul><li>Your code is called when needed  </li></ul>public class MyActivity extends Activity implements OnClickListener {           Button b= (Button) findViewById(R.id.pressme);         b.setOnClickListener(this);     . . .     // Implement the OnClickListener callback     public void onClick(View v) {       // actions when the button is clicked     }  
  21. 21. GUI Basics <ul><li>Android follows the conventional model in most respects </li></ul><ul><li>One exception: components and layout is in XML, not code </li></ul><ul><li>Here’s what the XML looks like: </li></ul><ul><li><Button> some attributes=“values” </Button> </li></ul><ul><li>Usually shortened to this: </li></ul><ul><li><Button some attributes=“values” /> </li></ul><ul><li>So your source is going to include a bunch of XML files </li></ul>
  22. 22. GUI Basics <ul><li>Your source includes a bunch of XML files </li></ul>
  23. 23. Getting into Code <ul><li>Every screen in your app is an Activity object that you code </li></ul><ul><li>Your code extends android.app.Activity </li></ul><ul><li>Every Activity has </li></ul><ul><li>An XML file defining the GUI objects on its screen </li></ul><ul><li>A Java file defining how user interacts with those objects </li></ul><ul><li>And entry points that are called for you at the right moments when something happens in the GUI (screen created, button pushed, etc) </li></ul>
  24. 24. Getting into Code, XML 1 <ul><li>An XML file defining the GUI objects on its screen </li></ul><ul><li>General appearance: </li></ul><?xml version= &quot;1.0&quot; encoding=&quot;utf-8&quot;?> <LinearLayout … some attribute=“value” pairs … > <TextView … some attribute=“value” pairs … /> </LinearLayout>
  25. 25. Getting into Code, XML 2 <ul><li>An XML file defining the GUI objects on its screen/Activity </li></ul><ul><li>Full details </li></ul><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> <LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:orientation=&quot;vertical&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent” > <TextView android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;@string/hello” /> </LinearLayout>
  26. 26. Getting into Code, XML 3 <ul><li>Every View must have a value for android:layout_width and _height </li></ul><ul><li>Tells layout manager how much room you want </li></ul><ul><li>for the WIDTH and the HEIGHT of the component </li></ul><ul><li>&quot;fill_parent” magic word that says “greedy – as much as possible” </li></ul><ul><li>&quot;wrap_content” magic word that says “frugal – as little as possible” </li></ul><ul><li><TextView </li></ul><ul><li>android:layout_width= &quot;fill_parent&quot; </li></ul><ul><li>android:layout_height= &quot;wrap_content&quot; </li></ul><ul><li>android:text= &quot;@string/hello” /> </li></ul>
  27. 27. Getting into Code, XML 3 <ul><li>You can look at the screen this XML defines, in MOTODEV Studio </li></ul>
  28. 28. Getting into Code, II <ul><li>The phases of an Activity (the code for each screen goes thru these steps). </li></ul><ul><li>Create </li></ul><ul><li>(runs in the event loop until something stops it) </li></ul><ul><li>Stop </li></ul><ul><li>There are finer divisions within the phases, but this is the template </li></ul><ul><li>You create methods to override the methods where you want to do something. You want to override onCreate, and set up your GUI </li></ul>
  29. 29. Getting into Code, Activity 1 <ul><li>Java file defining interaction with GUI objects on screen </li></ul><ul><li>General appearance: </li></ul>public class MainActivity extends android.app.Activity { /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // glue code! } }
  30. 30. Getting into Code, Activity 2 <ul><li>Java file defining interaction with GUI objects on screen </li></ul><ul><li>Full details </li></ul>package com.motorola.hello2; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
  31. 31. Getting into Code, Glue <ul><li>The glue code is generated for you automatically </li></ul><ul><li>Makes XML names in res folder, appear in Java namespace! </li></ul><ul><li>This shows the convention for getting hold of layout files </li></ul><ul><li>under yourProject/res/layout </li></ul>setContentView( R.layout.main ); // says “use layout in file res/layout/main.xml”
  32. 32. Getting into Code, Glue 2 The glue code is generated for you Makes XML names in res folder, visible in Java namespace! Create an ID name for an XML element with this attribute In Java, get hold of that XML-declared TextView by: In XML, get hold of that XML-declared TextView by: TextView tv = (TextView) findViewById( R.id.myTV ); <TextView android:id=&quot;@+id/myTV &quot; <Button android:layout_below= ”@id/myTV &quot;
  33. 33. Getting into Manifest <ul><li>AndroidManifest.xml – describes your app to the system </li></ul>< ?xml version= &quot;1.0&quot; encoding=&quot;utf-8&quot;?> <manifest xmlns:android= &quot;http://schemas.android.com/apk/res/android&quot; package= &quot;com.motorola.hello2&quot; android:versionCode= &quot;1&quot; android:versionName= &quot;1.0&quot;> <application android:icon= &quot;@drawable/icon&quot; android:label=&quot;@string/app_name&quot;> <activity android:name= &quot;.MainActivity&quot; android:label= &quot;@string/app_name&quot;> <intent-filter> <action android:name= &quot;android.intent.action.MAIN&quot; /> <category android:name= &quot;android.intent.category.LAUNCHER&quot; /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion= &quot;8&quot; /> </manifest>
  34. 34. Running your code <ul><li>Test your code on a sw emulator, or on a handset via USB </li></ul><ul><li>The sw emulator runs on your development system. It’s an app that emulates the hardware, and you load your test app onto it. </li></ul><ul><li>Click “Run > Run as > Android Application” </li></ul><ul><li>Live Demonstration of this </li></ul>
  35. 35. Follow-up <ul><li>developer.android.com </li></ul><ul><li>– Reference documentation </li></ul><ul><li>– Technical notes </li></ul><ul><li>• developer.motorola.com </li></ul><ul><li>– Technical Library </li></ul><ul><li>– Product Specs </li></ul><ul><li>– MOTODEV Studio </li></ul><ul><li>– Discussion Boards </li></ul><ul><li>– Blog posts </li></ul><ul><li>– and more … </li></ul>
  36. 36. Q & A Where to find more information: http://developer.motorola.com One stop shopping, with links to many other useful places One stop Tools and SDK download Tools that work for all Android devices from all manufacturers.
  37. 37. Apache 2 license Copyright (c) 2007-2010 by the Android Open Source Project Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  38. 38. <ul><li>Copyright (c) 2010, Motorola, Inc. All  rights reserved. - Redistribution and use in source and binary forms, with or without  modification, are permitted provided that the following conditions are  met: -  Redistributions of source code must retain the above copyright notice, this  list of conditions and the following disclaimer. -  Redistributions in binary form must reproduce the above copyright notice, this  list of conditions and the following disclaimer in the documentation and/or  other materials provided with the distribution. -  Neither the name of the Motorola, Inc. nor the names of its contributors may  be used to endorse or promote products derived from this software without  specific prior written permission. </li></ul>BSD license
  39. 39. <ul><li>THIS  SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND ANY  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH  DAMAGE. </li></ul>BSD license
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×