Your SlideShare is downloading. ×
Android Programming                         Lecture 06                       Vladimir Kulyukin                Department o...
Outline   ●     Working with Resources   ●     Customizing Views   ●     Customizing Array Adapters   ●     Two Applicatio...
Q&A   ●     Q: Do I have to use broadcast receivers and     broadcast senders in HW 04?   ●     A: No, it is primarily a m...
Working with Resourceswww.youtube.com/vkedco           www.vkedco.blogspot.com
Compiled and Non-compiled Resources   ●       Most resources are compiled into binary files before       deployment   ●   ...
Resource Sub-directories   ●     anim – compiled animation files   ●     drawable - .bmp, .png, jpg, etc.   ●     layout –...
Resource Compiler   ●       The resource compiler is part of the Android       Packaging Tool (AAPT)   ●       The resourc...
Other Key Resources   Colors                /res/values/some_file.xml   Color codes; exposed in                           ...
Raw Assets   ●       Project directory /assets/ contains raw files   ●       Unlike /res/ directory, /assets/ may contain ...
Color Resources   ●       Android color constants are available in android.R.color       namespace   ●       http://code.g...
Color Resources   ●       Color resource files are placed in /res/values/   ●       Here is how to access color resources ...
Dimension Resources   ●       Dimensions can be specified in the following units:   ●       px – pixels   ●       in – inc...
Raw Resources   ●       Place audio, video, text that you need in your       application in /res/raw/   ●       These file...
Raw Resources: Example     private String processRawTextFile() {        Resources res = getResources();        InputStream...
Assets   ●       Directories /assets/ and /res/ are at the same level of       the project tree hierarchy   ●       Files ...
Assets: Example     // process /assets/church_bio.txt     private String processAssetsTextFile() {         AssetManager am...
Customizing Viewswww.youtube.com/vkedco              www.vkedco.blogspot.com
Custom Views    ●        Views can be customized to change the look        and feel of your applications    ●        Views...
Custom Views: Example 1www.youtube.com/vkedco        www.vkedco.blogspot.com
Custom Views: Example 1: Step 1                               (famous_mathematicians.xml)   <?xml version="1.0" encoding="...
Custom Views: Example 1: Steps 2 & 3   private static String[] mComputabilityPillars;   private static Resources mRes;   p...
Custom Views: Example 1 Demowww.youtube.com/vkedco       www.vkedco.blogspot.com
Custom Views: Example 2www.youtube.com/vkedco        www.vkedco.blogspot.com
Custom Views: Example 2    ●        What Do We Need To Do To Construct Custom View 2?    ●        Step 1: Specify the math...
Custom Views: Example 2: Step 1                              (same as in Example 1)   <?xml version="1.0" encoding="utf-8"...
Custom Views: Example 2: Step 3 (colors.xml)   <?xml version="1.0" encoding="utf-8"?>   <resources>      <color name="back...
Custom Views: Example 2: Step 3 (dimens.xml)   <?xml version="1.0" encoding="utf-8"?>   <resources>       <dimen name="lef...
Custom Views: Example 2: Step 3                                             (mathematician_view_1.xml)   <?xml version="1....
Custom Views: Example 2: Steps 4 & 5   private void populateListViewTwo() {         mComputabilityPillars = mRes.getString...
Custom Views: Example 2 Demowww.youtube.com/vkedco       www.vkedco.blogspot.com
Custom Views: Example 3www.youtube.com/vkedco        www.vkedco.blogspot.com
Custom Views: Example 3    ●        What Do We Need To Do To Construct Custom View 3?    ●        Step 1: Specify the math...
Custom Views: Example 3: Steps 1 & 2     Steps 1 & 2 are the same as in Example 2www.youtube.com/vkedco         www.vkedco...
Custom Views: Example 3: Step 3                          (mathematician_view_2.xml)                 ListOfMathematicians P...
Customizing Array Adapterswww.youtube.com/vkedco          www.vkedco.blogspot.com
Custom Views: Example 3: Step 4    ●        MathematicianViewArrayAdapter in Example 3        illustrates how to customize...
Custom Views: Example 3: Step 4    ●        To customize the layout of each view, you        can extend ArrayAdapter and o...
Customizing ArrayAdapter      public class CustomArrayAdapter extends ArrayAdapter<Class> {               private int mRes...
Custom Views: Example 3: Step 4                Customizing Array Adapters                Project ListOfMathematicians:    ...
Custom Views: Example 3 Demowww.youtube.com/vkedco       www.vkedco.blogspot.com
Todo List Application    ●        Todo List Application in Chapters 2, 4, 5 of        Meiers “Professional Android 2 Appli...
Todo List Application Demowww.youtube.com/vkedco          www.vkedco.blogspot.com
Consumption of Internet Resources                in Android Applicationswww.youtube.com/vkedco        www.vkedco.blogspot....
Terminology: GPRS   ●       General packet radio service (GPRS) is a packet       oriented mobile data service   ●       G...
Terminology: EDGE   ●       EDGE stands for Enhanced Data rates for GSM       (global system for mobile communications)   ...
Reasons for Developing Internet Apps   ●     Limit bandwidth requirements to data use   ●     Dealing with patch Internet ...
Connecting to Internet Resources:                                               AndroidManifest.xml   ●        If your app...
Connecting to Internet Resources: Java      String myUrlStr = getResources().getString(R.id.my_url);      try {          U...
Projects      BrowserChoiceDemo and LaunchWebBrowserwww.youtube.com/vkedco       www.vkedco.blogspot.com
References & Reading Suggestions    ●        Chapters 3, 4, 5 in “Professional Android 2        Application Development” b...
Feedback      Comments, typos, bugs to vladimir dot kulyukin      at gmail dot comwww.youtube.com/vkedco           www.vke...
Android Programming: Lecture 06
Android Programming: Lecture 06
Upcoming SlideShare
Loading in...5
×

Android Programming: Lecture 06

2,072

Published on

1) Resources
2) Customizing Views
3) Customizing ArrayAdapters
4) Internet Access in Android Applications
5) Class home page is at http://vkedco.blogspot.com/2011/08/android-programming-home.html

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,072
On Slideshare
0
From Embeds
0
Number of Embeds
25
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Android Programming: Lecture 06"

  1. 1. Android Programming Lecture 06 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
  2. 2. Outline ● Working with Resources ● Customizing Views ● Customizing Array Adapters ● Two Applications That Illustrate Custom Views and Array Adapters ● Internet Accesswww.youtube.com/vkedco www.vkedco.blogspot.com
  3. 3. Q&A ● Q: Do I have to use broadcast receivers and broadcast senders in HW 04? ● A: No, it is primarily a menu assignment ● Q: What will the final project proposal entail? ● A: Writeup of your application including a use casewww.youtube.com/vkedco www.vkedco.blogspot.com
  4. 4. Working with Resourceswww.youtube.com/vkedco www.vkedco.blogspot.com
  5. 5. Compiled and Non-compiled Resources ● Most resources are compiled into binary files before deployment ● Two types of resource files: XML and raw (image, video, audio) ● String and layout resource files are compiled into binary format ● XML files placed in /res/raw/ are not compiled into binary format ● Audio and video also go into /res/raw/www.youtube.com/vkedco www.vkedco.blogspot.com
  6. 6. Resource Sub-directories ● anim – compiled animation files ● drawable - .bmp, .png, jpg, etc. ● layout – view specifications ● values – arrays, colors, dimensions, strings, styles ● xml – compiled XML ● raw – non-compiled raw fileswww.youtube.com/vkedco www.vkedco.blogspot.com
  7. 7. Resource Compiler ● The resource compiler is part of the Android Packaging Tool (AAPT) ● The resource compiler compiles all resources except those in /res/raw/ and places them into the .apk (stands for “Android Package”) file ● The .apk file is similar to the Java .jar file and contains all the applications code and resources ● The .apk file is installed on the devicewww.youtube.com/vkedco www.vkedco.blogspot.com
  8. 8. Other Key Resources Colors /res/values/some_file.xml Color codes; exposed in R.java as R.color.* Dimensions /res/values/some_file.xml Sizes of various elements in pixels, inches, millimeters; exposed in R.java as R.dimen.* Images /res/drawable/some- Image resources (.jpg, .gif, subfolder/some_file.xml .png); Exposed through R.java as R.drawable.*www.youtube.com/vkedco www.vkedco.blogspot.com
  9. 9. Raw Assets ● Project directory /assets/ contains raw files ● Unlike /res/ directory, /assets/ may contain arbitrarily many sub-directories ● Unlike files in /res/ directory, /assets/ directory do not generate resource IDs ● Relative path names must be used to access files in /assets/www.youtube.com/vkedco www.vkedco.blogspot.com
  10. 10. Color Resources ● Android color constants are available in android.R.color namespace ● http://code.google.com/android/reference/android/R.color.html ● You can also specify your own colors in /res/values/ <resources> <color name=”red_clr”>#f00</color> <color name=”blue_clr”>#0000ff</color> </resources>www.youtube.com/vkedco www.vkedco.blogspot.com
  11. 11. Color Resources ● Color resource files are placed in /res/values/ ● Here is how to access color resources in Java and XML int color = getResources().getColor(R.color.my_color); <TextView android:textColor=”@color/blue_clr” android:text=”Blue Text” />www.youtube.com/vkedco www.vkedco.blogspot.com
  12. 12. Dimension Resources ● Dimensions can be specified in the following units: ● px – pixels ● in – inches ● mm – millimeters ● pt – points ● dp – density-independent pixels based on 160 dpi (pixel density per inch) screen ● sp – scale-independent pixels (used with fonts)www.youtube.com/vkedco www.vkedco.blogspot.com
  13. 13. Raw Resources ● Place audio, video, text that you need in your application in /res/raw/ ● These files are not compiled but moved to the .apk as they are ● These files are referenced through R.java ● For example, /res/raw/hilbert_bio.txt can be referenced as R.raw.hilbert_biowww.youtube.com/vkedco www.vkedco.blogspot.com
  14. 14. Raw Resources: Example private String processRawTextFile() { Resources res = getResources(); InputStream instrm = res.openRawResource(R.raw.hilbert_bio); processRawStream(instrm); }www.youtube.com/vkedco www.vkedco.blogspot.com
  15. 15. Assets ● Directories /assets/ and /res/ are at the same level of the project tree hierarchy ● Files placed in /assets/ do not generate IDs in R.java ● Files in /assets/ are accessed through relative paths starting at /assets/ ● The AssetManager class is used to access assetswww.youtube.com/vkedco www.vkedco.blogspot.com
  16. 16. Assets: Example // process /assets/church_bio.txt private String processAssetsTextFile() { AssetManager amngr = getAssets(); InputStream instrm = amngr.open(“hilbert_bio.txt”); processAssetsStream(instrm); }www.youtube.com/vkedco www.vkedco.blogspot.com
  17. 17. Customizing Viewswww.youtube.com/vkedco www.vkedco.blogspot.com
  18. 18. Custom Views ● Views can be customized to change the look and feel of your applications ● Views can be are typically customized by extending either View or SurfaceView classes ● View is a lightweight solution for 2D graphics ● SurfaceView is used for 3D graphicswww.youtube.com/vkedco www.vkedco.blogspot.com
  19. 19. Custom Views: Example 1www.youtube.com/vkedco www.vkedco.blogspot.com
  20. 20. Custom Views: Example 1 ● What Do We Need To Do To Construct Custom View 1? ● Step 1: Specify the mathematicians names in an XML file (famous_mathematicians.xml) ● Step 2: Create an ArrayAdapter<String> ● Step 3: Connect the ArrayAdapter<String> to the activitys ListViewwww.youtube.com/vkedco www.vkedco.blogspot.com
  21. 21. Custom Views: Example 1: Step 1 (famous_mathematicians.xml) <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="computability_pillars"> <item>Alonzo Church</item> <item>Kurt Gödel</item> <item>David Hilbert</item> <item>Giuseppe Peano</item> <item>Georg Cantor</item> </string-array> </resources>www.youtube.com/vkedco www.vkedco.blogspot.com
  22. 22. Custom Views: Example 1: Steps 2 & 3 private static String[] mComputabilityPillars; private static Resources mRes; private void populateListViewOne() { mRes = getResources(); mComputabilityPillars = mRes.getStringArray(R.array.computability_pillars); getListView().setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, mComputabilityPillars)); }www.youtube.com/vkedco www.vkedco.blogspot.com
  23. 23. Custom Views: Example 1 Demowww.youtube.com/vkedco www.vkedco.blogspot.com
  24. 24. Custom Views: Example 2www.youtube.com/vkedco www.vkedco.blogspot.com
  25. 25. Custom Views: Example 2 ● What Do We Need To Do To Construct Custom View 2? ● Step 1: Specify the mathematicians names in XML (famous_mathematicians.xml) ● Step 2: Design and develop a mathematician class Mathematician.java ● Step 3: Design and develop a mathematicians view in Java (MathematicianView.java) and in XML (colors.xml, dimens.xml mathematician_view_1.xml) ● Step 4: Create an ArrayAdapter<Mathematician> ● Step 5: Connect ArrayAdapter<Mathematician> to the activitys ListViewwww.youtube.com/vkedco www.vkedco.blogspot.com
  26. 26. Custom Views: Example 2: Step 1 (same as in Example 1) <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="computability_pillars"> <item>Alonzo Church</item> <item>Kurt Gödel</item> <item>David Hilbert</item> <item>Giuseppe Peano</item> <item>Georg Cantor</item> </string-array> </resources>www.youtube.com/vkedco www.vkedco.blogspot.com
  27. 27. Custom Views: Example 2: Step 2 Project ListOfMathematicians: Mathematician.javawww.youtube.com/vkedco www.vkedco.blogspot.com
  28. 28. Custom Views: Example 2: Step 3 (colors.xml) <?xml version="1.0" encoding="utf-8"?> <resources> <color name="background_clr">#AAFFFF99</color> <color name="line_clr">#FF0000FF</color> <color name="margin_clr">#90FF0000</color> <color name="text_clr">#AA0000FF</color> </resources>www.youtube.com/vkedco www.vkedco.blogspot.com
  29. 29. Custom Views: Example 2: Step 3 (dimens.xml) <?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="left_margin">50dp</dimen> <dimen name="right_margin">30dp</dimen> </resources>www.youtube.com/vkedco www.vkedco.blogspot.com
  30. 30. Custom Views: Example 2: Step 3 (mathematician_view_1.xml) <?xml version="1.0" encoding="utf-8"?> <org.vkedco.android.listofmathematicians.MathematicianView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:scrollbars="vertical" android:textColor="@color/text_clr" android:textSize="20dp" android:fadingEdge="vertical" />www.youtube.com/vkedco www.vkedco.blogspot.com
  31. 31. Custom Views: Example 2: Steps 4 & 5 private void populateListViewTwo() { mComputabilityPillars = mRes.getStringArray(R.array.computability_pillars); ArrayList<Mathematician> mathematicians = new ArrayList<Mathematician>(); for(String name : mComputabilityPillars) { String[] fnln = name.trim().split(" "); mathematicians.add(new Mathematician(fnln[0], fnln[1])); } getListView().setAdapter(new ArrayAdapter<Mathematician>(this, R.layout.mathematician_view_1, mathematicians)); }www.youtube.com/vkedco www.vkedco.blogspot.com
  32. 32. Custom Views: Example 2 Demowww.youtube.com/vkedco www.vkedco.blogspot.com
  33. 33. Custom Views: Example 3www.youtube.com/vkedco www.vkedco.blogspot.com
  34. 34. Custom Views: Example 3 ● What Do We Need To Do To Construct Custom View 3? ● Step 1: Specify the mathematicians names in XML (famous_mathematicians.xml) ● Step 2: Design and develop a mathematician class Mathematician ● Step 3: Design and develop a mathematicians view in XML (mathematician_view_2.xml) ● Step 4: Create an MathematicianArrayAdapter ● Step 5: Connect MathematicianArrayAdapter to the activitys ListViewwww.youtube.com/vkedco www.vkedco.blogspot.com
  35. 35. Custom Views: Example 3: Steps 1 & 2 Steps 1 & 2 are the same as in Example 2www.youtube.com/vkedco www.vkedco.blogspot.com
  36. 36. Custom Views: Example 3: Step 3 (mathematician_view_2.xml) ListOfMathematicians Project: mathematician_view_2.xmlwww.youtube.com/vkedco www.vkedco.blogspot.com
  37. 37. Customizing Array Adapterswww.youtube.com/vkedco www.vkedco.blogspot.com
  38. 38. Custom Views: Example 3: Step 4 ● MathematicianViewArrayAdapter in Example 3 illustrates how to customize an adapter ● By default, ArrayAdapter uses the toString() values of the objects in the sequence that it binds with a view container ● Under some circumstances, it is necessary to customize the layout of each viewwww.youtube.com/vkedco www.vkedco.blogspot.com
  39. 39. Custom Views: Example 3: Step 4 ● To customize the layout of each view, you can extend ArrayAdapter and override the getView() method ● The getView() method constructs, inflates, and populates the view that will be displayed in the parent containerwww.youtube.com/vkedco www.vkedco.blogspot.com
  40. 40. Customizing ArrayAdapter public class CustomArrayAdapter extends ArrayAdapter<Class> { private int mResource; public CustomArrayAdapter(Context c, int res, List<Class> items) { super(c, res, items); mResource = res; } @Override public View getView(int pos, View convertView, ViewGroup parent) { // do something with convertView return a new View } }www.youtube.com/vkedco www.vkedco.blogspot.com
  41. 41. Custom Views: Example 3: Step 4 Customizing Array Adapters Project ListOfMathematicians: MathematicianViewArrayAdapter.javawww.youtube.com/vkedco www.vkedco.blogspot.com
  42. 42. Custom Views: Example 3 Demowww.youtube.com/vkedco www.vkedco.blogspot.com
  43. 43. Todo List Application ● Todo List Application in Chapters 2, 4, 5 of Meiers “Professional Android 2 Application Development” illustrates: – menu construction – run-time menu construction with onPrepareOptionsMenu() – custom view design – custom adapter designwww.youtube.com/vkedco www.vkedco.blogspot.com
  44. 44. Todo List Application Demowww.youtube.com/vkedco www.vkedco.blogspot.com
  45. 45. Consumption of Internet Resources in Android Applicationswww.youtube.com/vkedco www.vkedco.blogspot.com
  46. 46. Terminology: GPRS ● General packet radio service (GPRS) is a packet oriented mobile data service ● GPRS works on the 2G and 3G cellular communication systems ● GPRS usage charging is based on data volume (e.g., up to 5 GB per month for a fixed fee) ● GPRS is a best-effort service (no quality of service quarantees)www.youtube.com/vkedco www.vkedco.blogspot.com
  47. 47. Terminology: EDGE ● EDGE stands for Enhanced Data rates for GSM (global system for mobile communications) Evolution or Enhanced Data for Global Evolution ● EDGE is a mobile data and Internet access technology; fast enough to support video and music clips, picture & video messaging, etc ● EDGE is considered by some to be slower than 3Gwww.youtube.com/vkedco www.vkedco.blogspot.com
  48. 48. Reasons for Developing Internet Apps ● Limit bandwidth requirements to data use ● Dealing with patch Internet connections ● Providing better user experiences by combining Internet with Android sensorswww.youtube.com/vkedco www.vkedco.blogspot.com
  49. 49. Connecting to Internet Resources: AndroidManifest.xml ● If your application needs to connect to Internet resources, make sure that you add <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" … /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity> … </activity> </application> <uses-permission android:name="android.permission.INTERNET"></uses-permission> </manifest>www.youtube.com/vkedco www.vkedco.blogspot.com
  50. 50. Connecting to Internet Resources: Java String myUrlStr = getResources().getString(R.id.my_url); try { URL myURL = new URL(myUrlStr); URLConnection conn = myURL.openConnection(); int responseCode = conn.getResponseCode(); if ( responseCode == HttpURLConnection.HTTP_OK ) { // do something } } catch ( Exception e ) { // handle exceptions }www.youtube.com/vkedco www.vkedco.blogspot.com
  51. 51. Projects BrowserChoiceDemo and LaunchWebBrowserwww.youtube.com/vkedco www.vkedco.blogspot.com
  52. 52. References & Reading Suggestions ● Chapters 3, 4, 5 in “Professional Android 2 Application Development” by Rito Meier ● Chapter 3 in “Pro Android 2” by Sayed Hashimi, Satya Komatineni, Dave MacLean ● http://en.wikipedia.org/wiki/General_Packet_Radio_Servicewww.youtube.com/vkedco www.vkedco.blogspot.com
  53. 53. Feedback Comments, typos, bugs to vladimir dot kulyukin at gmail dot comwww.youtube.com/vkedco www.vkedco.blogspot.com

×