Your SlideShare is downloading. ×
Theory of Computation: Lecture36
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Theory of Computation: Lecture36

1,078
views

Published on

1) Context-Free Grammars (CFGs) …

1) Context-Free Grammars (CFGs)
2) Backus-Naur Notation (BNN)
3) Stack Machines
4) Class home page is at http://vkedco.blogspot.com/2011/08/theory-of-computation-home.html

Published in: Education, Technology

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

  • Be the first to like this

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Android Programming Lecture 06 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco
  • 2. Outline ● Working with Android Resources ● Customizing Views ● Customizing Array Adapters ● Two Applications That Illustrate Custom Views and Array Adapters ● Internet Accesswww.youtube.com/vkedco
  • 3. Resources Source code and screencasts for this lecture are available at http://vkedco.blogspot.com/2011/10/android-programming-lecture-06.htmlwww.youtube.com/vkedco
  • 4. Working with Android Resourceswww.youtube.com/vkedco
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 14. Raw Resources: Example private String processRawTextFile() { Resources res = getResources(); InputStream instrm = res.openRawResource(R.raw.hilbert_bio); processRawStream(instrm); }www.youtube.com/vkedco
  • 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
  • 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
  • 17. Customizing Viewswww.youtube.com/vkedco
  • 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
  • 19. Custom Views: Example 1www.youtube.com/vkedco
  • 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
  • 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
  • 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
  • 23. Custom Views: Example 1 Demowww.youtube.com/vkedco
  • 24. Custom Views: Example 2www.youtube.com/vkedco
  • 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
  • 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
  • 27. Custom Views: Example 2: Step 2 Project ListOfMathematicians: Mathematician.javawww.youtube.com/vkedco
  • 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
  • 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
  • 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
  • 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
  • 32. Custom Views: Example 2 Demowww.youtube.com/vkedco
  • 33. Custom Views: Example 3www.youtube.com/vkedco
  • 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
  • 35. Custom Views: Example 3: Steps 1 & 2 Steps 1 & 2 are the same as in Example 2www.youtube.com/vkedco
  • 36. Custom Views: Example 3: Step 3 (mathematician_view_2.xml) ListOfMathematicians Project: mathematician_view_2.xmlwww.youtube.com/vkedco
  • 37. Customizing Array Adapterswww.youtube.com/vkedco
  • 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
  • 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
  • 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
  • 41. Custom Views: Example 3: Step 4 Customizing Array Adapters Project ListOfMathematicians: MathematicianViewArrayAdapter.javawww.youtube.com/vkedco
  • 42. Custom Views: Example 3 Demowww.youtube.com/vkedco
  • 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
  • 44. Todo List Application Demowww.youtube.com/vkedco
  • 45. Internet Resourceswww.youtube.com/vkedco
  • 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
  • 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
  • 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
  • 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
  • 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
  • 51. Projects BrowserChoiceDemo and LaunchWebBrowserwww.youtube.com/vkedco
  • 52. References & Reading Suggestions ● http://code.google.com/android/reference/android/R.color.html ● http://developer.android.com/resources/tutorials/views/index.html ● http://en.wikipedia.org/wiki/General_Packet_Radio_Service ● Chapters 3, 4, 5 in “Professional Android 2 Application Development” by Rito Meier ● Chapter 3 in “Pro Android 2” by S. Hashimi, S. Komatineni, D. MacLeanwww.youtube.com/vkedco
  • 53. Feedback Bugs, errors to vladimir dot kulyukin at gmail dot comwww.youtube.com/vkedco