Android - Hello World Example
Be make sure
that you have setup your Android development environment properly. if you have done it go ahead.
Create Android Application
Create a simple Android Application using Eclipse IDE. Follow File -> New -> Project and finally select Android New Application wizard from the wizard list. Now name your application as HelloWorld using the wizard window as follows:
follow the instructions and keep all other entries as default till the final step. Once your project is created successfully.

i. src:
This contains the .java source files for your project. By default, it includes an MainActivity.java.
ii. gen: This contains the .R file, a compiler-generated file that references all the resources found in your project. You should not modify this file.
iii. bin: This folder contains the Android package files .apk built by the ADT during the build process and everything else needed to run an Android application.
iv. res/drawable(hdpi/mdpi/xhdpi etc): these are directories for drawable objects that are designed for different density screens.
v. res/layout: This is a directory for files that define your app's user interface.
vi. res/values: This is a directory for other various XML files that contain a collection of resources, such as strings and colors definitions.
vii. AndroidManifest.xml: This is the manifest file which describes the fundamental characteristics of the app and defines each of its components.
HelloWorld Project is discussed below.
MainActivity.java
The main activity code is a Java file MainActivity.java. This is the actual application file which ultimately gets converted to a Dalvik executable and runs your application.
Here, R.layout.activity_main refers to the activity_main.xml file located in the res/layout folder. The onCreate() method is a method which is called when an activity is loaded. all these methods will be discussed in activity life cycle tutorials.
follow the instructions and keep all other entries as default till the final step. Once your project is created successfully.
After creating project you will get different directories of your project.
i. src:
This contains the .java source files for your project. By default, it includes an MainActivity.java.
ii. gen: This contains the .R file, a compiler-generated file that references all the resources found in your project. You should not modify this file.
iii. bin: This folder contains the Android package files .apk built by the ADT during the build process and everything else needed to run an Android application.
iv. res/drawable(hdpi/mdpi/xhdpi etc): these are directories for drawable objects that are designed for different density screens.
v. res/layout: This is a directory for files that define your app's user interface.
vi. res/values: This is a directory for other various XML files that contain a collection of resources, such as strings and colors definitions.
vii. AndroidManifest.xml: This is the manifest file which describes the fundamental characteristics of the app and defines each of its components.
HelloWorld Project is discussed below.
MainActivity.java
The main activity code is a Java file MainActivity.java. This is the actual application file which ultimately gets converted to a Dalvik executable and runs your application.
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
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;
}
}
The Layout File
The activity_main.xml is a layout file available in res/layout directory, that is referenced by your application when building its interface.
<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>
<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 an example of simple RelativeLayout .The TextView is used for displaying "Hello World!". all these UI related things will be in future tutorials.
The Strings File
The strings.xml file is located in the res/values folder and it contains all the text that your application uses. For example, the names of buttons, labels, default text, and similar types of strings go into this file. This file is responsible for their textual content. For example, a default strings file will look like as following file −
<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>
<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 Manifest File
Whatever component you develop as a part of your application, you must declare all its components in amanifest.xml which resides at the root of the application project directory. This file works as an interface between Android OS and your application, so if you do not declare your component in this file, then it will not be considered by the OS. For example, a default manifest file will look like as following file −
<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>
<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>
the manifest will be discussed in next tutorial.
nice tutorial
ReplyDeletethnx lizee
Delete