Saturday, May 18, 2013
Google Android Programming Tutorial
Main Class
When developers build Android projects using the Eclipse Integrated Development Environment, which is the official IDE for the platform, Eclipse automates certain parts of the process. For example, when you create a new Android project, it automatically creates a main class, which is an Activity class. An Activity class represents a single screen within the app, containing visual elements and user interface controls. The main Activity class is launched when users run your app. The following sample code demonstrates a typical main Activity class outline:public class MyApp extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}}
Activities
Developers can create an Activity for each screen in an app. The outline for each Activity is similar, extending the Activity super-class and providing an 'onCreate' method. Inside 'onCreate,' a developer can implement any processing he wants to take place when the Activity is launched. This normally involves setting the Activity layout:setContentView(R.layout.help);This instructs Android to use the XML layout contained in a file named 'help.xml' which is saved inside the 'res/layout' folder in the application package. Each Activity must also appear in the app's 'AndroidManifest.xml' file:
This reflects a Java Activity class file named 'Help' for demonstration.
Intents
When a developer creates Activity files in Android apps, he can create instances of those classes from other points within the project. For example, if you want an Activity to be launched by pressing a button in another Activity, you can add the code inside the launching Activity class file, specifying the details of the Activity to launch using Intents:startActivity(new Intent(this, Help.class));This starts the Activity saved as 'Help' in the application package. The 'startActivity' method can appear in any other Activity class file. For example, you may wish to launch an Activity with helpful information about the app from other screens within it.
Layouts
Each Activity class in an Android app may be associated with a layout. A developer can define layouts in Java code, but the official resources for Android recommend defining them as XML files. The application package for an Android app has a 'res/layout' directory, in which a developer can include these XML layouts. The XML code can include user interface and visual items, as well as specifications for how these should be laid out relative to one another:
android:layout_width='fill_parent'
android:layout_height='fill_parent'
android:orientation='vertical' >
android:layout_width='wrap_content'
android:layout_height='wrap_content'
android:text='Here is some text' />
This sample layout has a single user interface item in it which is a text-field.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment