Android Programming Basics
Agenda
Basic program structure
Java-based layout
XML-based layout
Eclipse ADT visual layout editor
Hybrid layout
Project s...
General Structure (Common to All Approaches)
Three Main Approaches
 • Java-based
 – Use Java to define Strings, lay out window, create GUI controls, and assign
event...
Java-Based Approach: Template
XML-Based Approach: Template
Hybrid Approach: Template
Java-Based Layout
Big Idea
 • Approach
 – Use Java to define Strings, lay out window, create GUI controls, and assign
event handlers.
 • ...
Code (Main Method)
Code (Event Handler Method)
Results on Emulator
Results on Physical Phone
• Reminder
– Configured phone (once only)
– Shut down emulator, plugged in phone
– R-clicked pro...
XML-Based Layout
Big Idea
 • Approach
 – Use XML files to define Strings, lay out window, create GUI controls, and assign event
handlers....
More Details
 • res/layout/main.xml
 – Define layout and controls with XML description
 • <LinearLayout …>Define contro...
Project Layout
Code (res/layout/main.xml)
Code (res/values/strings.xml)
Code (Java)
Results
Eclipse ADT
Visual Layout Editor
 • Invoking
 – When editing main.xml, click Graphical Layout
 • Features
 – Can inter...
Eclipse Visual Layout Editor
Hybrid Layout
Big Idea
 • Approach
 – Use XML files to define Strings, lay out window, and create
GUI controls.
 – Use Java to assign...
Code (res/layout/main.xml)
Code (res/values/strings.xml)
Code (Java)
Results
Project Layout
Summary
Questions?
Upcoming SlideShare
Loading in …5
×

01 06 - android programming basics

316 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
316
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

01 06 - android programming basics

  1. 1. Android Programming Basics
  2. 2. Agenda Basic program structure Java-based layout XML-based layout Eclipse ADT visual layout editor Hybrid layout Project structure summary
  3. 3. General Structure (Common to All Approaches)
  4. 4. Three Main Approaches  • Java-based  – Use Java to define Strings, lay out window, create GUI controls, and assign event handlers. Like Swing programming.  • XML-based  – Use XML files to define Strings, lay out window, create GUI controls, and assign event handlers. The Java method will read the layout from XML file and pass it to setContentView.  • Hybrid  – Use an XML file to define Strings, lay out window and create GUI controls. Use Java to assign event handlers.  • Examples in this tutorial section  – Button that says “Show Greeting”. Small popup message appears when button is pressed.  – Implemented each of the three ways.
  5. 5. Java-Based Approach: Template
  6. 6. XML-Based Approach: Template
  7. 7. Hybrid Approach: Template
  8. 8. Java-Based Layout
  9. 9. Big Idea  • Approach  – Use Java to define Strings, lay out window, create GUI controls, and assign event handlers.  • Advantages  – Familiar to Java desktop developers. Like approach used for Swing, SWT, and AWT.  – Good for layouts that are dynamic (i.e., that change based on program logic).  • Disadvantages  – Harder to maintain (arguable, but general consensus)  – Works poorly with I18N  – Not generally recommended except for dynamic layouts  • But still acceptable for App Store. Whatever works best for your programmers and your app. No code police.
  10. 10. Code (Main Method)
  11. 11. Code (Event Handler Method)
  12. 12. Results on Emulator
  13. 13. Results on Physical Phone • Reminder – Configured phone (once only) – Shut down emulator, plugged in phone – R-clicked project, Run As->Android Application
  14. 14. XML-Based Layout
  15. 15. Big Idea  • Approach  – Use XML files to define Strings, lay out window, create GUI controls, and assign event handlers.  • Define layout and controls in res/layout/main.xml  • Define Strings in res/values/strings.xml  • Advantages  – Easier to maintain  – Works well with I18N  – Can use visual layout editor in Eclipse  – Standard/recommended approach (along with hybrid)  • Disadvantages  – Works poorly for dynamic layouts
  16. 16. More Details  • res/layout/main.xml  – Define layout and controls with XML description  • <LinearLayout …>Define controls</LinearLayout>  – Refer to strings (from strings.xml) with @string/string_name  – Assign event handler with android:onClick  • res/values/strings.xml  – Define strings used in GUI or that might change with I18N  • Java code  – Refer to layout with R.layout.main (res/layout/main.xml)  – Refer to strings with getString(R.string.string_name)  – Refer to controls with findViewById(R.id.some_id)  • More info  – http://developer.android.com/guide/topics/ui/declaring-layout.html
  17. 17. Project Layout
  18. 18. Code (res/layout/main.xml)
  19. 19. Code (res/values/strings.xml)
  20. 20. Code (Java)
  21. 21. Results
  22. 22. Eclipse ADT Visual Layout Editor  • Invoking  – When editing main.xml, click Graphical Layout  • Features  – Can interactively change layout attributes (vertical/horizontal, fill  characteristics, etc.)  – Can drag from palette of available GUI controls  – Can interactively set control characteristics (colors, fill, event handler, etc.)  – Shows visual preview  • Warning  – Although visual editor is very useful, you should still manually edit XML to fix indentation, order of attributes, use of obsolete attribute names (fill_parent instead of match_parent), and other stylistic things.
  23. 23. Eclipse Visual Layout Editor
  24. 24. Hybrid Layout
  25. 25. Big Idea  • Approach  – Use XML files to define Strings, lay out window, and create GUI controls.  – Use Java to assign event handlers.  • Advantages  – Mostly same as XML-based approach  – But, since event handler needs to be edited by Java programmer anyhow, often makes more sense to assign it programmatically as well.  • Disadvantages  – Works poorly for dynamic layouts
  26. 26. Code (res/layout/main.xml)
  27. 27. Code (res/values/strings.xml)
  28. 28. Code (Java)
  29. 29. Results
  30. 30. Project Layout
  31. 31. Summary
  32. 32. Questions?

×