"HelloAndroid.java" - First Android Java Code

This section provides a tutorial example on how to review and modify the first Android Java code, HelloAndroid.java. The modification is to print out the 'Hello Android' text on mobile devices.

Before generating the project binary file, I reviewed and modified the Java source code in .\src\com\herongyang\HelloAndroid.java to print out the "Hello Android" text on the mobile device:

/* HelloAndroid.java
 * Copyright (c) 2015, HerongYang.com, All Rights Reserved.
 */
package com.herongyang;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      TextView tv = new TextView(this);
      tv.setText("Hello, Android");
      setContentView(tv);
   }
}

The modification is based on the example code provided in the "Hello, World" tutorial in the Android documentation. The following explanations are copied from the documentation:

An Android user interface is composed of hierarchies of objects called Views. A View is a drawable object used as an element in your UI layout, such as a button, image, or (in this case) a text label. Each of these objects is a subclass of the View class and the subclass that handles text is TextView.

In this change, you create a TextView with the class constructor, which accepts an Android Context instance as its parameter. A Context is a handle to the system; it provides services like resolving resources, obtaining access to databases and preferences, and so on. The Activity class inherits from Context, and because your HelloAndroid class is a subclass of Activity, it is also a Context. So, you can pass this as your Context reference to the TextView.

Next, you define the text content with setText().

Finally, you pass the TextView to setContentView() in order to display it as the content for the Activity UI. If your Activity doesn't call this method, then no UI is present and the system will display a blank screen.

See next tutorial on how to generate the project binary file.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows System

 Installation of Android SDK R24 and Emulator

 Installing Apache Ant 1.9 on Windows System

Developing First Android Application - HelloAndroid

 Creating Android Project with "android" Command

 Listing Target Android Platforms

 "HelloAndroid" - First Android Project

"HelloAndroid.java" - First Android Java Code

 "ant debug" Command and Build Error

 Building the Debug Binary Package

 Installing the Debug Binary Package

 Running the Debug Binary Package

 Android Application Package (APK) Files

 Android Debug Bridge (adb) Tool

 Android File Systems

 AboutAndroid - Application to Retrieve System Information

 android.app.Activity Class and Activity Lifecycle

 View Objects and Layout Resource Files

 Using "adb logcat" Command for Debugging

 Build Process and Package File Content

 Building Your Own Web Browser

 Android Command Line Shell

 Samsung Galaxy Tab 3 Mini Tablet

 USB Debugging Applications on Samsung Tablet

 Android Tablet - LG-V905R

 USB Debugging Applications on LG-V905R Tablet

 Android Phone - LG-P925g

 USB Debugging Applications on LG-P925g Phone

 Archived Tutorials

 References

 Full Version in PDF/EPUB