Saturday, November 12, 2011

Creating your First Android Application

From a .Net Developer's Perspective


I will be showing you how to create the same application that was shown in the tutorial titled 'Creating your First Mono for Android Application'. Over time, I will create more advanced Android application development tutorials using both the Mono for Android (using C#) and Eclipse Android Plugin (using Java) frameworks.


Setting Up the Development Environment
  1. Download and install the Java SE Development Kit (JDK):http://www.oracle.com/technetwork/java/javase/downloads/index.html I've installed JDK version 6 Update 27 (1.6.0.270).
  2. Download and install Eclipse IDE for Java EE Developers: http://www.eclipse.org/downloads/ I've installed version Indigo Service Release 1, build 20110916-0149.
    1. Extract the Eclipse files from the 'eclipse' folder into a folder on your PC. I've installed mine in C:\Eclipse.
  3. Install the Android SDK from http://developer.android.com/sdk/index.html. I installed Android SDK revision 15.
    1. Do not use any spaces in the installation path. I used C:\AndroidSDK.
    2. Leave the 'Start SDK Manager' checkbox checked.
    3. Ensure that the 'Android SDK Tools' and ''Android SDK Platform-tools' are fully installed.
    4. I also installed 'Android 2.2 (API 8)' and 'Android 2.3.3 (API 10)'.
    5. Click on the 'Install N packages...' when ready.
    6. Check the 'Accept all' radio button, then click on 'Install'. It will download and then install the packages. Please note that this download & installation process takes a long time to complete. Don't worry if the log shows an "'adb kill-server' failed -- run manually if necessary." error message. This is simply the package manager's attempt to stop the Android Debug Bridge's server process, which is not yet/already running.
    7. Click 'Yes' to restart ADB.
    8. Close the SDK manager.
  4. Configure the Android Simulator:
    1. Open up the Android SDK AVD Manager.
    2. Click on 'New...'.
    3. Enter MonoDroid in the 'Name' field.
    4. In the 'Target' drop-down, select 'Android 2.2 - API Level 8'.
    5. Choose 512MB for the 'SD Card' 'Size'.
    6. Select 'HVGA' as the 'Built-in' 'Skin'.
    7. In the 'Hardware' section, click on the 'New ...' button.
    8. Select the 'Device ram size' property from the drop-down list.
    9. Click on 'OK'.
    10. Enter 512 for the 'Device ram size' property.
    11. Click on 'Create AVD'. It will take about a minute to complete.
    12. Close out of all Android SDK windows.
  5. Install the Android Development Tools (ADT) Plugin for Eclipse: 
    1. Launch the 'eclipse.exe' file from the Eclipse folder.
    2. Specify your Workspace location. This is the folder in which Eclipse stores your Eclipse projects. I've set mine to C:\Documents and Settings\Mike\My Documents\Eclipse.
    3. Click on 'OK'.
    4. Click on the 'Help' menu, then click on 'Install New Software...'.
    5. Enter the following URL in the 'Work with:' text box: http://dl-ssl.google.com/android/eclipse
    6. Click on 'Add...'.
    7. Name the repository. I've called mine Android Plugin for Eclipse.
    8. Click on 'OK'.  Wait until you see the 'Developer Tools' checkbox in the list.
    9. Check the 'Developer Tools' checkbox.
    10. Click on 'Next'.
    11. Click on 'Next'.
    12. Check the license terms radio checkbox.
    13. Click on 'Finish'.
    14. Click on 'OK' when and if you see a 'Security Warning' dialog box regarding installing unsigned software.
    15. Click on 'Restart Now' to restart Eclipse.
    16. In the 'Welcome to Android Development' / 'Configure SDK' dialog box, check the radio checkbox labeled 'Use existing SDKs'.
    17. Browse to and select the location of the Android SDK installed previously. I've set my 'Existing Location' to C:\AndroidSDK.
    18. Click on 'Next'.
    19. I've opted not to send usage statistics to Google.
    20. Click on 'Finish'.
Creating the First, Hello World Android Application
  1. If it's not already open, launch the 'eclipse.exe' application.
  2. Click on the 'File' menu, then go to 'New' and click on 'Project'.
  3. Open up the 'Android' folder and select 'Android Project'.
  4. Click on 'Next'.
  5. In the 'Project Name' text box, enter HelloWorld.
  6. Click on 'Next'.
  7. Select the 'Android 2.2' SDK target.
  8. Click on 'Next'.
  9. In the 'Package Name' text box, change the name to net.DotNetFun.HelloWorld.
  10. In the 'Create Activity' text box, change the activity to MainActivity.
  11. Click on 'Finish'.
  12. Close the 'Welcome' tab in the upper-right corner.
  13. Rearrange the workspace so the panes are more appropriate spaced. This is what mine looks like:
  14. In the 'Package Explorer', expand the 'HelloWorld', then expand the 'res' and 'layout' folders.
  15. Double-click on the 'main.xml' file to open it. This file is equivalent to the Main.axml XML file used in the various Mono for Android Visual Studio project templates. It defines, declaratively, the layout for one or more activities. For more info about declaring XML layouts, visit this URL: http://developer.android.com/guide/topics/ui/declaring-layout.html
  16. Click on the 'main.xml' tab.
  17. Use the following XML as the 'main.xml' contents:

    <?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"
        >
    <Button  
        android:id="@+id/MyButton"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/Hello"
        />
    <Button
          android:id="@+id/DialogButton"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Dialog Button"
        />
    </LinearLayout>
    
    

    For more info about Android XML layouts, visit this URL: 

  18. In the 'Package Explorer', expand the 'values' folder.
  19. Double-click on the 'strings.xml' file to open it. This is equivalent to the the 'Strings.xml' file used in the various Mono for Android Visual Studio project templates. It defines, declaratively, string resources. One difference you'll notice, however, is the 'app_name' resource. The Eclipse project requires, by default, this resource exist. For more info about declaring string resources, visit this URL: http://developer.android.com/guide/topics/resources/string-resource.html
  20. Use the following XML as the 'strings.xml' contents:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="Hello">Hello World, Click Me!</string>
        <string name="app_name">DotNetFun.Android.HelloWorld</string>
        <string name="HelloButtonText">Button Text via Strings.xml</string>
    </resources>
    
    

    For more info about Android string resources, visit this URL: http://developer.android.com/guide/topics/resources/string-resource.html

  21. In the 'Package Explorer', expand the 'src', and 'net.DotNetFun.HelloWorld' folders.
  22. Double-click on the 'MainActivity.java' file to open it.
  23. Ensure that the java code in this file has exactly what you see here:

    package net.DotNetFun.HelloWorld;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.widget.*;
    import android.view.*;
    import android.view.View.OnClickListener;
    
    public class MainActivity extends Activity implements OnClickListener {
     
     private int count;
     
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            this.count = 0;
            
            Button button = (Button) findViewById(R.id.MyButton);
            
            button.setOnClickListener(this);
            
            Resources res = this.getResources();
            button.setText(res.getString(R.string.HelloButtonText));
            
            Button dialogButton = (Button) findViewById(R.id.DialogButton);
            dialogButton.setText("Dialog Button");
            
            dialogButton.setOnClickListener(this);
        }
        
        public void onClick(View v) {
         
         Button button = (Button) v;
         
      if (button.getId() == R.id.MyButton)
      {
       this.count++;
       button.setText(String.format("%s clicks!", this.count));
      }
      else if (button.getId() == R.id.DialogButton)
      {
       AlertDialog.Builder builder = new AlertDialog.Builder(this);
       builder.setMessage("Hello, World!");
       builder.setCancelable(false);
       builder.setPositiveButton("OK", null);
       AlertDialog dialog = builder.create();
       dialog.show();
      }
     }
    }
    
    
  24. Click on the 'Run' menu, then click on 'Debug Configurations...'.
  25. Click on the 'Target' tab.
  26. In the 'Select a preferred Android Virtual Device for deployment' list, select/check the 'MonoDroid' emulator created earlier.
  27. Click on 'Apply', then click on 'Close'.
  28. Press F11 to debug and start the Android application. If you get a dialog box stating it's waiting for the debugger to attach, and it doesn't go away after a while, you'll need to restart Eclipse, re-check your debug configuration, and press F11 again.
  29. Slide over the button to get to the Android desktop, then click on the applications icon/button.
  30. Click on the 'DotNetFun.Android.HelloWorld' application.
  31. Click on the 'Button Text via Strings.xml' button. A dialog box stating ''Hello, World!' should appear. Close it.
  32. Click on the 'Hello World, Click me!' button several times. With each click, the button's text/caption changes to the number of times it has been clicked.

6 comments:

facebook app said...

that is cool
and looking nice to me.
but it is also looking hard to learn these all steps.

Android app development said...

This is one of the knowledgeable post.I like your blog technique.This is one of the talented post.Your blog is looking attractive and good.

Ecommerce developer said...

Lot of useful points are there. Its really keeps me updated. Thanks for making a wonderful site.

Offshore Software Development Company said...

Awesome post! Interesting info to know.

Dot Net Experts

Latest Video Releases said...

I’m not that much of a online reader to be honest but your blogs really nice, keep it up! I'll go ahead and bookmark your site to come back later. Many thanks Effects Of Smoking.

rize wordpress said...

Android Development platform is based on Linux which makes it very stable for all kinds of web application and web-based application development.