Sunday, March 1, 2015

103-How To Create Android Project


.
103-Creating An Android Project
An Android project contains all the files that comprise the source code for your Android app. The Android SDK tools make it easy to start a new Android project with a set of default project directories and files.
This lesson shows how to create a new project either using  Android Developer Toolkit(ADT).

1) Click Menu File/New/Android Application Project




2) New Android Application options

Fill up the form based on the guide at http://developer.android.com/training/basics/firstapp/creating-project.html and then click Next.
The explanation as stated on the above website:
  • Application Name is the app name that appears to users. For this project, use "My First App."
  • Project Name is the name of your project directory and the name visible in Eclipse.
  • Package Name is the package namespace for your app (following the same rules as packages in the Java programming language). Your package name must be unique across all packages installed on the Android system. For this reason, it's generally best if you use a name that begins with the reverse domain name of your organizationor publisher entity. For this project, you can use something like "com.example.myfirstapp." However, you cannot publish your app on Google Play using the "com.example" namespace.
  • Minimum Required SDK is the lowest version of Android that your app supports, indicated using the API level. To support as many devices as possible, you should set this to the lowest version available that allows your app to provide its core feature set. If any feature of your app is possible only on newer versions of Android and it's not critical to the app's core feature set, you can enable the feature only when running on the versions that support it (as discussed in Supporting Different Platform Versions). Leave this set to the default value for this project.
  • Target SDK indicates the highest version of Android (also using the API level) with which you have tested with your application.
  • As new versions of Android become available, you should test your app on the new version and update this value to match the latest API level in order to take advantage of new platform features.
  • Compile With is the platform version against which you will compile your app. By default, this is set to the latest version of Android available in your SDK. (It should be Android 4.1 or greater; if you don't have such a version available, you must install one using the SDK Manager). You can still build your app to support older versions, but setting the build target to the latest version allows you to enable new features and optimize your app for a great user experience on the latest devices.
  • Theme specifies the Android UI style to apply for your app. You can leave this alone.

3) Project Configuration.

Accept Default.
Click Next.

4) Configure Launcher Options.

Accept Default.
Click Next.

5) Create Activity Option.

Accept Default (Tick Create Activity)
Click Next.
When we choose Create Activity, ADT will perform two tasks:
1) Create a Java Class for the Activity
2) Update Android Manifest file.
AndroidManifest.xml is a powerful file in the Android platform that allows you to describe the functionality and requirements of your application to Android.

6) New Blank Activity options.

Accept Default.
Click Finish.

7) Observe Package Explorer Panel.

Check each of the files.
7.1) MainActivity.Java
A typical Java file consists of three segment of codes:
a) The package declaration (to identify the project codes)
b) The import declaration (to use the existing code libraries)
c) The class instructions (programmer instructions)
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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.activity_main, menu);
        return true;
    }
   
}
Notice the statement setContentView(R.layout.activity_main);
It tells the apps to set its view based on the layout specified in the file activity_main (which is kept in the sub folder layout of the Resource folder).
7.2) activity_main.xml
(You may need to click the tab “activity_main.xml” in order to see the source codes)
<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"
    tools:context=".MainActivity" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />
</RelativeLayout>
7.3) AndroidManifest.xml
(You may need to click the tab “AndroidManifest.xml” in order to see the source codes)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.myfirstapp.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>
Notice that the AndroidManifest.xml actually contains the information that has been mostly entered in the project setup wizard above (Step 2 to Step 6).
Some keywords are preceded by the character @. These refer to the files in which values are stored.
Find the location of the following files/values:
@drawable/ic_launcher
@string/app_name
@style/AppTheme
@string/app_name

DOWNLOAD


.

No comments:

Post a Comment