Module 3: Application Basic: M.Sc. Bui Tan Loc
Module 3: Application Basic: M.Sc. Bui Tan Loc
Department of Software Engineering, Faculty of Information Technology, University of Science Ho Chi Minh City, Viet Nam
.
Objectives
Contents
from within
How Different Resources Are Referenced from Within Java and XML Files
Each
Android project includes a manifest le, AndroidManifest.xml, stored in the root of the project hierarchy. The manifest lets you dene the structure and metadata of your application, its components, and its requirements.
each of the components (Activities, Services, Content Providers, and Broadcast Receivers) that make up your application and, using Intent Filters and Permissions, determines how they interact with each other and with other applications.
. 10
Activity Nodes Service Nodes Content Provider Nodes Broadcast Receiver Nodes
11
Service Node
. 15
<receiver android:name=".MusicIntentReceiver" > <intent-filter > <action android:name="android.media.AUDIO_BECOMING_NOISY" /> </intent-filter> <intent-filter > <action android:name="android.intent.action.MEDIA_BUTTON" /> </intent-filter> </receiver> </application> Broadcast Receiver Node
16
Views are the base class for all visual interface elements
(commonly known as controls or widgets). All UI controls, including the layout classes, are derived from View. View Groups are extensions of the View class that can contain multiple child Views. Extend the ViewGroup class to create compound controls made up of interconnected child Views. The ViewGroup class is also extended to provide the layout managers that help you lay out controls within your Activities. Activities described in the previous modules, represent the window, or screen, being displayed. Activities are the Android equivalent of Forms. To display a user interface you assign a View (usually a layout) to an Activity.
. 17
ViewGroup
18
19
setBackgroundColor(int color) setBackgroundDrawable(Drawable d) setClickable(boolean c) setFocusable(boolean f) setLayoutParams(ViewGroup.LayoutParams l) setMinimumHeight(int minHeight) setMinimumWidth(int minWidth) setOnClickListener(OnClickListener l) setOnFocusChangeListener(OnFocusChangeListener l) setPadding(int left, int right, int top, int bottom)
20
While
it is technically possible to create and attach widgets to your activity purely through Java code, the more common approach is to use an XML-based layout file. Dynamic instantiation of widgets is reserved for more complicated scenarios, where the widgets are not known at compile time (e.g., populating a column of radio buttons based on data retrieved from the Internet)
21
An XML-based layout is a specification of widgets relationships to each otherand to containersencoded in XML format. Android considers XML-based layouts to be resources, and as such, layout files are stored in the res/layout directory inside your Android project Each XML file contains a tree of elements specifying a layout of widgets and containers that make up one View. The attributes of the XML elements are properties, describing how a widget should look or how a container should behave. Androids SDK ships with a tool (aapt) that uses the layouts. Of particular importance to you as a developer is that aapt generates the R.java source file within your projects gen/ directory, allowing you to access layouts and widgets within those layouts directly from your Java code
22
Most
everything you do using XML layout files can be achieved through Java code. For example, you could use setTypeface() to have a button render its text in bold, instead of using a property in an XML layout. Dynamic instantiation of widgets is reserved for more complicated scenarios, where the widgets are not known at compile time. Using XML-based layout does have the advantage of helping to ease the transition to Android from any other XML-centered view description language.
23
The + after the @ in the id string indicates that the id should be automatically created as a resource if it does not already exist.
24
25
26
});
... }
27
protected void onCreate(Bundle savedValues) { ... // Capture our button from layout Button button = (Button)findViewById(R.id.corky); // Register the onClick listener with the implementation above button.setOnClickListener(mCorkyListener); ... }
. 28
29
Questions or Discussions
How
many kinds of resources are there in an Android Application? What are they? How are resources used? Why should we use string values in string.xml file rather than use them directly?
Should use tv.setText(R.string.app_name); Shouldnt use tv.setText("Hello, Android"); Why use xml-based layouts?
Ex:
30
http://developer.android.com/resources/faq/commontasks.html#fi
lelist
http://developer.android.com/resources/faq/commontasks.html#lo http://developer.android.com/resources/faq/commontasks.html#h
andle
http://developer.android.com/resources/faq/commontasks.html#li
31
The Android Developers Cookbook Building Applications with the Android SDK, James Steele,
Nelson To, Addison-Wesley Professional Publishing (2011) Android Application Overview, Chapter 2
32
Professional
Application
Android 2 Development,
Fundamental
Chapter 4
Android UI Design,
33
Beginning
Using
Android
3,
34