Hello World Program for Android phones

Source: Internet
Author: User

Abstract: After configuring the development environment, this article will introduce how to develop the first Hello World Program for Android phones. This article will introduce two methods, one is to develop through code, it is developed by configuring the xml file describing Layout. The latter is actually the control design interface in Visual Studio, but it is not convenient to use Visual Studio. This is because Eclipse and Android sacrifice ease of use, however, it provides good cross-platform performance and result.

Step 1, Create a project through Eclipse, select the Android project type, and click the next button:

Step 2, Enter the project name, package name, and select "Build Target" as Android2.3.3. Here, you can select the Target platform for compiling the project as needed, an enterprise-level application I have experienced is Anroid1.6, which aims to be compatible with more mobile phones. Click Finish to help create project-related files by using Eclipse:

Step 3The project files automatically created by Eclipse are displayed. Details about the files will be provided later. The HelloworldActivity files and Activity) is a main type of Android applications, readers can refer to the blog (http://www.cnblogs.com/johnsonwong/archive/2011/08/08/2128012.html) compiled from IBM related articles ),

Step 4An application can have multiple activities. A user can only interact with one Activity at a time. When a user accesses the Activity, the Android operating system automatically calls the onCreate () event, add the following code in this method:

Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
TextView TV = new TextView (this );
TV. setText ("Hello, Android ");
SetContentView (TV );
}

To use the TextView class, you need to import android. widget. TextView. A quick way to introduce this class is to use the Ctrl + Shift + O key combination;

Step 5Then you can run the application. Before running the application, you need to configure a simulator. Use the Android SDK and AVD Manager sub-menu under the Eclipse Window Menu:

In the displayed window, select "New" to create a New simulator. In the displayed window, set the name of the simulator and select the target platform for testing, here we select "Andorid 2.3.3-API Level 10" to keep the default values unchanged:

Step 6, Select the project, select Run As, and then select 1 Android Application. Depending on the simulator, it may take several minutes to start:

The execution result is as follows:

Code-based UI development is cumbersome and difficult to control. Eclipse also supports a visual UI Designer. UI image elements are described in XML files, the default description file is in main under the/res/layout folder. xml:

Main. the xml file supports two editing modes. The visual designer can directly edit the file through XML. The following figure shows the visual designer, for friends who are used to Visual Studio, this design is slightly cumbersome and ugly, but as mentioned above, the sacrifice of convenience brings about compatibility with more platforms:

Step 1Then, you can directly edit the main. XML file in the xml editor and replace the content in the main. xml file with the following content:

<? Xml version = "1.0" encoding = "UTF-8"?>
<TextView xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/textview"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: text = "@ string/hello"/>

Step 2, Open string under/res/values. xml file, which also provides two editing modes: design mode or XML direct editing mode. Modify the following hello key values in the XML editing mode, this value is used by the preceding layout file main. xml File Usage:

? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, this is my first Android Application </string>
<String name = "app_name"> Helloworld </string>
</Resources>

Step 3Open the HelloworldActivity. java class file and modify the content as follows:

Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}

Here, we use the setContentView method. the settings in the xml file are applied to the current Activity. To achieve this, Eclipse uses the dynamically generated R. java class, which stores references to layout files:

Package com. johnson;

Public final class R {
Public static final class attr {
}
Public static final class drawable {
Public static final int icon = 0x7f020000;
}
Public static final class layout {
Public static final int main = 0x7f030000;
}
Public static final class string {
Public static final int app_name = 0x7f040001;
Public static final int hello = 0x7f040000;
}
}

You can see that the reference to main is in the static class layout, and press Ctrl + F11 to run the program:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.