Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Lab

Android Lab

Android Lab
http://leonguyen.com

LeoNguyen.com

July 11, 2013
Tweet

More Decks by LeoNguyen.com

Other Decks in Programming

Transcript

  1. Outline - Lab 01: An introduction to Android - Lab

    02.1: Linear Layout - Lab 02.2: Layout - Lab 02.3: List View Layout - Lab 02.4: Menu - Lab 02.5: Dialog - Lab 02.6: Widget - Lab 03.1: Shares Preferences - Lab 03.2: SQLite - Lab 03.3: Internal and External - Lab 04.1: Intent - Lab 04.2: Content Provider - Lab 04.3: Broadcast Receiver
  2. References - Developer Android (developer.android.com) - Mobile Tus (mobile.tutsplus.com) -

    Androidhive (www.androidhive.info) - Code Of A Ninja (www.codeofaninja.com) - Vogella (www.vogella.com/android.html) - Java Papers (www.javapapers.com) - Android-er (android-er.blogspot.com) - Mkyong (http://bit.ly/12XCJOW) - Java2s (www.java2s.com) - Dan Clarke (dan.clarke.name) - My Sample Code (www.mysamplecode.com) - Android Design Patterns (www.androiddesignpatterns.com) - Wagied Davids (w2davids.wordpress.com)
  3. References (cont) - MyDoople (www.mydoople.com) - Android Dev (http://bit.ly/10IFFRm) -

    Viralpatel (http://bit.ly/18ICNZd) - Scaloid (blog.scaloid.org) - Android UI (androiduiux.com) - Android Begin (http://bit.ly/11f1Lri) - Android Exp (http://bit.ly/Zm0ktu) - Developer Feed (http://bit.ly/11MY1lR) - Programmer Guru (android.programmerguru.com) - Suvendu (http://bit.ly/10IHKge) - Xamarin (http://bit.ly/15sGkpW) - Marakana (http://bit.ly/13vM0y6) - Grokking Android (www.grokkingandroid.com)
  4. References (cont) - Android Mobile Develope (http://bit.ly/148e7Wr) - Android Patterns

    (www.androidpatterns.com) - Tutorial Point (http://bit.ly/13vMmF9) - Android Education (android-ed.blogspot.com) - Tech Blog On (http://bit.ly/11f2Wa5) - Android App Market (www.android-app-market.com) - Java Code Geeks (http://bit.ly/18IFOsh) - New Think Tank (http://bit.ly/13cXN5G) - Android Aspect (www.androidaspect.com) - Android Hub 4 You (www.androidhub4you.com) - Surviving With Android (www.survivingwithandroid.com) - Sachin Shelke (http://bit.ly/10R5kUG) - Knowledge of Experience (http://bit.ly/13vNLv7)
  5. References (cont) - Android For Beginners (http://bit.ly/Zm2wRS) - Clean Code

    (http://bit.ly/19yBbzX) - Think Android (thinkandroid.wordpress.com) - Romain Guy (http://bit.ly/13vOc91) - Mr Bool (mrbool.commrbool.com) - Android Guide Tips (http://bit.ly/Zm2Rns) - The Android (http://bit.ly/12XIJHl) - Android Tuts (androidituts.com) - Traintelco (bit.ly/15YprUo) - Pavand (bit.ly/15sWbqS)
  6. Outline - Download and install JDK - Download Android Developer

    Tools (ADT) - Write a Helloworld program (Use ADT) - Download Android Studio - Write a Helloworld program (Use AS)
  7. Task 1 - Download JDK - Goto Java download site

    http://www.oracle. com/technetwork/java/javase/downloads/index.html
  8. Task 1 - Download JDK (cont) - Select Java Platform

    (JDK), choose your operation platform (eg Window X86) and download it
  9. Task 2 - Install JDK - Run the downloaded installer,

    which installs both the JDK (Java Development Kit) and JRE (Java Runtime). By default the JDK and JRE will be installed into directories C:\Program Files\Java
  10. Task 3 - Config PATH environment variables - Windows Operating

    System searches the current directory and the directories listed in the PATH environment variable for executable programs invoked from the CMD shell. It helps programmer can compile Java code in CMD shell. - Click the "Start" button > "Control Panel" > "System" > (Vista/7 only) "Advanced system settings"
  11. Task 4 - Verify the JDK Installation - Launch a

    CMD shell > type java –version to check that JDK is properly installed and display its version, and javac to check Path work properly too.
  12. Task 2: Create Android Application - Go to File >

    New > Android Application Project
  13. Task 2: Create Android Application (cont) - The Main XML

    Layout: https://gist.github. com/leonguyen/5351679
  14. Task 3: Write a MainActivity program (cont) - Enter code:

    https://gist.github.com/leonguyen/5301102
  15. Outline - Linear Layout - Linear Layout - Message -

    Linear Layout - Quiz - Linear Layout - Login - Linear Layout - Register - Linear Layout - Home
  16. Linear Layout - A layout is a view group that

    aligns all children in a single direction, vertically or horizontally. - All children of a LinearLayout are stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are, and a horizontal list will only be one row high (the height of the tallest child, plus padding). A LinearLayout respects margins between children and the gravity (right, center, or left alignment) of each child.
  17. Layout weight - To create a linear layout in which

    each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android: layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1".
  18. Outline - Relative Layout - Relative Layout - Reminder -

    Relative Layout - Work - Table Layout - Table Layout - Weather - Scroll View
  19. Relative Layout - A layout is a view group that

    displays child views in relative positions.
  20. Positioning Views - Some of the many layout properties available

    to views in a RelativeLayout include: android:layout_alignParentTop If "true", makes the top edge of this view match the top edge of the parent. android:layout_centerVertical If "true", centers this child vertically within its parent. android:layout_below Positions the top edge of this view below the view specified with a resource ID. android:layout_toRightOf Positions the left edge of this view to the right of the view specified with a resource ID. These are just a few examples. All layout attributes are documented at RelativeLayout.LayoutParams.
  21. Table Layout - A layout that arranges its children into

    rows and columns. A TableLayout consists of a number of TableRow objects, each defining a row (actually, you can have other children, which will be explained below). TableLayout containers do not display border lines for their rows, columns, or cells. Each row has zero or more cells; each cell can hold one View object. The table has as many columns as the row with the most cells. A table can leave cells empty. Cells can span columns, as they can in HTML.
  22. Outline - List View - List View with Custom Layout

    - List View with Context Menu - List View with Custom Adapter - List View with Custom ArrayList - List View with Activity - List View with Activity and Custom Main Layout - List View with Thread
  23. List View - A view group that displays a list

    of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.
  24. ArrayAdapter - Use this adapter when your data source is

    an array. By default, ArrayAdapter creates a view for each array item by calling toString() on each item and placing the contents in a TextView.
  25. Task 4: Defining an Context Menu XML - Enter code:

    https://gist.github.com/leonguyen/5345210
  26. Loader - Using a CursorLoader is the standard way to

    query a Cursor as an asynchronous task in order to avoid blocking your app's main thread with the query. - When the CursorLoader receives the Cursor result, the LoaderCallbacks receives a callback to onLoadFinished(), which is where you update your Adapter with the new Cursor and the list view then displays the results.
  27. SimpleCursorAdapter - Use this adapter when your data comes from

    a Cursor. When using SimpleCursorAdapter, you must specify a layout to use for each row in the Cursor and which columns in the Cursor should be inserted into which views of the layout.
  28. Task 1: Create Button in Main Layout - Edit file

    res/layout/activity_main.xml: https://gist.github. com/leonguyen/5351375
  29. Task 1: Create Button in Main Layout - Edit file

    res/layout/activity_main.xml: https://gist.github. com/leonguyen/5351375
  30. Widget - App Widgets are miniature application views that can

    be embedded in other applications (such as the Home screen) and receive periodic updates. - To create an App Widget, you need the following: + AppWidgetProviderInfo object Describes the metadata for an App Widget, such as the App Widget's layout, update frequency, and the AppWidgetProvider class. + AppWidgetProvider class implementation Defines the basic methods that allow you to programmatically interface with the App Widget, based on broadcast events. + View layout Defines the initial layout for the App Widget, defined in XML.
  31. Task 2: Defining an Widget XML Layout - Enter code:

    https://gist.github.com/leonguyen/5667769
  32. Task 6: Declaring an App Widget in the Manifest -

    Enter code: https://gist.github.com/leonguyen/5667862
  33. Task 7: Execute your program - Choose "Run As" >

    "Android Application": http://youtu.be/L- TsvhrDoDg
  34. Shared Preferences - The SharedPreferences class provides a general framework

    that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed). + getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter. + getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.
  35. Task 3: Execute your program - Choose "Run As" >

    "Android Application": http://youtu. be/Gj2VkO8zxaM
  36. Task 1: Defining Main XML Menu - Edit file res/menu/main.xml:

    https://gist.github. com/leonguyen/5409423
  37. Task 3: Defining Array XML Value - Edit file res/xml/arrays.xml:

    https://gist.github.com/leonguyen/5409973
  38. Task 9: Execute your program - Choose "Run As" >

    "Android Application": http://youtu. be/CDyIaC3woZU
  39. Outline - SQLite - SQLite with Multiple Tables - SQLite

    with CRUD - SQLite with Multiple Tables CRUD - SQLite with Design Pattern
  40. Task 2: Create UserDA class with CRUD - Enter code:

    https://gist.github.com/leonguyen/5425683
  41. Task 2: Create UserDA class with CRUD - Enter code:

    https://gist.github.com/leonguyen/5524722
  42. Task 6: Create Data Manipulation Object class - Enter code:

    https://gist.github.com/leonguyen/5684403
  43. Storage - Internal storage are private to your application and

    other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed. - External storage such as SD card can also store application data, there's no security enforced upon files you save to the external storage. All applications can read and write files placed on the external storage and the user can remove them.
  44. Task 4: Execute your program - Choose "Run As" >

    "Android Application": http://youtu. be/6BE2BEnoZKM
  45. Intent - Intents are system messages, running around the inside

    of the device, notifying applications of various events, from hardware state changes (e.g.,an SD card was inserted), to incoming data (e.g., an SMS message arrived),to application events (e.g., your activity was launched from the device’s main menu). - Not only can you respond to intents, but you can create your own, to launch other activities, or to let you know when specific situations arise (e.g., raise such-and-so intent when the user click this button). - Intents are asynchronous messages which allow Android components to request functionality from other components of the Android system. For example an Activity can send an Intents to the Android system which starts another Activity. - Three of the core components of an application — activities, services, and broadcast receivers — are activated through messages, called intents.
  46. Explicit and Implicit - Explicit intents designate the target component

    by its name (the component name field, mentioned earlier, has a value set). Since component names would generally not be known to developers of other applications, explicit intents are typically used for application-internal messages. - Implicit intents do not name a target (the field for the component name is blank). Implicit intents are often used to activate components in other applications.
  47. Task 3: Execute your program - Choose "Run As" >

    "Android Application": http://youtu. be/tJaNGzoGUG4
  48. Task 5: Execute your program - Choose "Run As" >

    "Android Application": http://youtu.be/7ewZXibMFy4
  49. Outline - Content Provider - Content Provider with Contact, Call

    Log Picker - Content Provider with Settings - Content Provider Custom
  50. Content Provider - ContentProvider manage access to a structured set

    of data. The main methods are Query, Insert, Update and Delete. - ContentResolver is a static proxy that communicates with a ContentProvider to access its data, either from within the same application or from another application.
  51. Built-In Provider - Android offers access to a wide range

    of system and user data using ContentProviders. - Browser – bookmarks and browser history (requires permission READ_HISTORY_BOOKMARKS and/or WRITE_HISTORY_BOOKMARKS). - CallLog – recent calls made or received with the device. - Contacts – detailed information from the user’s contact list, including people, phones, photos & groups. - MediaStore – contents of the user’s device: audio (albums, artists, genres, playlists), images (including thumbnails) & video. - Settings – system-wide device settings and preferences. - UserDictionary – contents of the user-defined dictionary used for predictive text input. - Voicemail – history of voicemail messages.
  52. Task 4: Execute your program - Choose "Run As" >

    "Android Application": http://youtu. be/21AQ5NFtv-w
  53. Task 4: Execute your program - Choose "Run As" >

    "Android Application": http://youtu. be/CzhJTPPx6oI
  54. Task 5: Execute your program - Choose "Run As" >

    "Android Application": http://youtu.be/q--ak- eZkw8
  55. Outline - Broadcast Receiver - Broadcast Receiver with Custom Intent

    - Broadcast Receiver with Incoming SMS - Broadcast Receiver with Incoming Phone Call
  56. Broadcast Receiver - Broadcast Receiver simply respond to broadcast messages

    from other applications or from the system itself. These messages are sometime called events or intents.
  57. System Events Event Constant Description android.intent.action.BATTERY_CHANGED Sticky broadcast containing the

    charging state, level, and other information about the battery. android.intent.action.BATTERY_LOW Indicates low battery condition on the device. android.intent.action.BATTERY_OKAY Indicates the battery is now okay after being low. android.intent.action.BOOT_COMPLETED This is broadcast once, after the system has finished booting. android.intent.action.BUG_REPORT Show activity for reporting a bug. android.intent.action.CALL Perform a call to someone specified by the data. android.intent.action.CALL_BUTTON The user pressed the "call" button to go to the dialer or other appropriate UI for placing a call. android.intent.action.DATE_CHANGED The date has changed. android.intent.action.REBOOT Have the device reboot.
  58. Task 3: Execute your program - Choose "Run As" >

    "Android Application": http://youtu. be/CuF9IakbEl0
  59. Task 5: Execute your program - Choose "Run As" >

    "Android Application": http://youtu. be/SyLzgtC0Rgw
  60. Task 3: Execute your program - Choose "Run As" >

    "Android Application": http://youtu.be/- CEKaJUV5ZM
  61. Task 3: Execute your program - Choose "Run As" >

    "Android Application": http://youtu.be/Rj1I04fsQQk
  62. References - Getting started with PhoneGap in Eclipse for Android

    (adobe. ly/H0NyLg) - PhoneGap From Scratch (bit.ly/H0NDOW) - Build a Feed Reader (bit.ly/1bDCxsZ)
  63. Task 1: Create index.html - Create the ‘index.html’ under ‘assets/www’

    folder then enter code: gist.github.com/leonguyen/7036244
  64. Task 2: Add to Build Path - Right click on

    ‘cordova.jar’ then select Add to Build Path