Android Application Development
Hi all. I welcome you all to my blog.
Let us start to code our first Android application.
To start with.,
Open Eclipse -> Project -> Android Application Project
In the screen that appears choose an Application Name, Project Name and Package Name. Choose a Target API and minimum SDK.
- The application name is shown in the Play Store, as well as in the Manage Application list in the Settings.
- The Project name is only used by Eclipse, but must be unique within the workspace. This can be typically the name of the application.
- The Package name must be a unique identifier of your project. It is typically not shown to the users, but it “must” stay the same for the lifetime of the application; it is how multiple versions of the same application are considered the “same app”.
- Choose a Target API to Compile the Android code
- For minimum required SDK, select the lowest version of Android that your application will support. Lower API level targets more devices.
Click Next to move to next screen
Screen 2: In this screen you can configure launcher icon, by selecting the requiredimage based on DPI
Click Next to move to next screen
Screen 3: This screen is used to create a layout activity for ur designs.
Screen4:
In this screen give the Activity name and layout name you wish to have(Make it relevant to the activity for understanding what class is used for what) and click Finish to complete the setup.
Once you click on Finish the IDE opens with DIRECTORY structure as shown below for your reference:
As you can see the directory structure has many folders and files.
src folder : This folder contains java class files that we use in the project.
res folder : This folder contains all drawables(hdpi,ldpi,mdpi,xdpi classified based on pixel density of images), Layouts (Contains all out xml files that is used for our User Interface Designs) that we will be using in our project.
AndroidManifest.xml file : This is file that is like a heart of an Android Application. We declare all activities created in out project.
- Manifest file for an android application is a resource file which contains all the details needed by the android system about the application.
- It is a key file that works as a bridge between the android developer and the android platform.
- It helps the developer to pass on functionality and requirements of our application to Android.
- This is an xml file which must be named as AndroidManifest.xml and placed at application root.
- Every Android app must have AndroidManifest.xml file.
Android Manifest files will contain the following elements:
<action>
<activity>
<activity-alias>
<application>
<category>
<data>
<grant-uri-permission>
<instrumentation>
<intent-filter>
<manifest>
<meta-data>
<permission>
<permission-group>
<permission-tree>
<provider>
<receiver>
<service>
<supports-screens>
<uses-configuration>
<uses-feature>
<uses-library>
<uses-permission>
<uses-sdk>
A sample Android Manifest file for your reference:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javapapers.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".HelloWorld" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Create layout design for our application.
- Open activity_main.xml(default)
- You have 2 views in this xml editor screen., such as., Graphical layout or xml code layout. Use can switch between the layout views to see your design and code
- Use the tools needed for ur design that is available in Palette pane in Graphical layout.
Once design is done now its time to code our activity. So open MainActivity.java from src folder in project hierarchy.
The above image shows the sample of how our MainActivity.java looks like. You can write your java code here.
Example Program:
Display you a welcome message on clicking a button.
Activity_main.xml:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/welcomebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Click me" />
</RelativeLayout>
MainActivity.java:
package com.example.welcome;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button hi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hi = (Button) findViewById(R.id.welcomebtn);
hi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Hi!!! ... Welocme you all for your first Andorid Application",
Toast.LENGTH_LONG).show();
}
});
}
@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;
}
}
Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.welcome"
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.welcome.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>
Compiling the Android Application:
After u have done the code:
Click on Run–>Run as Android Application.
As i mentioned Before u run, if u have created AVD ., else go back to my earlier post and know how to make AVD's.
Enjoy your first app on ur emulator and install the .apk file from bin folder in your Project Hierarchy.
Happy Coding...
Be Good Do Good...
