Rabu, 01 Oktober 2014

How To Make Android App Hello World By Java Coding

In my previous post, we talk about how to instantly make an android application even without knowing the coding. But we have to know the code or the application structure, haven't we?
So, let's move on to the next step. :)

Let's continue from the previous example.
Expand the "src" node. Expand the "com.example.helloandroid" package node. Open the "MainActivity.java", and replace it with the following codes:


 
 
package com.example.helloandroid;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
 
public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textView = new TextView(this);   // Construct a TextView UI component
        textView.setText("Hello, from my code!"); // Set the text message for TextView
        setContentView(textView);  // this Activity sets its content to the TextView
    }
}
Run the application by right-clicking the project node ⇒ "Run As" ⇒ "Android Application". You shall see the message "Hello, from my code!".

Dissecting the "MainActivity.java" - Application, Activity & View
An Android application could have one or more Activity.
An Activity, which usually has a screen, is a single, focused thing that the user can interact with the application (hence called activity). The MainActivity extends the android.app.Activity class, and overrides the onCreate() method. The onCreate() is a call-back method, which is called by the Android system when the activity is launched.
A View is a UI component (or widget, or control). We construct a TextView (which is a subclass of android.view.View), and set its text message. We then set the content-view of the MainActivity screen to this TextView.
Android Application Structure
An Android project consists of these folders:
  • src: Java Source codes. The Java classes must be kept in a proper package with at least two levels of identifiers (by default, com.example.projectname for test project).
  • res: Resources, including drawable (e.g., images and icons), layout (UI components and layout), values (e.g., locale strings).
  • asset: where you store raw files (e.g., configuration, audio and image files).
  • gen: Generated Java codes.
  • bin: Compiled bytecodes (in sub-directory classes), and the ".apk" (Android Package Archive file).
  • AndroidManifest.xml: The manifest to describe this app, such as its activities and services.
  • default.properties: holds various settings for the build system, updated by the ADT.
  • Android 4.4: the build target platform, with link to Android API ("android.jar").

Android Application Descriptor File - "AndroidManifest.xml"
Each Android application has a manifest file named AndroidManifest.xml in the project's root directory. It describes the application.
For example, our "HelloAndroid" application, with an activity MainActivity, has the following manifest (generated automatically by the Eclipse ADT):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloandroid"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.helloandroid.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
  • The <manifest> element specifies the package name, version-code and version-name. The version-code is an integer uses by the Android Market to keep track of new versions, which is usually a running number starts at 1. The version-name is a string for your own identification.
  • The <manifest> contains one <application> element.
  • The <application> element specifies the icon, label (the application's title) and theme of this application. It contains one ore more <activity> elements.
  • This application has one activity. The <activity> element declares its program name ("MainActivity" in package com.example.helloandroid); and label (the activity's screen title). It may contain <intent-filter>.
  • The <intent-filter> declares that this activity is the entry point (android.intent.action.MAIN) of the application. This activity is to be added to the application launcher (android.intent.category.LAUNCHER).
In the Android Application, we are not only having Java but we have XML too as layout manager. Interesting isn't? So we can write Hello World in XML too.
Wanna try out XML coding? ofcourse you have. Let's check for my next post about it.

0 komentar:

Posting Komentar