Friday, September 16, 2011

Android Programming - Hello World

Okay, the preparation phase is complete. You can now make Android Project. Let us start with the classic way in which a program Hello World. Let us immediately make, you need not know anything yet, we make first and I will explain later.
Example 1. Hello Android
Create a new project by accessing the File >> New >> Other >> Android >> Android Project.

Project name Hello Android
Target check one platform you have installed
Application name Hello Android
Package name com.capcay.helloandroid
Create Activity check the checkbox, the name "MainActivity"
Leave the others are. Click Finish and you've made ​​your first Android program. Congratulations!
Is this as easy? Of course, you are able to last and Run your project will get the output of the AVD like this (please wait loading AVD J).
Let me explain the structure of this android project. You can see on the left there is a Package Explorer. Expand all folders in your Android Project Halo. Appearance should be like this.
Src folder will contain the source code of your program. Because we just declare and use a single class then there would be only one file that is MainActivity.java. Can be seen that the source here in accordance with the concept of java class and contained in the package com.capcay.haloandroid. The contents of MainActivity:
package com.capcay.helloandroid;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
} 

Top line is a package declaration, it must be precisely written so as not to cause errors. Each command line in Java will end with ";" to signify its line-end. The next line that begins with the import is a process to import a library that we will use within that class.
Next is the creation of the component class MainActivity Android will enter the Activity groups that need to extends Activity (inherit / inheritance of class Activity). This class is then simply override (stacking / replace) the OnCreate method of the class Activity. The core of this code is to set the screen layout in accordance MainActivity declaration in files in a folder named main.xml layout.
Next is a file R.java, this is a magic file, therefore you do not even need to touch it and it will always be automatically updated by the SDK, if you change it manually will appear warning that the file R.java changed and will be returned as originally. This file lists every resource in your application, and about providing an integer id value for each resource, therefore we can use it as the file MainActivity had to call the appropriate layout file. If you are curious about the contents of this R.java file, you can open it and view its contents that there are many lists that hexadecimal code.
Android 2.2 folder contains the libraries that you can import into your program directly. Its contents will vary for each type of platform, because it is currently using the Android platform 2.2 Froyo the resource library will be in accordance with SDK 8.
Res folder handle placement and organization of resource files. There are lots of subfolders, at least at the beginning of the project made ​​no drawable folder-ldpi, hdpi and mdpi to separate resource file according to dpi (dots per inch), the usefulness of this separation of resource-based dpi for Android that will determine after knowing the dpi of the handset screen resource in the folder which will be used. Folder layout for placement of an XML file layout, here there are files that had been accessed by main.xml MainActivity, string folder also stores resource, here the origin of the words "Hello Android blah blah blah ..." which was contained in the emulator screen. Let's look at a file main.xml that make up the display as you see it. 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>  
Quite simply, this XML file begins with a declaration of his version and encoding type. It must exist in each file according to XM L. XML stands for eXtensible Markup Language that is actually providing flexibility for users to use their own tagnya, unlike HTML tag that already determined and has certain conventions, in XML we are free to use our own tag, Therefore Android chose XML as the language because it is easy to add markup tag with our own rules. Tag  linear layout and textview is a tag made ​​by programmers of Android OS for the layout definition. LinearLayout is one type of layouts are supported Android and TextView is similar to the label element in many other visual programming languages, ie text that can not be edited, just show what it is .
The first line on LinearLayout shows the XML schema used (all Android XML files using this scheme). Then the rules set its value orientation in the vertical screen. Then the width and height layout gives a chance fill_parent. Fill_parent means its value will follow the value of parentnya, while if its value wrap_content will follow the area occupied by the object in it. Therefore, in its height TextView element set wrap_content, to follow the text that will have high there. The last line tells Android TextView where to retrieve the value for the content of this element values ​​are in the folder, the file named string.xml and variable named hello. Try to open the file string.xml, you will see a variable declaration is also accompanied by hello there again that is one variable APP_NAME.
The last file which I will explain is AndroidManifest.xml (others yet to be discussed here). This file shows the structure of your program.
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.capcay.haloandroid"
      android:versionCode="1"
      android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-sdk android:minSdkVersion="8" /><application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".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>  
As always begins with the declaration of type XML and then to show that he was the manifest file is used as a tag parent tag manifest. We immediately see the tag application, here there will be a child - a child activity, receiver, or broadcast content resolver. In the Hello Android program there is only one Activity of MainActivity labeled with take a string APP_NAME. In Activity was determined Intent to run Activity Activity MainActivity as they were when the launcher program starts. And then stay close tags that exist for each XML file must be ensured wellformed, there can be no formatting errors in it.
Congratulations you can make your own Android programs, and already understand (should be :p) what should exist in the project file Android.

Categories: , , , , ,

0 komentar:

Copyright © Johannes Dwi Cahyo | Powered by Blogger

Design by Anders Noren | Blogger Theme by NewBloggerThemes.com | BTheme.net      Up ↑