Sunday 31 August 2014

DAY 3 : CHANGE UI ELEMENTS AND CREATE AVD

Hello World with EditText input 

 In this exercise, I will implement a simply Hello World Andrid application; there is a EditText, after user enter name and click OK button, the Title of the application will be changed accordingly.

create new project and give Name of it,as shown in Previous exercise.

Modify the main.xml to change the UI screen:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Who you are?"
/>
<EditText
android:id = "@+id/message_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id = "@+id/ok"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="OK" />
<Button
android:id = "@+id/cancel"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Cancel" />
</LinearLayout>

And then, change the HelloAndroid.java as follow:
HelloAndroid.java


package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


       Button okButton = (Button) findViewById(R.id.ok);
       okButton.setOnClickListener(okOnClickListener);
       Button cancelButton = (Button) findViewById(R.id.cancel);
       cancelButton.setOnClickListener(cancelOnClickListener);
}
   private Button.OnClickListener okOnClickListener =
             new Button.OnClickListener(){
       @Override
       public void onClick(View v) {
           EditText edit_text = (EditText)
                findViewById(R.id.message_text);
           CharSequence edit_text_value =
                edit_text.getText();
           setTitle("Hello:"+edit_text_value);
       }
   };

   private Button.OnClickListener cancelOnClickListener =
           new Button.OnClickListener(){
       @Override
       public void onClick(View v) {
           finish();
       }
   };
}

now Its time to run this package  >>>


Running on the emulator


Before you can run your application on the Android Emulator, you must create an AVD.

 The AVD Manager is an easy to use user interface to manage your AVD (Android Virtual Device) configurations. An AVD is a device configuration for the Android emulator that allows you to model different configurations of Android-powered devices. When you start the AVD Manager in Eclipse or navigate to your SDK's tools/ directory and execute android avd. You will see the AVD Manager as shown below in figure. 
Creating AVD:
You can create as many AVDs as you would like to test on. It is recommended that you test your applications on all API levels higher than the target API level for your application.
To create an AVD:

  1. Start the AVD Manager:
    • In Eclipse: select Window > AVD Manager, or click the AVD Manager icon in the Eclipse toolbar.
    • In other IDEs: Navigate to your SDK's tools/ directory and execute the android tool with no arguments.
  2. In the Virtual Devices panel, you'll see a list of existing AVDs. Click New to create a new AVD. The Create New AVD dialog appears.

    1. Fill in the details for the AVD. Give it a name, a platform target, an SD card size, and a skin (HVGA is default). You can also add specific hardware features of the emulated device by clicking the New... button and selecting the feature. For a list of hardware features.
      Note: Be sure to define a target for your AVD that satisfies your application's Build Target (the AVD platform target must have an API Level equal to or greater than the API Level that your application compiles against).
    2. Click Create AVD.

To run (or debug) your application, select Run > Run (or Run > Debug) from the Eclipse menu bar. The ADT plugin will automatically create a default run configuration for the project. Eclipse will then perform the following:

  1. Compile the project (if there have been changes since the last build).
  2. Create a default run configuration (if one does not already exist for the project).
  3. Install and start the application on an emulator (or device), based on the Deployment Target defined by the run configuration.
     
    If you run the application with the Debug option, the application will start in the "Waiting For Debugger" mode. Once the debugger is attached, Eclipse opens the Debug perspective and starts the application's main activity. Otherwise, if you run the application with the normal Run option, Eclipse installs the application on the device and launches the main activity.   
    Your AVD is now ready and you can either close the AVD Manager, create more AVDs, or launch an emulator with the AVD by selecting a device and clicking Start.
    • then Right click on your  project (@project explorer)
    and then click on RUN AS -> ANDROID APPLICATION ...
    It will be appear after a few time in the AVD which you have created.

DAY 2 : START YOUR FIRST APP WITH ECLIPSE

Create Android Application

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:


Next, follow the instructions provided and keep all other entries as default till the final step. Once your project is created successfully, you will have following project screen:



Anatomy of Android Application

Before you run your app, you should be aware of a few directories and files in the Android project:

S.N.Folder, File & Description
1src
This contains the .java source files for your project. By default, it includes an MainActivity.java source file having an activity class that runs when your app is launched using the app icon.
2gen
This contains the .R file, a compiler-generated file that references all the resources found in your project. You should not modify this file.
3bin
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.
4res/drawable-hdpi
This is a directory for drawable objects that are designed for high-density screens.
5res/layout
This is a directory for files that define your app's user interface.
6res/values
This is a directory for other various XML files that contain a collection of resources, such as strings and colors definitions.
7AndroidManifest.xml
This is the manifest file which describes the fundamental characteristics of the app and defines each of its components.
 

Following section will give a brief overview few of the important application files.

The Main Activity File

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. Following is the default code generated by the application wizard for Hello World! application:

package com.example.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

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;
    }
}
 
 
Here, R.layout.activity_main refers to the activity_main.xml file located in the res/layout folder. The onCreate() method is one of many methods that are fi red when an activity is loaded.

The Manifest File

Whatever component you develop as a part of your application, you must declare all its components in a manifest file called AndroidManifest.xml which ressides 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="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>
 
 
 
Here <application>...</application> tags enclosed the components related to the application. Attribute android:icon will point to the application icon available under res/drawable-hdpi. The application uses the image named ic_launcher.png located in the drawable folders
The <activity> tag is used to specify an activity and android:name attribute specifies the fully qualified class name of the Activity subclass and the android:label attributes specifies a string to use as the label for the activity. You can specify multiple activities using <activity> tags.
The action for the intent filter is named android.intent.action.MAIN to indicate that this activity serves as the entry point for the application. The category for the intent-filter is named android.intent.category.LAUNCHER to indicate that the application can be launched from the device's launcher icon.
The @string refers to the strings.xml file explained below. Hence, @string/app_name refers to the app_name string defined in the strings.xml fi le, which is "HelloWorld". Similar way, other strings get populated in the application.
Following is the list of tags which you will use in your manifest file to specify different Android application components:
  • <activity>elements for activities
  • <service> elements for services
  • <receiver> elements for broadcast receivers
  • <provider> elements for content providers

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>

The R File

The gen/com.example.helloworld/R.java file is the glue between the activity Java files like MainActivity.java and the resources like strings.xml. It is an automatically generated file and you should not modify the content of the R.java file. Following is a sample of R.java file:
 
/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.example.helloworld;

public final class R {
    public static final class attr {
    }
    public static final class dimen {
        public static final int padding_large=0x7f040002;
        public static final int padding_medium=0x7f040001;
        public static final int padding_small=0x7f040000;
    }
    public static final class drawable {
        public static final int ic_action_search=0x7f020000;
        public static final int ic_launcher=0x7f020001;
    }
    public static final class id {
        public static final int menu_settings=0x7f080000;
    }
    public static final class layout {
        public static final int activity_main=0x7f030000;
    }
    public static final class menu {
        public static final int activity_main=0x7f070000;
    }
    public static final class string {
        public static final int app_name=0x7f050000;
        public static final int hello_world=0x7f050001;
        public static final int menu_settings=0x7f050002;
        public static final int title_activity_main=0x7f050003;
    }
    public static final class style {
        public static final int AppTheme=0x7f060000;
    }
}

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. You will modify this file very frequently to change the layout of your application. For your "Hello World!" application, this file will have following content related to default layout:

<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 which we will study in a separate chapter. The TextView is an Android control used to build the GUI and it have various attribuites like android:layout_width, android:layout_height etc which are being used to set its width and height etc. The @string refers to the strings.xml file located in the res/values folder. Hence, @string/hello_world refers to the hello string defined in the strings.xml fi le, which is "Hello World!".

Running the Application

Let's try to run our Hello World! application we just created. I assume you had created your AVD while doing environment setup. To run the app from Eclipse, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Eclipse installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window:


 Congratulations!!! you have developed your first Android Application and
 now just keep following rest of the tutorial step by step to become a 
great Android Developer. All the very best.



Saturday 30 August 2014

DAY 1 : ANDROID INTRODUCTION

HELLO BUDDS!!!

WELCOME TO ANDROID WORKSHOP!!!

I'M SURE THAT , YOU WILL LEARN MUCH FROM HERE...
 
SPENT 1-2 HOUR DAILY HERE TO LEARN ANDROID 

 HAVE A NICE JOURNEY !!!

  • In this pages you will learn : 
  • 1.what is android 
  • 2.How to install Android Platform(Eclipse)

 

What is Android?

  • The Android platform is a software stack for mobile devices including an operating system, middleware and key applications. Developers can create applications for the platform using the Android SDK. Applications are written using the Java programming language and run on Dalvik, a custom virtual machine designed for embedded use which runs on top of a Linux kernel.

  • An early look at the the Android SDK is also available. It includes sample projects with source code, development tools, an emulator, and of course all the libraries you'll need to build an Android application. 

      

    BEFORE STARTING ANDROID FIRST YOU HAVE TO DOWNLOAD SOMETHING:

    1. ANDROID DEVELOPMENT KIT (INCLUDES ....)
    • Eclipse + ADT plugin
    • Android SDK Tools
    • Android Platform-tools
    • A version of the Android platform
    • A version of the Android system image for the emulator

     

    Install Eclipse


    Eclipse is a free Open Source IDE, it can be download from http://www.eclipse.org/ or the download page here.

    The first package, Eclipse IDE (Integrated Development Environment) for Java Developers, contains what you need to build Java applications.

    Click the selected package to involve the mirror selection page, select your closest mirror site, start download and save it to any position on your machine you want.

    After download, un-zip it on any position you want. It's the installed folder, "C:\App\eclipse\" on my setup.

    When you run the application "C:\App\eclipse\eclipse.exe", you will be asked to select the workspace, it's "C:\App\eclipse\workspace" for me.

    Remark: JDK is assumed to be installed before install Eclipse.


    Android Emulator in Action

    Android Emulator is included in Android SDK. It can be involved inside Eclipse IDE, or run as a standalone application.
 

Install Android SDK and Eclipse's Android Development Tools plugin 

In order to setup Eclipse for development of Android, both Install Android SDK and Eclipse's Android Development Tools plugin are needed.

Download Android SDK - ADT (WHICH YOU DOWNLOADED ABOVE)and unzip it in any folder you want, it's "C:\App\android_sdk_windows_m3-rc22a" for me.

Android Development Tools plugin can be download inside Eclipse IDE.

  1. Start Eclipse, then select Help > Software Updates > Find and Install....
  2. In the dialog that appears, select Search for new features to install and press Next.
  3. Press New Remote Site.
  4. In the resulting dialog box, enter a name for the remote site (e.g. Android Plugin) and enter this as its URL:
    https://dl-ssl.google.com/android/eclipse/
    Press OK.
  5. You should now see the new site added to the search list (and checked). Press Finish.
  6. In the subsequent Search Results dialog box, select the checkbox for Android Plugin > Eclipse Integration > Android Development Tools and press Next.
  7. Read the license agreement and then select Accept terms of the license agreement, if appropriate. Press Next.
  8. Press Finish.
  9. The ADT plugin is not signed; you can accept the installation anyway by pressing Install All.
  10. Restart Eclipse.
  11. After restart, update your Eclipse preferences to point to the SDK directory:
    1. Select Window > Preferences... to open the Preferences panel. (Mac OS X: Eclipse > Preferences)
    2. Select Android from the left panel.
    3. For the SDK Location in the main panel, press Browse... and locate the SDK directory.
    4. Press Apply, then OK.

DON'T BE SELFISH !!! SHARE IT !!!