Implementation of "hello World" in Android Development

Source: Internet
Author: User

By convention, we also want to better guide readers into the wonderful Android World. Next we will implement a simple "hello World" example. Android SDK 2.1 is used as the basis for Eclipse Galileo (Eclipse 3.5) development.
Use "File"> "New"> "Project" to create an Android Project, as shown in figure 1. Set "Project name:" to "helloWorld", "Build Target" to "Android 2.1", "Application name:" to "helloWorld", and "Package name: "is" com. miaozl. helloWorld, set "Create Activity:" to "helloWorld ". Click "Finish" to create the project.

Figure 1 create an Android Project
It should be noted that, in the Android design, android 1.0 SDK version is "1", Android 1.1 SDK version is "2", Android 1.5 SDK version is "3", Android 1.5 SDK version is "3 ", the SDK version of Android 1.6 is "4", the SDK version of Android 2.0 is "5", the SDK version of Android 2.0.1 is "6", and the SDK version of Android 2.1 is "7 ", the SDK version of Android 2.2 is "8", the SDK version of Android 2.3 is "9", and the SDK version of Android 3.0 is "Honeycomb ". When selecting a specific SDK version, you should select a version as small as possible based on technical implementation feasibility, which is conducive to expanding compatibility with physical devices.
In the process of creating the Android project. Due to Eclipse, the system may prompt "no classfiles specified, Conversion to Dalvik format failed with error 1". If this error message is displayed, press F5 to refresh the project. It should be noted that after Foryo, this problem has rarely occurred. The copied project may cause engineering attribute problems, you can choose "Android Tools"> "Fix Project Properties" from the right-click menu of the Android Project to Fix the problem.
In addition, if this is the first time you create a project, Eclipse will prompt you to "Failed to find an AVD compatible with target ". This is because Android does not provide the default Android Virtual Device (AVD, Android Virtual Device) in a later version and needs to be created by the user. In Linux, AVD-related files include userdata. img image files, which are stored in the/root/. android directory by default. AVD describes the device configuration information of the current simulator. After the project is created, the Android project layout is displayed in the "Package Explorer" of Eclipse. Eclipse maintains projects under its jurisdiction in the/root/workspace/. metedata directory by default. If you want to get a clean running environment, simply delete the/root/. android and/root/workspace/. metedata directories.
2 shows the layout of the generated Android "helloWorld" project, where ". "settings" describes several information about Eclipse and the JDT package used. ". assets is an empty directory that describes the asserted information of the project. assertions are not commonly used. A common usage is to maintain some project resource files, but assertions have a size limit of 1 MB for a single file.
"Bin" is the output file directory, contains the CLASS bytecode file corresponding to each source file, the "ap _" format resource file, the DEX bytecode file, and the Android installation package in APK format that is finally packaged using the "aapt" tool. It must be noted that the Android installation package in APK format is essentially a ZIP package, which contains resources, DEX bytecode files, and AndroidManifest. xml. When the system detects an APK file, the system treats it as an application.
"Gen" is the resource data directory. Contains all the resource data discovered by the "aapt" tool in the project.
"Res" is the XML file directory describing resources. It usually includes "drawable", "layout", "values", and other sub-directories. It describes the icons, strings, and la s involved in the project, complex applications also involve directories such as "menu", "color", and "style. The layout resource file is main. xml, and the string resource file is strings. xml.
"Src" is the source file directory of the project and contains the Java files created by the developer.
The ". classpath" file describes the paths involved in the project.
The ". project" file describes the project information, such as the project name and required compilation commands.
"AndroidManifest. xml describes project implementation details such as "manifest", "application", "activity", "intent-filter", "action", "category", and SDK version information., application permissions, etc, is the most important file in the Android project.
"Default. properties" describes the SDK version.

Figure 2 Android Project Layout
For native code-based applications, the "jni" directory may exist in the project to store native source code, and the "libs" directory to store dynamic shared libraries generated by compiling native source code.
The following are the implementation details of the main file:
1. src \ com \ miaozl \ helloWorld. java
Code 1 is helloWorld of the "hello World" project. the content of the java file defines an Activity named "helloWorld" in the file, and loads the layout resource file of the Activity through the setContentView () function.
Code 1 helloWorld. java
Package com. miaozl. helloWorld;
Import android. app. Activity;
Import android. OS. Bundle;
Public class helloWorld extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState); // create an Activity
SetContentView (R. layout. main); // load the layout Resource
}
}
The setContentView () function can also directly load the View class. View is used to build the UI control system of the application, including the List (Lists), GRID (Grids), and Text box (Text Boxs) and Buttons (Buttons. Figure 3 shows the class diagram of the View subsystem of the Android system. The android. view. View class is the basis class of the UI control, and android. view. ViewGroup is the base class of the layout class.

Figure 3 View sub-system class diagram
Activity is closely related to the window manager. The View subsystem is the basis of the entire Android UI framework.
2. res \ layout \ main. xml
Code 2 is the main of the "helloWorld" project. xml file, which defines the layout attribute of the Activity, defines the root layout of the Activity as LinearLayout, and sets the Activity as a vertical full screen window, and defines a TextView control.
Code 2 main. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = http://schemas.android.com/apk/res/android // defines the root layout and xmlns namespace
Android: orientation = "vertical" // The Activity is vertical.
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent" // same width as the parent object
Android: layout_height = "wrap_content" // set the width based on the Content
Android: text = "@ string/hello" // sets the display string
/>
</LinearLayout>
In Android, the layout manager determines the layout of the UI interface in the Activity. The UI layout can be implemented based on XML files or code. The XML file-based method is mainly used for the layout of different screen rotations, different display resolutions, and the default layout of different localization languages, which is conducive to decoupling the behavior and display of applications, it helps you change the layout of an application without modifying the Code. The Code-based method focuses on modifying the layout in real time, and the performance is slightly superior, however, it is not conducive to application localization and adaptation to different hardware devices. Especially as the target environment of the tablet computer is introduced, the hardware environment becomes more complex.
In the XML layout file, a root element of the View type and ViewGroup type must exist (the root element in the main. xml file is LinearLayout ). You can add subsequent layout objects or UI controls (such as TextView in the main. xml file) only after defining the root element ).
It should be noted that for the UI control, its "android: layout_width" and "android: in higher versions, layout_height has three attribute values: "fill_parent", "match_parent", and "wrap_content. The "fill_parent" and "match_parent" attribute values are defined as "-1 ". The actual meaning is the same. The attribute value of "match_parent" has a clearer literal meaning and is recommended by Google.
If the layout files are slightly different in different screen directions or in hardware configuration and Language Environments, you can place the layout files with the same name under different layout folders, for example, the layout folder in the simplified Chinese environment is "layout-zh-rCN ".
When building layout files, it will naturally involve pixel issues. In Android, there are several units such as dp, px, and dip, where dip is independent of physical devices, it is the pixel unit designed by Android to adapt to a variety of different resolutions. The most commonly used is dip.
Note that the dip settings are irrelevant to the resolution, but related to the screen density (density). By default, the QVGA density is 120, the coefficient is 0.75, And the HVGA density is 160, the coefficient is 1.0, the density of WVGA is 240, and the coefficient is 1.5. Currently, Android supports xhdpi, hdpi, mdpi, and ldpi. Hdpi is usually applicable to high-resolution smart terminals, while mdpi is usually used for high-resolution tablets. Ldpi applies to entry-level smart terminals. In addition, the xhdpi concept is introduced in Foryo. Its density is defined as 320, which is suitable for high-end smart terminals with high split screens, android has clearly defined the size of its menu icons and application icons. For the density of hdpi, the size of its menu icon and application icon is 72*72. For the density of mdpi, the size of its menu icon and application icon is 48*48, for ldpi density, the size of the menu icon and application icon is 36*36.
There is also a nodpi concept that needs to be noted, which is designed to avoid pixel scaling of image resources.
The relationship between px and dip is as follows:
Px = (int) (dip * density + 0.5f) // a dip under hdpi, equivalent to 1.5 physical pixels
In actual implementation, screen density is called scale. The conversion process of px and dip is as follows:
Public void scale (float scale ){
X = (int) (x * scale + 0.5f );
Y = (int) (y * scale + 0.5f );
If (width> 0 ){
Width = (int) (width * scale + 0.5f );
}
If (height> 0 ){
Height = (int) (height * scale + 0.5f );
}
}
In addition, the unit involved in the description font is sp. In actual development, many beginners can easily use these units randomly. This may cause many potential problems.
When setting the filling of the control, you must note that Android provides two filling types: inner filling and outer filling. For inner filling, the space occupied by the filling is part of the control, filling out space is part of the parent control of the control.
The filling property is set as follows:
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
Android: paddingTop = "5dip"/> // fill 5 dip in the top of the control
The attribute setting method of the field filling is as follows:
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
Android: layout_marginTop = "5dip"/> // fill 5 dip in the top of the control
There are a lot of skills for developers to figure out how to make the layout file more flexible and adapt to multiple devices. I hope readers will learn more.
3. res \ values \ strings. xml
Code 3 is the strings. xml file of the "helloWorld" project. In this file, the string resources involved in the Activity are defined. For software localization, Android uses the ISO naming convention to set the Resource Directory names in different languages. For Simplified Chinese, the Resource Directory is "values-zh-rCN". For traditional Chinese, the Resource Directory is "values-zh-rTW ".
Code 3 strings. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, helloWorld! </String> // "hello" string
<String name = "app_name"> helloWorld </string> // "app_name" string
</Resources>
In more complex cases, you may encounter a string array, which is defined as follows:
<String-array name = "imAddressTypes">
<Item> Home </item>
<Item> Work </item>
<Item> Other </item>
<Item> Custom </item>
</String-array>
Other types of arrays include color arrays, image arrays, and integer arrays. For details about how to define them, see the frameworks/base/core/res/values/arrays. xml file.
4. \ AndroidManifest. xml
Code 4 is the AndroidManifest. xml file of the "helloWorld" project. This file is unique for each application.
The AndroidManifest. xml file defines the activity, intent, uses-sdk, uses-permission, service, uses-library, Content provider, Broadcast explorer, and other information of the application.
Code 4 AndroidManifest. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. miaozl. helloWorld" // package name
Android: versionCode = "1" // version number, which must be an integer to determine whether to upgrade or not
Android: versionName = "1.0"> // version name
<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
<Activity android: name = ". helloWorld" // define activity
Android: label = "@ string/app_name">
<Intent-filter> // defines the intent filter.
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>
<Uses-sdk android: minSdkVersion = "7"/> // defines the Android version used.
</Manifest>
In the "uses-sdk" element, developers can specify three Android versions based on the application situation. The android: minSdkVersion attribute describes the minimum Android version required for normal operation of the application. The default value is "1". The android: maxSdkVersion attribute describes the highest Android version required for normal operation of the application, if this attribute is not declared, the system assumes that the default value is infinitely large. The android: targetSdkVersion attribute describes the best Android version information for the normal operation of the application, and declares the android: targetSdkVersion attribute, allows applications to call specific platforms, rather than limited to the platform support capability of the lowest version.
In addition to the Activity, in actual development, the constructed service, receiver, provider, and uses-library) and so on must be in AndroidManifest. declared in xml. Otherwise, an exception is thrown. Services and providers can be called by other applications.
The "android: versionCode" attribute defines the current version number of the application. It can be used with a digital signature certificate to upgrade the application. The "android: versionName" attribute usually specifies the version information displayed to the end user.
In Foryo, Android adds the "android: installLocation" attribute to allow developers to specify the application installation location. Its values include: "auto", "internalOnly", and "preferExternal ". This facilitates the market expansion of entry-level Android terminals.

 

This article is from the "embedded" blog

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.