Android---1---HelloWorld

Source: Internet
Author: User

Start creating your first Android project: HelloWorld

In almost all languages, the first project that was written at the beginning of the study was: HelloWorld, so how do you write your first Android project?

First, in the MyEclipse compiler, in the navigation bar, click FIle, New, other, Android, Android application Project (Figure 1)

The dialog box for creating an Android project pops up. (Fig. 2)



Where application name represents the app name, and the app is installed on the phone

The name is displayed on the phone, and here we fill in the HelloWorld. Project name, which represents the item, is created after the project

The name is displayed in Eclipse, where we fill in the HelloWorld (the project name usually does not add a space). Then

The package name represents the name of the project, and the Android system distinguishes the application by its package name.

Next is a few drop-down boxes, Minimum Required SDK is the least compatible version of the program, my minimum version is 4.4,

Target SDK means that you have already done enough testing on this target version, and the system will not be able to help you do forward-compatible operations on this version.

Compile with refers to which version of the SDK the program will use to compile

After that, it's OK to go crazy next. The first HelloWorld project was created.

Right-click on Android application, HelloWorld, Run as

Figure 3


The Android simulator is needed here and a simulator is recommended BlueStacks This simulator needs to be used with the "Force assistant".

Now let's analyze This Android app:

Expand HelloWorld This project we will see the directory structure shown in 4.


which

1.src

This directory is where all the Java code is stored.

2.gen

The contents of this directory are automatically generated, there is a R.java file, and any resources added to the project will generate a corresponding resource ID in it. This file should never be manually modified.

3.assets

This directory is not used much, the main can be stored in a number of files packaged with the program, you can dynamically read the contents of these files when your program runs. In addition, if you use the WebView to load the local Web page, all the pages related files are also stored in this directory.

4.bin

This directory you do not need to pay much attention to, it mainly contains some of the files generated automatically at compile time. which

There will be a build package for your current project, expand the bin directory you will see HELLOWORLD.APK, put this

The file is copied to the phone and can be installed directly.

5.libs

If you use a third-party jar package in your project, you will need to place the jar packages in the Libs directory and

The Jar packages in this directory will be automatically added to the build path.

6.res

The contents of this directory is a bit more, simply say, you use in the project all the pictures, cloth

The resources of the Bureau, string, etc. are to be stored in this directory, the above mentioned R.java content is also according to this goal

Recorded files are automatically generated. Of course, there are many subdirectories under this directory, and the images are placed in the drawable directory.

Layouts are placed in the layout directory, strings are placed in the values directory, so you don't have to worry about putting the entire res directory

Make a mess.

7. Androidmanifest.xml

This is the configuration file for your entire Android project, and all four components you define in the program need to be in this

Files are registered in a file. You can also add a permission statement to the application in this file, or you can reassign your

The minimum and target version of the program specified when the project was created. Since this file is often used later, we

When you use it, do a detailed explanation.

8. Project.Properties

This is a very simple file that specifies the version of the SDK that is used to compile the program through a single line of code.

Next we look at the code:


Package Com.example.helloword;import Android.app.activity;import Android.os.bundle;import android.view.Menu;import Android.view.menuitem;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);}}

Helloworldactivity is inherited from the Activity. Activity is an Android system that

For an activity base class, all activities in our project must inherit it to have the properties of the activity. Then you can

See there are two methods in Helloworldactivity, Oncreateoptionsmenu () This method is used to create the menu,

We can ignore it first, mainly look at the OnCreate () method. The OnCreate () method is an activity that is created when it must be

The method of the line. There are only two lines of code, and there is no HelloWorld word, then we run the display of the HelloWorld where ...

Let's open the layout under RES and see Activity_main.xml open it, figure 5,1 is the layout file graphical interface 2 is the layout file code interface


Let's look at the code:


<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" fill_parent "    android:layout_height=" Fill_parent    " android:orientation= "vertical"    android:paddingbottom= "@dimen/activity_vertical_margin"    android: paddingleft= "@dimen/activity_horizontal_margin"    android:paddingright= "@dimen/activity_horizontal_margin"    android:paddingtop= "@dimen/activity_vertical_margin"    tools:context= " Com.example.helloword.MainActivity ">    <textview        android:layout_width=" Wrap_content "        Android : layout_height= "wrap_content"        android:text= "@string/hello_world"/></linearlayout>

Where LinearLayout is a form of layout, called linear layout,android:orientation="vertical" This sentence indicates that the layout is vertical.

TextView is a component that is used primarily to display a text message on the interface.

android:layout_width="Wrap_content" Specifies the width of the control

android:layout_height="Wrap_content" Specifies the height of the control

Android:text="@string/hello_world" Specifies that the text information displayed by the control is displayed under string named hello_ what the world tag stands for

And then we'll open the stirngs.xml under values.


<?xml version= "1.0" encoding= "Utf-8"?><resources>    <string name= "App_name" >helloword</ string>    <string name= "Hello_world" >hello world!</string>    <string name= "Action_settings" >Settings</string></resources>

At this point, the first project is complete.



Android---1---HelloWorld

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.