Android Programming Basics
Android Programming Basics
Servlets, JSP, Hibernate/JPA, EJB3, GWT, jQuery, and SOAP-based Web Services RESTful Web Services, Android. Spring, JSF 2.0, Java 6, Ajax, RESTful GWT, Spring, Hibernate, Contact hall@coreservlets.com for details Developed and taught by well-known author and developer. At public venues or onsite at your location.
Already configured
Android SDK components updated Eclipse preferences
Android SDK location set At least one AVD (Android Virtual Device) defined
Documentation
7
http://developer.android.com/guide/developing/index.html http://developer.android.com/reference/packages.html
Steps
File New Project Android Android Project
Once you do this once, next time you can do File New Android Project
Fill in options as shown on next page Run new project as shown previously
R click Run As R-click Android Application
8
Build Target
Th Android version th t you want t use. F most phone apps, choose The A d id i that t to For t h h 2.2, since that is the most common version in use worldwide.
Application name
Human-readable app name title will be shown on Android title bar bar.
Package name
Apps on a particular Android device must have unique packages, so use y p yp j com.yourCompany.project
Create Activity
The name of the top-level Java class
Human-readable app name Package. Use naming convention to ensure uniqueness Java class name Number corresponding to build target
10
Execution steps
Same as with any project
R-click Run As Android Applicaton
Reminder: do not close emulator after testing. Emulator takes a long time to start initially, but it is relatively fast to deploy a new or a changed project to the emulator.
11
Steps
Configure phone to allow untrusted apps
Once only. See next page.
Shut down emulator Sh d l Plug in phone R-click R click project Run As Android Application
This installs and runs it. But it is left installed after you unplug phone, and you can run it on phone in normal manner.
12
public class SomeName extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SomeLayoutOrView view = createOrGetView(); ... setContentView(view); } ... }
15
Apps are frequently shut down by the device. This lets you remember some info about the previous invocation. Covered in later lectures, but for now, just know that you should always call super.onCreate as first line of onCreate.
I also follow a few official Android coding conventions here (4-space indentation, no *s in imports, {s on same line as previous code, @Override where appropriate). Conventions are strictly enforced in official code, and are used in all examples and tutorials. So, you might as well follow the conventions from the beginning. Follow these simple ones for now, and a later lecture will give coding convention details and provide an Eclipse preferences file to help with them.
Hybrid
Use an XML file to define Strings, lay out window and create g y GUI controls. Use Java to assign event handlers.
16
17
XML
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="some_name"></string> </resources>
18
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout > <TextView /> <Button android:onClick="handlerMethod" /> </LinearLayout>
XML
19
Controls that need handlers are given IDs You do not use android:onClick to assign handler
Java-Based Layout
Big Idea
Approach
U J Use Java to d fi S i define Strings, l out window, create GUI lay i d controls, and assign event handlers.
Advantages g
Familiar to Java desktop developers. Like approach used for Swing, SWT, and AWT. Good for layouts that are dynamic (i.e., that change based (i e on program logic).
Disadvantages
H d to maintain (arguable, but general consensus) Harder i i ( bl b l ) Works poorly with I18N g y p y y 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.
21
22
23
Results on Emulator
Reminder
R-clicked project, Run As Android Application
24
25
XML-Based Layout
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 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
27
More Details
res/layout/main.xml
D fi layout and controls with XML description Define l d l ih d i i
<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
res/layout/main.xml
Refer to layout with R.layout.main 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
28
Project Layout
Refers to layout defined in res/layout/main.xml with R.layout.main. Refers to strings defined in res/values/strings.xml with getString(R.string.string_name)
Defines screen layout and GUI controls. Optionally assigns event handlers to controls. Refers to strings defined in res/values/strings.xml with @string/string_name
Conventional for main file to be called main.xml, but not required. If it is foo.xml, then Java uses R.layout.foo. As we will see later, complex apps have several layout files for different screens screens.
Defines strings that are either used in G controls or that f GUI might change with internationalization.
29
Code (res/layout/main.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout y xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" These attributes (android:orientation, etd.) are defined in JavaDoc API for android:layout_width="match_parent" LinearLayout. y android:layout_height="match_parent"> android:layout height="match parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/window_text"/> <Button android:text= @string/button_label android:text="@string/button label" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="showToast"/> </LinearLayout> /
30
This must be a public method in main class, have a void return type, and take a View as argument. No interface needs to be implemented, as it does with event handlers referred to in Java code.
Code (res/values/strings.xml)
app_name is special, predefined name (although it can <?xml version="1.0" encoding="utf-8"?> be overridden in AndroidManifest.xml) All the rest are developer-specified names. developer specified <resources> <string name="app_name">Say Hello Application</string> <string name="window_text"> Press the button below to receive a friendly greeting from Android. </string> <string name="button_label">Show Greeting</string> <string name="greeting_text">Hello f t i " ti t t" ll from Android!</string> d id! / t i </resources>
main.xml main xml refers to this with @string/greeting text @string/greeting_text Java refers to this with getString(R.string.greeting_text) Eclipse auto-completion will recognize the names when editing other files that use them.
31
Code (Java)
public class SayHelloXml extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void showToast(View clickedButton) { String greetingText = getString(R.string.greeting_text); St i ti t tSt i ( t i ti t t) Toast tempMessage = Toast.makeText(this, greetingText, Toast.LENGTH_SHORT); Toast LENGTH SHORT); tempMessage.show(); } }
32
Results
On emulator
R-clicked project, Run As Android Application Exactly same look and behavior as previous (Java-based) example
On physical phone
Configured phone (once only) Sh d Shut down emulator, plugged in phone l l di h R-clicked project, Run As Android Application Exactly same look and behavior as previous (Java-based) example
33
Features
Can interactively change layout attributes (vertical/horizontal, fill characteristics, etc.) h i i ) 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.
More info
http://tools.android.com/recent http://www.youtube.com/watch?v=Oq05KqjXTvs
35
36
Hybrid Layout
Big Idea
Approach
Use XML files to define Strings, lay out window, and create GUI controls. U J Use Java to assign event h dl i handlers.
Advantages
M tl same as XML-based approach Mostly XML b d h But, since event handler needs to be edited by Java programmer anyhow, often makes more sense to assign it anyhow programmatically as well.
Disadvantages g
Works poorly for dynamic layouts
38
Code (res/layout/main.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout y xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> android:layout height="match parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/window_text"/> <Button android:id= @+id/greeting_button android:id="@+id/greeting button" android:text="@string/button_label" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
39
We define an id for the button, so that the button can be referred to in Java code with findViewById(R.id.greeting_button)
We do not assign an event handler to the button, as we did in the previous example.
Code (res/values/strings.xml)
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Say Hello Application</string> <string name="window_text"> Press the button below to receive a friendly greeting from Android. </string> <string name="button_label">Show Greeting</string> <string name="greeting_text">Hello f t i " ti t t" ll from Android!</string> d id! / t i </resources>
40
Code (Java)
public class SayHelloHybrid extends Activity { @Override public void onCreate(Bundle savedInstanceState) { You must call setContentView before super.onCreate(savedInstanceState); calling findViewById. If you call setContentView(R.layout.main); findViewById first, you get null. Button greetingButton = (Button)findViewById(R.id.greeting_button); greetingButton.setOnClickListener(new Toaster()); } private class Toaster implements OnClickListener { @Override public void onClick(View clickedButton) { String greetingText = getString(R.string.greeting_text); Toast tempMessage = Toast.makeText(SayHelloHybrid.this, greetingText, Toast.LENGTH_SHORT); tempMessage.show(); } }}
41
Results
On emulator
R-clicked project, Run As Android Application Exactly same look and behavior as previous (Java-based) example
On physical phone
Configured phone (once only) Sh d Shut down emulator, plugged in phone l l di h R-clicked project, Run As Android Application Exactly same look and behavior as previous (Java-based) example
42
Wrap-Up
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Project Layout
Refers to layout defined in res/layout/main.xml with R.layout.main. Refers to controls defined in res/layout/main.xml with findViewById(R.id.some_id) Refers to strings defined in res/values/strings.xml with getString(R.string.string_name)
Defines screen layout and GUI controls. Optionally assigns event handlers to controls. Refers to strings defined in res/values/strings.xml with @string/string_name
Defines strings that are either used in G controls or that f GUI might change with internationalization.
44
Summary
XML code
res/layout/main.xml /l / i l
Defines layout properties. Defines GUI controls. Sometimes assigns event handlers to controls
res/values/strings.xml / l / i l
Defines Strings used in GUI or for I18N.
Java code
Main class extends Action
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Call setContentView before calling setContentView(R.layout.main); findViewById. maybeFindControlAndAssignHandler(); }
Questions?
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.