Hello guys, Today I am going to explain simple example in Android.
Hello World Example:
The first step is to create a simple Android Application using Eclipse IDE. Follow the option 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:
Click on Next and follow the instructions which are provided and keep all other entries as default till the final step. Once your project is created successfully, you will have following project screen.
Now You need to know about your project structure before you run your application.
1. src: This contains the .java files for your project. By default, it includes an MainActivity.java which you gives during created a new project. This java file having an activity class that runs when your app is launched using the app icon.
2. gen: This contains the .R file, Its a auto generated by compiler that references all the resources found in your project. You should not modify this file.
3. bin: This folder contains the Android package files .apk built by the ADT during the build process and other files like Android Manifest, classes, cache file, resource file and so on.
4. res/drawable-hdpi: This directory used for drawable objects ( Images, Icons etc ) that are designed for high-density screens. Other directories are also used for same purpose but the density are different.
5. res/layout: This folder is used for create a user interface for your application. In simple meaning you can design your screens in xml file using this folder.
6. res/values: This directory have various XML files that contain a collection of resources, such as strings, colors, attributes, dimensions, styles and so on.
7. AndroidManifest.xml: The manifest file which describes the fundamental characteristics of the app and defines each of its components. User can set the application version code, application's minimum version, screen orientation, required permissions, required services and many more.
Now the turn of coding part.
As I told you before you can see the MainActivity.java file like following:
package com.example.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
Code of activity_main.xml:
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_gravity="center"/>
</LinearLayout>
Code of AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<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:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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>
Now you can run your application. You will get following output in your virtual emulator.
Enjoy with example...:-)
Hello World Example:
The first step is to create a simple Android Application using Eclipse IDE. Follow the option 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:
Click on Next and follow the instructions which are provided and keep all other entries as default till the final step. Once your project is created successfully, you will have following project screen.
Now You need to know about your project structure before you run your application.
1. src: This contains the .java files for your project. By default, it includes an MainActivity.java which you gives during created a new project. This java file having an activity class that runs when your app is launched using the app icon.
2. gen: This contains the .R file, Its a auto generated by compiler that references all the resources found in your project. You should not modify this file.
3. bin: This folder contains the Android package files .apk built by the ADT during the build process and other files like Android Manifest, classes, cache file, resource file and so on.
4. res/drawable-hdpi: This directory used for drawable objects ( Images, Icons etc ) that are designed for high-density screens. Other directories are also used for same purpose but the density are different.
5. res/layout: This folder is used for create a user interface for your application. In simple meaning you can design your screens in xml file using this folder.
6. res/values: This directory have various XML files that contain a collection of resources, such as strings, colors, attributes, dimensions, styles and so on.
7. AndroidManifest.xml: The manifest file which describes the fundamental characteristics of the app and defines each of its components. User can set the application version code, application's minimum version, screen orientation, required permissions, required services and many more.
Now the turn of coding part.
As I told you before you can see the MainActivity.java file like following:
package com.example.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
Code of activity_main.xml:
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_gravity="center"/>
</LinearLayout>
Code of AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<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:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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>
Enjoy with example...:-)
No comments:
Post a Comment