Android Hello World instance [TODO]

Table of Contents

Android Hello World instance

Create Android App

Analysis of Android Applications

Main activity documents

Manifest file

Strings file

R file

Layout file

Running the application

Android Hello World instance

Let's start real Android framework based programming. Before you start writing the first example using the Android SDK, make sure you have followed the Android - environment building(https://rtoax.blog.csdn.net/article/details/104222012 )The tutorial describes the completion of your Android development environment. At the same time, I assume you have some knowledge of the Eclipse IDE.

Now let's start writing a simple Android app that prints out "Hello World.".

Create Android App

The first step is to create a simple Android application through the Eclipse IDE. Follow the options file - > New - > project, and finally select Android New Application from the wizard list. Now, use the following window wizard to name the application HelloWorld:

Next, follow the instructions provided to keep all the default input until the last step. Once the project is created successfully, you will see the following project interface-

Analysis of Android Applications

Before running the app, you need to know some file directories and files in the Android project-

Serial number Folders, files, and descriptions
1 src: contains all the. Java source files in the project. By default, it includes an activity class corresponding to the MainActivity.java source file. When the application is started through the application icon, it will run.
2 gen: This contains the. R file generated by the compiler, which references resources in all projects. The file cannot be modified.
3 bin: this folder contains the. apk package files that Android builds from APT, as well as everything else needed to run Android applications.
4 RES / drawable hdpi: this directory includes all the drawable objects needed for high-density screen design.
5 res/layout: this directory holds the files used to define the user interface.
6 res/values: this directory holds various XML files containing a series of resources, such as the definition of strings and colors.
7 Android manifest.xml: This is the manifest file of the application. It describes the basic features of the application and defines its various components.

The following sections give an overview of some important application files.

Main activity documents

The main activity code is in the Java file of MainActivity.java. This is the actual application file that will be converted to a Dalvik executable and run. Here is the default code generated by the application wizard for the Hello World application-

package com.example.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
   }
}

Here, r.layout.activity main refers to the activity main.xml file under the res/layout directory. onCreate() is one of the many methods called after an activity is loaded.

Manifest file

No matter what component you develop as part of the application, you need to declare all components in the manifest.xml file in the application project root directory. This file is the interface between Android operating system and your application, so if you do not declare your components in this file, they will not be recognized by the operating system. For example, a default listing file looks like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.helloworld"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="22" />

   <application
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >

       <activity
          android:name=".MainActivity"
          android:label="@string/title_activity_main" >

          <intent-filter>
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER"/>
          </intent-filter>

       </activity>

   </application>
</manifest>

Here,... Between tags are application related components. The andnroid:icon property indicates the application icon under the RES / drawable hdpi. This application uses a picture named ic_launcher.png under the drawable folder.

The tag is used to specify an Activity, and the android:name property specifies the full name of an Activity class subclass. The android:label property specifies the string used for the Activity name. You can use labels to specify multiple activities.

The action of the intent filter is named android.intent.action.MAIN, indicating that the activity is used as an entry to the application. The category of intent filter is named android.intent.category.LAUNCHER, indicating that the application can be started through the icon of device initiator.

@String refers to strings.xml (which will be covered later). Therefore, @ string/app_name refers to the app_name defined in strings.xml, which is actually "Hello World". Similarly, other strings in the application are also popular.

Here are the tags that will be used in your manifest file to specify different Android application components:

  • Active element
  • Service element
  • Broadcast receiver element
  • Content provider element

Strings file

The strings.xml file, under the res/value folder, contains all the text used by the application. For example, names of buttons, labels, default text, and other similar strings. This document is responsible for their textual content. A default strings file looks like this:

<resources>
   <string name="app_name">HelloWorld</string>
   <string name="hello_world">Hello world!</string>
   <string name="menu_settings">Settings</string>
   <string name="title_activity_main">MainActivity</string>
</resources>

R file

The gen/com.example.helloworld/R.java file is an active Java file, such as the glue between MainActivity.java and resources such as strings.xml. This is an auto generated file. Do not modify the contents of the R.java file. Here is an example of an R.java file:

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.example.helloworld;

public final class R {
   public static final class attr {
   }

   public static final class dimen {
      public static final int padding_large=0x7f040002;
      public static final int padding_medium=0x7f040001;
      public static final int padding_small=0x7f040000;
   }

   public static final class drawable {
      public static final int ic_action_search=0x7f020000;
      public static final int ic_launcher=0x7f020001;
   }

   public static final class id {
      public static final int menu_settings=0x7f080000;
   }

   public static final class layout {
      public static final int activity_main=0x7f030000;
   }

   public static final class menu {
      public static final int activity_main=0x7f070000;
   }

   public static final class string {
      public static final int app_name=0x7f050000;
      public static final int hello_world=0x7f050001;
      public static final int menu_settings=0x7f050002;
      public static final int title_activity_main=0x7f050003;
   }

   public static final class style {
      public static final int AppTheme=0x7f060000;
   }
}

Layout file

Activity main.xml is a layout file in the res/layout directory. Referenced when an application builds its interface. You will modify this file very often to change the layout of the application. In the "Hello World" application, this file has a default layout, as follows:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >

   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_centerVertical="true"
      android:padding="@dimen/padding_medium"
      android:text="@string/hello_world"
      tools:context=".MainActivity" />

</RelativeLayout>

This is a simple example of RelativeLayout, and more will be covered in separate chapters. TextView is an Android control for building user graphics interfaces. It contains many different properties, such as Android: layout ﹣ width, Android: layout ﹣ height, etc. to set its width and height. @String refers to the strings.xml file in the res/values folder. Therefore, @ string / hello'world refers to the string named Hello defined in strings.xml: "Hello World!".

Running the application

Let's try to run the Hello World! Application we just created. Let's say you've created the AVD when you build the environment. Run the application from Eclipse, open an active file in your project, and clickIcon. Eclipse installs the application on AVD and starts it. If all goes well, the following simulator window will be displayed-

Congratulations on developing the first Android application. Follow the rest of the tutorial step by step and you will become a great Android Developer.

 

Published 562 original articles, won praise 274, visited 480000+
His message board follow

Tags: Android xml Java Eclipse

Posted on Sat, 08 Feb 2020 03:06:45 -0500 by leon77