Android API

API Reference:
http://developer.android.com/reference/android/app/Activity.html

Different approaches for setting up Android Development Environment

There are several options to prepare Android Development environment in Eclipse.

A) First Approach:
i) Install Eclipse and add ADT (Android Development Tool) plugin to it.
ii) Install Android SDK and configure it in Eclipse
Please Refere to : Installation of Android Development Tools (ADT)


B) Second Approach:
i) Download ADT Bundle (Android SDK + Eclipse) from below URL,
http://developer.android.com/sdk/installing/index.html

ii) Unzip the bundle and start Eclipse
[n this approach ADT is already available and not need to install any ADT plugin like (A) above]

How to import existing Android Project into the Eclipse

1) From Eclipse menu select (File/Import)

2) Select an import source to (Android/Existing Android Code Into Workspace)

3) Select project to be imported by select the top level project directory

What is adb (Android Debug Bridge) ?

Android Debug Bridge (adb) is a command line tool that allows to communicate with emulator instance or real connected handset.


# Using GridView in Android Application

  • Add GridView on the activity layout which will automatically adds XML GridView object in the related xml layout as shown below


  • Create Activity layout : layout/results.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FF234499"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/resultsGrid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:numColumns="5"
        android:background="#FF234499">
    </GridView>
</LinearLayout>
  • Create Adapter which is responsible for creating cells on the GUI 
public class TextAdapter extends BaseAdapter{
     private Context c;
     private String[] list;

     public TextAdapter(Context ctx) {
          this.c = ctx;
          list = new String[50];
         
          for (int i = 0; i < list.length; i++) {
               list[i] = "Cell #" + i;
          }
     }
    
     public int getCount() {
          return list.length;
     }

     public Object getItem(int arg0) {
          return null;
     }

     public long getItemId(int arg0) {
          return 0;
     }

     public View getView(int position, View convertView, ViewGroup parent) {
          final int p = position;
         
          TextView txtView = new TextView(c);
         
          if (position % 2 == 0) {
               txtView.setBackgroundColor(Color.BLACK);
          } else {
               txtView.setBackgroundColor(Color.WHITE);
          }
    
          //onclick enablement
          txtView.setOnClickListener(new View.OnClickListener() {
               public void onClick(View arg0) {
                    System.out.println("Postion clicked : " + p);
               }
          });
           
          txtView.setText(list[position]);
          return txtView;
     }
}
  • Create linkage between GridView and application code by adding below code
public class ResultsActivity extends Activity implements View.OnClickListener {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      //grid view
      GridView resultsGrid = (GridView) findViewById(R.id.resultsGrid);
      resultsGrid.setAdapter(new TextAdapter(this));
 }

#P2 Capital quiz on Android platform


Project Background
Develop a Quiz application to Display Country name and four capital options (out of one is correct). User could tap on any of the four options to select correct capital. Next question will be displayed once user selects correct answer.

Output
  
   

Project Structure in the Eclipse



























Source code 



package com.myapps.model;



/**

 * Model to hold information about each question and related four answer options,

 * out of which one is correct.

 * @author Santosh Lawoo

 * @version 1.2
 */
public class Question {
           private int index;
           private String description;
           private String answer;
           private String[] options;
          
          
           public int getIndex() {
                      return index;
           }
           public void setIndex(int index) {
                      this.index = index;
           }
           public String getDescription() {
                      return description;
           }
           public void setDescription(String description) {
                      this.description = description;
           }
           public String getAnswer() {
                      return answer;
           }
           public void setAnswer(String answer) {
                      this.answer = answer;
           }
           public String[] getOptions() {
                      return options;
           }
           public void setOptions(String[] options) {
                      this.options = options;
           }
}






-----------------------

#P1 Using Google Map in your Application

Pre-requisites :
a. ADT is already installed (Reference : Installation of Android Development Tools (ADT))
b. Google map key (Reference : How to generate google map key.html)
c. Google API (Install Google API if not already installed)
    (Google API = map api + android api)
Use Eclipse to install Google API as shown below


----------------------------------------------------------------------------------
Project Structure :


----------------------------------------------------------------------------------
Setting Emulator for Google Map Application :
- Select Target : "Google API" as shown below 
(In the Target, Google API option is available after installing Google API using Android SDK Manager in Eclipse) 

Steps to create Map based application :

1) Create Activity which extends MapActivity
package com.namasteandroid.mapapp;
import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class MyMapsActivity extends MapActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String mapKey = "0CJatumMJbC7I_ZPfoi5gQWZffw"; //Replace this key with yours 
        MapView mapView = new MapView(this, mapKey);
        mapView.setEnabled(true);
        mapView.setClickable(true);
        mapView.setBuiltInZoomControls(true);
        setContentView(mapView);
    }
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
----------------------------------------------------------------------------------
2) Modify Manifest (/res/AndrodManifest.xml) add below lines highlighted in orange
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.namasteandroid.mapapp"
    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:name=".MyMapsActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <uses-library android:name="com.google.android.maps" />
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

----------------------------------------------------------------------------------
3) Output

How to generate Google Map Key ?

1. Signup for Android Map API key (it allows to embed Google maps in Android App)

2. Use your Google account to finally generate Map Key

3. Generate your certificate's MD5 fingerprint
i)  Check for debug.keystore on your machine under [.android] directory
        Example on MacOS:
        /Users/santoshlg/.android/debug.keystore

ii) Check for the keytool location on your machine (JDK installation)  
        Example on Windows:
     C:\Program Files\Java\jdk1.6.0_21\bin\keytool
   
iii) Use below command to list contents of existing debug.keystore
            $keytool -list -keystore ~/.android/debug.keystore

     Enter keystore password: android
     Keystore type: JKS
     Keystore provider: SUN
     Your keystore contains 1 entry

     androiddebugkey, Jun 26, 2010, PrivateKeyEntry,
     Certificate fingerprint (MD5): 8A:B7:F5:D4:0F:F3:FA:31:E6:24:D6


iv) Access below URL and enter Certificate fingerprint (MD5) generated above 
           URL: http://code.google.com/android/maps-api-signup.html

v) Finally you will get page similar to below,
      

4. Use Map key (generated above) in your application code so that your application can use Google Map API (Important : Please go through the Google Policy for Using their API in your code)
i) Create Activity which extends MapActivity
import android.os.Bundle;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class MyMapsActivity extends MapActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        String mapKey = "0CXT_1IuVXHJUxJatumMJbC7I_Zw"
        MapView mapView = new MapView(this, mapKey);
        mapView.setEnabled(true);
        mapView.setClickable(true);
        mapView.setBuiltInZoomControls(true);
        setContentView(mapView);
        
    }

@Override
protected boolean isRouteDisplayed() {
return false;
}
}

For detail map application please refer to below blog post,
http://namaste-android.blogspot.jp/2012/04/using-google-map-in-your-application.html

Creating UI using XML and Java

Below are the two ways to create UI (User Interface)
------------------------------------------------------------------
1) Using XML (use below 2 files)

i) XML code (/res/layout/main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="UI Created by using XML layout (res/layout/main.xml)" 
        android:textColor="#FF11AA33"
     />
</LinearLayout>

ii) Java (/src/MyXmlLayoutActivity.java)
import android.app.Activity;
import android.os.Bundle;

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

Output


2) Using Java (use below 1 file)
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyJavaLayoutActivity extends Activity {

LinearLayout linear;
TextView text;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

linear = new LinearLayout(this);
linear.setOrientation(LinearLayout.VERTICAL);
text = new TextView(this);
text.setText("UI Created by Java Code without using /layout/main.xml");
text.setTextColor(Color.parseColor("#FF11AA33")); //#AARRGGBB format 
linear.addView(text);
setContentView(linear);
}
}

Output