HelloWorld Application in Android

June 10, 2012
By

10. About AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="15" />

    <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>

This xml file defines all the information related with the application.

  • <manifest> : This is the root element of xml file
    • xmlns : This attribute defines the namespace
    • package : Defines the package name of this application. Package name should be unique for the applications. So in order to avoid the duplication, the package name will be comprised of Uri in the reverse order.
    • android:versionCode : version code is an integer number used up to upgrade or to downgrade the applications.
    • android:versionName : versionName¬† is a string shown to user to inform about the version of the application. Usually, versionCode and versionName are different.
  • <uses-sdk> : This element defines the minimum Android SDK level required to run this application.
  • <application> : The information regarding the application contents are defined using this element
    • android:icon : This attribute defines the icon for this application in the application launcher.
      • @drawable¬† : Represents the directories /res/drawable-ldpi, /res/drawable-hdpi, /res/drawable-mdpi, /res/drawable-xhdpi.
        • drawable-ldpi : Devices with low density will use the images of this directory
        • drawablel-hdpi : Devices with high density will use the images of this directory
        • drawable-mdpi : Devices with medium density will use the images of this directory
        • drawtable-xhdpi : Devices with extreme high density will use the images of this directory
      • ic_launcher : Specifies the image file name stored in the drawable directories. In android:icon attribute, the image extension will not be specified
    • android:label : This attribute defines the title of the application in the launcher.
      • @string : Represents the file /res/values/strings.xml
      • app_name : A <string> element defined in strings.xml, whose name attribute is “app_name”
    • <activity> : An <activity> element represents an activity of the application. An activity represents an user interface screen of the application. An application can have one or more activities.
      • android:name : Specifies the class name of the Activity. The activity classes are stored in /src/ directory
      • android:label: Specifies the title of the activity
      • <intent-filter> : Intent filter defines the capabilities of the parent activity.
        • <action> : The name of the action, using which , other part of the application or other part of the operating system communicates with this activity. The action name “android.intent.action.MAIN” is a built in action, specifies that the parent activity is the main activity or this is the first activity to be executed when launching this application.
        • <category> : Specifies , to which category, this activity belongs. The category “android.intent.category.DEFAULT” specifies that, this¬† activity can be launched from the operating system launcher

11. About strings.xml

<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>

The strings.xml defines some predefined text to be used in the various parts of the application. Each text is enclosed in <string> element and is uniquely identified by its name.


12. About activity_main.xml file

<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:id="@+id/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
</RelativeLayout>

This main.xml is the layout file which defines the contents of the user interface screen. The root element of this xml file is <LinearLayout> which defines the layout of the screen. Linear layout aligns one widget ( eg : textview, textbox, button etc ) in a row or in a column. The horizontal or vertical alignment is defined by the attribute android:orientation.

The child of the <LinearLayout> element is <TextView> element which displays “HelloWorld” in the screen. The textview content is specified by the attribute android:text.


13. About MainActivity.java

package in.wptrafficanalyzer.helloworld;

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

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;
    }
}
  • This is the MainActivity class, and the method onCreate() will be executed when the application is launching
  • The function call setContentView(R.layout.activity_main) which sets the layout of this activity as activity_main.xml stored in /res/layout directory



14. Reference

http://developer.android.com/guide/index.html


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Pages: 1 2

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner