Friday, November 27, 2009

Display a marker on MapView, using Overlays.

Expends from last exercise, AndroidMapper. A marker is shown on the current location using Overlays, if MapView started with location, Mode 1.



To use Overlays, a class of InterestingLocations have to be created, extends ItemizedOverlay.

It involve modification on AndroidMapView.java

package com.AndroidMapper;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class AndroidMapView extends MapActivity {

private TextView myLongitude, myLatitude;
private CheckBox mySatellite;

private MapView myMapView;
private MapController myMapController;

private void SetSatellite()
{
myMapView.setSatellite(mySatellite.isChecked());
};

@Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.mymapview);

Bundle bundle = this.getIntent().getExtras();
int Mode = bundle.getInt("Mode");

myMapView = (MapView)findViewById(R.id.mapview);
myMapController = myMapView.getController();
myMapView.setBuiltInZoomControls(true);

myLongitude = (TextView)findViewById(R.id.longitude);
myLatitude = (TextView)findViewById(R.id.latitude);
mySatellite = (CheckBox)findViewById(R.id.satellite);
mySatellite.setOnClickListener(mySatelliteOnClickListener);

SetSatellite();


if(Mode == 1)
{
int intLatitude = bundle.getInt("Latitude");
int intLongitude = bundle.getInt("Longitude");
GeoPoint initGeoPoint = new GeoPoint(intLatitude, intLongitude);
CenterLocation(initGeoPoint);

Drawable marker=getResources().getDrawable(
android.R.drawable.ic_menu_myplaces);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
myMapView.getOverlays().add(new InterestingLocations(marker,
intLatitude, intLongitude));
}
}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}

private void CenterLocation(GeoPoint centerGeoPoint)
{
myMapController.animateTo(centerGeoPoint);

myLongitude.setText("Longitude: "+
String.valueOf((float)centerGeoPoint.getLongitudeE6()/1000000)
);
myLatitude.setText("Latitude: "+
String.valueOf((float)centerGeoPoint.getLatitudeE6()/1000000)
);
};

private CheckBox.OnClickListener mySatelliteOnClickListener =
new CheckBox.OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub
SetSatellite();
}
};

class InterestingLocations extends ItemizedOverlay<OverlayItem>{

private List<OverlayItem> locations =
new ArrayList<OverlayItem>();
private Drawable marker;

public InterestingLocations(Drawable defaultMarker,
int LatitudeE6, int LongitudeE6) {
super(defaultMarker);
// TODO Auto-generated constructor stub
this.marker=defaultMarker;
// create locations of interest
GeoPoint myPlace = new GeoPoint(LatitudeE6,LongitudeE6);
locations.add(new OverlayItem(myPlace ,
"My Place", "My Place"));
populate();
}

@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return locations.get(i);
}

@Override
public int size() {
// TODO Auto-generated method stub
return locations.size();
}

@Override
public void draw(Canvas canvas, MapView mapView,
boolean shadow) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);

boundCenterBottom(marker);
}
}
}



Download the files.

Monday, November 23, 2009

Exercise AndroidMapper, Android Application using MapView

In the previous exercises, the MapView track the GPS and center on it repeatly. In this exercise, AndroidMapper, it will not track on GPS. Instead, there are three options to center in starting of the MapView.
- Default: Start ViewMap without any center location.
- GPS: It's the current GPS (which will be track before ViewMap start). If no valid GPS, this option will be disable.
- Location: User input location. If no location input, this option will be disable.



After ViewMap loaded, user can move the Map, Zoom-in/out using MapView's BuiltInZoomControls.



This application is not yet finished, more feature (or bug fixed) will be added in the furture.

Create a Android Application named, AndroidMapper.
Package Name: com.AndroidMapper
Target Google Platform 2.0 with Google APIs.

Modify main.xml to have the UI as seen in the picture.
<?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"
/>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/option_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Default Location" />
<RadioButton
android:id="@+id/option_gps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GPS:" />
<RadioButton
android:id="@+id/option_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location:" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="latitude"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/latitude"
android:inputType="numberDecimal"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="longitude"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/longitude"
android:inputType="numberDecimal"
/>
<Button
android:id="@+id/loadmap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Load Map"
/>
</LinearLayout>


Modify AndroidMapper.java
package com.AndroidMapper;

import com.google.android.maps.GeoPoint;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class AndroidMapper extends Activity {

private LocationManager myLocationManager;
private LocationListener myLocationListener;
private GeoPoint GeoPoint_GPS, GeoPoint_Location;

RadioButton myoption_default, myoption_gps, myoption_location;
EditText mylatitude, mylongitude;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

loadMenu();
validGPS();
validLocation();

myLocationManager = (LocationManager)getSystemService(
Context.LOCATION_SERVICE);

myLocationListener = new MyLocationListener();

myLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
myLocationListener);
}

private void loadMenu()
{
setContentView(R.layout.main);
myoption_default = (RadioButton)findViewById(R.id.option_default);
myoption_gps = (RadioButton)findViewById(R.id.option_gps);
myoption_location = (RadioButton)findViewById(R.id.option_location);
mylatitude = (EditText)findViewById(R.id.latitude);
mylongitude = (EditText)findViewById(R.id.longitude);

mylatitude.setOnKeyListener(locationOnKeyListener);
mylongitude.setOnKeyListener(locationOnKeyListener);

Button myLoadMapButton = (Button)findViewById(R.id.loadmap);
myLoadMapButton.setOnClickListener(myLoadMapButtonOnClickListener);
}

Button.OnClickListener myLoadMapButtonOnClickListener =
new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(myoption_default.isChecked()){
OpenIntentAndroidMapView(null);
}
else if(myoption_gps.isChecked()){
OpenIntentAndroidMapView(GeoPoint_GPS);
}
else if(myoption_location.isChecked()){
OpenIntentAndroidMapView(GeoPoint_Location);
}
else{
OpenMissingOptionDialog();
}

}

};

private void OpenIntentAndroidMapView(GeoPoint startLocation)
{
Intent intent = new Intent();
intent.setClass(AndroidMapper.this, AndroidMapView.class);

Bundle bundle = new Bundle();

if (startLocation == null)
{
bundle.putInt("Mode", 0);
}
else
{
bundle.putInt("Mode", 1);
bundle.putInt("Longitude", startLocation.getLongitudeE6());
bundle.putInt("Latitude", startLocation.getLatitudeE6());
}

intent.putExtras(bundle);
startActivityForResult(intent, 0);
}

private void OpenMissingOptionDialog()
{
new AlertDialog.Builder(this)
.setTitle("missing selection")
.setMessage("Please select one of the option")
.setPositiveButton("OK",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{}
})
.show();
}

EditText.OnKeyListener locationOnKeyListener =
new EditText.OnKeyListener(){

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
validLocation();
return false;
}

};

private void validGPS()
{
GeoPoint_GPS = loadGPS();
if (GeoPoint_GPS==null)
{
myoption_gps.setClickable(false);
}
else
{
myoption_gps.setText("GPS: (" +
String.valueOf((float)GeoPoint_GPS.getLatitudeE6()/1000000) +" : " +
String.valueOf((float)GeoPoint_GPS.getLongitudeE6()/1000000) +")");
myoption_gps.setClickable(true);
}
}

private void validLocation()
{
/*
Toast.makeText(AndroidMapper.this,
mylatitude.getText().toString(),
Toast.LENGTH_LONG).show();*/
if (mylatitude.getText().toString().equals("") || mylongitude.getText().toString().equals(""))
{

myoption_location.setText("Location: ");
myoption_location.setClickable(false);
myoption_location.setChecked(false);
}
else
{
float locationLatitude = Float.parseFloat(mylatitude.getText().toString());
float locationLongitude = Float.parseFloat(mylongitude.getText().toString());

myoption_location.setText("Location: (" +
String.valueOf(locationLatitude) +" : " +
String.valueOf(locationLongitude) +")");
myoption_location.setClickable(true);
myoption_location.setChecked(true);

GeoPoint_Location = new GeoPoint(
(int)(locationLatitude*1000000),
(int)(locationLongitude*1000000));
}
}

private GeoPoint loadGPS()
{
//Get the current location from GPS
myLocationManager = (LocationManager)getSystemService(
Context.LOCATION_SERVICE);
Location initLocation=myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(initLocation != null)
{
return (
new GeoPoint(
(int)(initLocation.getLatitude()*1000000),
(int)(initLocation.getLongitude()*1000000)));
}
else
return null;
}

private class MyLocationListener implements LocationListener{

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
GeoPoint_GPS = new GeoPoint(
(int)(location.getLatitude()*1000000),
(int)(location.getLongitude()*1000000));
myoption_gps.setText("GPS: (" +
String.valueOf((float)GeoPoint_GPS.getLatitudeE6()/1000000) +" : " +
String.valueOf((float)GeoPoint_GPS.getLongitudeE6()/1000000) +")");
myoption_gps.setClickable(true);
}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

}
}


Modify mymapview.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"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<CheckBox
android:id="@+id/satellite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Satellite "
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/longitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Longitude:"
/>
<TextView
android:id="@+id/latitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Latitude:"
/>
</LinearLayout>
</LinearLayout>
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="-----Your Own API Key here-------------"
/>
</LinearLayout>
You have to Obtaining a Maps API Key, and insert into the field "apikey"

Modify AndroidMapView.java
package com.AndroidMapper;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

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

public class AndroidMapView extends MapActivity {

private TextView myLongitude, myLatitude;
private CheckBox mySatellite;

private MapView myMapView;
private MapController myMapController;

private void SetSatellite()
{
myMapView.setSatellite(mySatellite.isChecked());
};

@Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.mymapview);

Bundle bundle = this.getIntent().getExtras();
int Mode = bundle.getInt("Mode");

myMapView = (MapView)findViewById(R.id.mapview);
myMapController = myMapView.getController();
myMapView.setBuiltInZoomControls(true);

myLongitude = (TextView)findViewById(R.id.longitude);
myLatitude = (TextView)findViewById(R.id.latitude);
mySatellite = (CheckBox)findViewById(R.id.satellite);
mySatellite.setOnClickListener(mySatelliteOnClickListener);

SetSatellite();


if(Mode == 1)
{
int intLatitude = bundle.getInt("Latitude");
int intLongitude = bundle.getInt("Longitude");
GeoPoint initGeoPoint = new GeoPoint(intLatitude, intLongitude);
CenterLocation(initGeoPoint);
}


}



@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}

private void CenterLocation(GeoPoint centerGeoPoint)
{
myMapController.animateTo(centerGeoPoint);


myLongitude.setText("Longitude: "+
String.valueOf((float)centerGeoPoint.getLongitudeE6()/1000000)
);
myLatitude.setText("Latitude: "+
String.valueOf((float)centerGeoPoint.getLatitudeE6()/1000000)
);
};

private CheckBox.OnClickListener mySatelliteOnClickListener =
new CheckBox.OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub
SetSatellite();
}
};

}


Also modify AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidMapper"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidMapper"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AndroidMapView">
</activity>
<uses-library android:name="com.google.android.maps" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-sdk android:minSdkVersion="5" />

</manifest>


Download the files.

Sunday, November 22, 2009

Check empty of EditText



To check if the content of a EditText is empty or not, the method equals() can be used.

if (EditText.getText().toString().equals(""))
{
//Do if the EditText is empty
}
else
{
//Do if the EditText is not empty
}


Where EditText is the View of the EditText.

RadioGroup and RadioButton

In this exercise, RadioGroup and RadioButton will be described.



A radio button is a two-states button that can be either checked or unchecked. When the radio button is unchecked, the user can press or click it to check it. A radio button cannot be unchecked by the user once checked.

Radio buttons are normally used together in a RadioGroup. When several radio buttons live inside a radio group, checking one radio button unchecks all the others.

RadioGroup is used to create a multiple-exclusion scope for a set of radio buttons. Checking one radio button that belongs to a radio group unchecks any previously checked radio button within the same group.

Intially, all of the radio buttons are unchecked. While it is not possible to uncheck a particular radio button, the radio group can be cleared to remove the checked state.

main.xml with three RadioButton inside a RadioGroup.
<?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"
  >
 <RadioGroup
  android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical">
   <RadioButton
    android:id="@+id/option1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Option 1" />
   <RadioButton
    android:id="@+id/option2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Option 2" />
   <RadioButton
    android:id="@+id/option3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Option 3" />
 </RadioGroup>
</LinearLayout>


AndroidRadioGroup.java
package com.exercise.AndroidRadioGroup;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;

public class AndroidRadioGroup extends Activity {
 
 RadioButton myOption1, myOption2, myOption3;
 
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      myOption1 = (RadioButton)findViewById(R.id.option1);
      myOption2 = (RadioButton)findViewById(R.id.option2);
      myOption3 = (RadioButton)findViewById(R.id.option3);
      myOption1.setOnClickListener(myOptionOnClickListener);
      myOption2.setOnClickListener(myOptionOnClickListener);
      myOption3.setOnClickListener(myOptionOnClickListener);
      myOption1.setChecked(true);
  }

  RadioButton.OnClickListener myOptionOnClickListener =
   new RadioButton.OnClickListener()
  {

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   Toast.makeText(AndroidRadioGroup.this,
     "Option 1 : " + myOption1.isChecked() + "\n"+
     "Option 2 : " + myOption2.isChecked() + "\n" +
     "Option 3 : " + myOption3.isChecked(),
     Toast.LENGTH_LONG).show();

  }
   
   
  };
}


Download the files.

Related: Read index of the checked RadioButton in RadioGroup.

Thursday, November 12, 2009

setBuiltInZoomControls on MapView

If you read the article Hello, MapView from Android, ZoomControls widget is suggested for zooming in and out of a View. MapView can automatically hook one for us by requesting it with the getZoomControls() method. But...actually it is now deprecated.

Instead of ZoomControls, MapView.setBuiltInZoomControls(true) can be used to add zoom mechanism on MapView.



Modify from previous article, AndroidLocation: with a CheckBox to toggle MapView.setSatellite().

Modify main.xml to remove SeekBar from the previous layout, and change android:clickable of com.google.android.maps.MapView to "true".

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"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<CheckBox
android:id="@+id/satellite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Satellite "
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/longitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Longitude:"
/>
<TextView
android:id="@+id/latitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Latitude:"
/>
</LinearLayout>
</LinearLayout>
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="-----Your Own API Key here-------------"
/>
</LinearLayout>


Modify AndroidLocation.java, remove all myZoomBar related code, and add the code
myMapView.setBuiltInZoomControls(true);

package com.AndroidLocation;

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

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidLocation extends MapActivity {

private LocationManager myLocationManager;
private LocationListener myLocationListener;
private TextView myLongitude, myLatitude;
private CheckBox mySatellite;

private MapView myMapView;

private MapController myMapController;

private void CenterLocation(GeoPoint centerGeoPoint)
{
myMapController.animateTo(centerGeoPoint);


myLongitude.setText("Longitude: "+
String.valueOf((float)centerGeoPoint.getLongitudeE6()/1000000)
);
myLatitude.setText("Latitude: "+
String.valueOf((float)centerGeoPoint.getLatitudeE6()/1000000)
);
};

private void SetSatellite()
{
myMapView.setSatellite(mySatellite.isChecked());
};

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

myMapView = (MapView)findViewById(R.id.mapview);

myMapView.setBuiltInZoomControls(true);


myLongitude = (TextView)findViewById(R.id.longitude);
myLatitude = (TextView)findViewById(R.id.latitude);
mySatellite = (CheckBox)findViewById(R.id.satellite);

SetSatellite();
myMapController = myMapView.getController();

myLocationManager = (LocationManager)getSystemService(
Context.LOCATION_SERVICE);

myLocationListener = new MyLocationListener();

myLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
myLocationListener);

//Get the current location in start-up
//check LastKnownLocation, if not valid, skip it.
Location initLocation=myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(initLocation != null)
{
GeoPoint initGeoPoint = new GeoPoint(
(int)(initLocation.getLatitude()*1000000),
(int)(initLocation.getLongitude()*1000000));
CenterLocation(initGeoPoint);
}
mySatellite.setOnClickListener(mySatelliteOnClickListener);
}

private CheckBox.OnClickListener mySatelliteOnClickListener =
new CheckBox.OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub
SetSatellite();
}

};


private class MyLocationListener implements LocationListener{

public void onLocationChanged(Location argLocation) {
// TODO Auto-generated method stub
GeoPoint myGeoPoint = new GeoPoint(
(int)(argLocation.getLatitude()*1000000),
(int)(argLocation.getLongitude()*1000000));

CenterLocation(myGeoPoint);
}

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

public void onStatusChanged(String provider,
int status, Bundle extras) {
// TODO Auto-generated method stub
}
}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
};
}


Download the files.

Wednesday, November 11, 2009

AndroidLocation: with a CheckBox to toggle MapView.setSatellite()

Modify previous exercise AndroidLocation with Zoom Level Control, using SeekBar, to have a CheckBox used to toggle "satellite" mode.



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"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<CheckBox
android:id="@+id/satellite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Satellite "
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/longitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Longitude:"
/>
<TextView
android:id="@+id/latitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Latitude:"
/>
</LinearLayout>
</LinearLayout>
<SeekBar
android:id="@+id/zoombar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="20"
android:progress="0"/>
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="false"
android:apiKey="-----Your Own API Key here-------------"
/>
</LinearLayout>


AndroidLocation.java
package com.AndroidLocation;

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

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidLocation extends MapActivity {

private LocationManager myLocationManager;
private LocationListener myLocationListener;
private TextView myLongitude, myLatitude;
private CheckBox mySatellite;

private MapView myMapView;
private SeekBar myZoomBar;

private MapController myMapController;

private void CenterLocation(GeoPoint centerGeoPoint)
{
myMapController.animateTo(centerGeoPoint);


myLongitude.setText("Longitude: "+
String.valueOf((float)centerGeoPoint.getLongitudeE6()/1000000)
);
myLatitude.setText("Latitude: "+
String.valueOf((float)centerGeoPoint.getLatitudeE6()/1000000)
);
};

private void SetZoomLevel()
{
int myZoomLevel = myZoomBar.getProgress()+1;
myMapController.setZoom(myZoomLevel);
Toast.makeText(this,
"Zoom Level : " + String.valueOf(myZoomLevel),
Toast.LENGTH_LONG).show();
};

private void SetSatellite()
{
myMapView.setSatellite(mySatellite.isChecked());
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myMapView = (MapView)findViewById(R.id.mapview);
myLongitude = (TextView)findViewById(R.id.longitude);
myLatitude = (TextView)findViewById(R.id.latitude);
myZoomBar = (SeekBar)findViewById(R.id.zoombar);
mySatellite = (CheckBox)findViewById(R.id.satellite);

SetSatellite();
myMapController = myMapView.getController();
SetZoomLevel();

myLocationManager = (LocationManager)getSystemService(
Context.LOCATION_SERVICE);

myLocationListener = new MyLocationListener();

myLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
myLocationListener);

//Get the current location in start-up
//check LastKnownLocation, if not valid, skip it.
Location initLocation=myLocationManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER);
if(initLocation != null)
{
GeoPoint initGeoPoint = new GeoPoint(
(int)(initLocation.getLatitude()*1000000),
(int)(initLocation.getLongitude()*1000000));
CenterLocation(initGeoPoint);
}
myZoomBar.setOnSeekBarChangeListener(myZoomBarOnSeekBarChangeListener);
mySatellite.setOnClickListener(mySatelliteOnClickListener);
}

private CheckBox.OnClickListener mySatelliteOnClickListener =
new CheckBox.OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub
SetSatellite();
}

};

private SeekBar.OnSeekBarChangeListener myZoomBarOnSeekBarChangeListener =
new SeekBar.OnSeekBarChangeListener(){

public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
SetZoomLevel();
}

public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

};

private class MyLocationListener implements LocationListener{

public void onLocationChanged(Location argLocation) {
// TODO Auto-generated method stub
GeoPoint myGeoPoint = new GeoPoint(
(int)(argLocation.getLatitude()*1000000),
(int)(argLocation.getLongitude()*1000000));

CenterLocation(myGeoPoint);
}

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

public void onStatusChanged(String provider,
int status, Bundle extras) {
// TODO Auto-generated method stub
}
}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
};
}


Download the files.

Tuesday, November 10, 2009

AndroidLocation with Zoom Level Control, using SeekBar

Further works on last exercise, MapView to center on the current location from GPS. A SeekBar to change Zoom Level is implemented. Also add valid checking in Start-up Location, skip if invalid; to prevent from Unexpected Stop.



Modify 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"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/longitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Longitude:"
/>
<TextView
android:id="@+id/latitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Latitude:"
/>
<SeekBar
android:id="@+id/zoombar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="20"
android:progress="0"/>
</LinearLayout>
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="false"
android:apiKey="-----Your Own API Key here-------------"
/>
</LinearLayout>


AndroidLocation.java
package com.AndroidLocation;

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

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidLocation extends MapActivity {

private LocationManager myLocationManager;
private LocationListener myLocationListener;
private TextView myLongitude, myLatitude;

private MapView myMapView;
private SeekBar myZoomBar;

private MapController myMapController;

private void CenterLocation(GeoPoint centerGeoPoint)
{
myMapController.animateTo(centerGeoPoint);


myLongitude.setText("Longitude: "+
String.valueOf((float)centerGeoPoint.getLongitudeE6()/1000000)
);
myLatitude.setText("Latitude: "+
String.valueOf((float)centerGeoPoint.getLatitudeE6()/1000000)
);
};

private void SetZoomLevel()
{
int myZoomLevel = myZoomBar.getProgress()+1;
myMapController.setZoom(myZoomLevel);
Toast.makeText(this,
"Zoom Level : " + String.valueOf(myZoomLevel),
Toast.LENGTH_LONG).show();
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myMapView = (MapView)findViewById(R.id.mapview);
myLongitude = (TextView)findViewById(R.id.longitude);
myLatitude = (TextView)findViewById(R.id.latitude);
myZoomBar = (SeekBar)findViewById(R.id.zoombar);

myMapView.setSatellite(true); //Set satellite view
myMapController = myMapView.getController();
SetZoomLevel();

myLocationManager = (LocationManager)getSystemService(
Context.LOCATION_SERVICE);

myLocationListener = new MyLocationListener();

myLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
myLocationListener);

//Get the current location in start-up
//check LastKnownLocation, if not valid, skip it.
Location initLocation=
myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(initLocation != null)
{
GeoPoint initGeoPoint = new GeoPoint(
(int)(initLocation.getLatitude()*1000000),
(int)(initLocation.getLongitude()*1000000));
CenterLocation(initGeoPoint);
}
myZoomBar.setOnSeekBarChangeListener(myZoomBarOnSeekBarChangeListener);
}

private SeekBar.OnSeekBarChangeListener myZoomBarOnSeekBarChangeListener =
new SeekBar.OnSeekBarChangeListener(){

public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
SetZoomLevel();
}

public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

};

private class MyLocationListener implements LocationListener{

public void onLocationChanged(Location argLocation) {
// TODO Auto-generated method stub
GeoPoint myGeoPoint = new GeoPoint(
(int)(argLocation.getLatitude()*1000000),
(int)(argLocation.getLongitude()*1000000));

CenterLocation(myGeoPoint);
}

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

public void onStatusChanged(String provider,
int status, Bundle extras) {
// TODO Auto-generated method stub
}
}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
};
}


Download the files.

Monday, November 9, 2009

MapView to center on the current location from GPS

In the article Read GPS location, LocationManager described how to get current location from GPS.
In the article A minimal Map application using MapActivity, described how to implemented a MapView with MapActiity.

In this article, both exercises are merged to have a MapView, which will center in the current location from GPS.



Please note that the application cannot be run directly, you have to have your own API Key to using the GPS API. Refer to the article A minimal Map application using MapActivity to obtain API Key.

Modify AndroidManifest.xml:
Add <uses-library android:name="com.google.android.maps" /> as a child of the <application> element.
Add <uses-permission android:name="android.permission.INTERNET" />
and <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> as children of the <manifest> element.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidLocation"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidLocation"
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" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-sdk android:minSdkVersion="5" />
</manifest>


Modify main.xml to have a MapView
<?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"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/longitude"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Longitude:"
/>
<TextView
android:id="@+id/latitude"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Latitude:"
/>
</LinearLayout>
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="false"
android:apiKey="-----Your Own API Key here-------------"
/>
</LinearLayout>


Finally, modify AndroidLocation.java
package com.AndroidLocation;

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

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidLocation extends MapActivity {

private LocationManager myLocationManager;
private LocationListener myLocationListener;
private TextView myLongitude, myLatitude;

private MapView myMapView;

private MapController myMapController;

private void CenterLocatio(GeoPoint centerGeoPoint)
{
myMapController.animateTo(centerGeoPoint);


myLongitude.setText("Longitude: "+
String.valueOf((float)centerGeoPoint.getLongitudeE6()/1000000)
);
myLatitude.setText("Latitude: "+
String.valueOf((float)centerGeoPoint.getLatitudeE6()/1000000)
);
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myMapView = (MapView)findViewById(R.id.mapview);
myLongitude = (TextView)findViewById(R.id.longitude);
myLatitude = (TextView)findViewById(R.id.latitude);
myMapView.setSatellite(true); //Set satellite view
myMapController = myMapView.getController();
myMapController.setZoom(20); //Fixed Zoom Level

myLocationManager = (LocationManager)getSystemService(
Context.LOCATION_SERVICE);

myLocationListener = new MyLocationListener();

myLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
myLocationListener);

//Get the current location in start-up
GeoPoint initGeoPoint = new GeoPoint(
(int)(myLocationManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER)
.getLatitude()*1000000),
(int)(myLocationManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER)
.getLongitude()*1000000));
CenterLocatio(initGeoPoint);
}

private class MyLocationListener implements LocationListener{

public void onLocationChanged(Location argLocation) {
// TODO Auto-generated method stub
GeoPoint myGeoPoint = new GeoPoint(
(int)(argLocation.getLatitude()*1000000),
(int)(argLocation.getLongitude()*1000000));

CenterLocatio(myGeoPoint);
}

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

public void onStatusChanged(String provider,
int status, Bundle extras) {
// TODO Auto-generated method stub
}
}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
};
}


Download the files.

Also refer Enable "Use wireless networks" and "Use GPS satellites" in Android Emulator and Change GPS location of Android Emulator.





Because te application read location in start-up,
so you have to send GPS Location to Android Emulator
at least once before the application start.

Edited@11-11-2009





Sunday, November 8, 2009

Enable "Use wireless networks" and "Use GPS satellites" in Android Emulator

To let your application to access networks and GPS, you have to enable it in Android Emulator.

Start Android Emulator, press MENU key in Home Screen, and Click Settings.


Select Location & security.


Enable Use wireless networks and Use GPS satellites

Friday, November 6, 2009

Change GPS location of Android Emulator

In order to test GPS function of a Android Application, DDMS of Android SDK can help to change GPS location of Android Emuator.

Change to DDMS in Eclipse:
Click Window->Open Perspective->DDMS

Scroll down in Emulator Control on the left, till Location Controls



type in Longitude and Latitude, and click Send.

Read GPS location, LocationManager

It's a activity implement LocationListener. When location changed, the method onLocationChanged() will be called-back.


Create a activity AndroidLocation


To let your application to access GPS function, ACCESS_FINE_LOCATION permission is needed. Add the code
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
to AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidLocation"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidLocation"
          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>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-sdk android:minSdkVersion="5" />
</manifest>


Modify main.xml to have 2 TextView to show location from GPS.
<?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="Latitude:"
/>
<TextView
android:id="@+id/Latitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Longitude:"
/>
<TextView
android:id="@+id/Longitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Modify the source Code, AndroidLocation.java:
package com.AndroidLocation;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidLocation extends Activity {

private LocationManager myLocationManager;
private LocationListener myLocationListener;
private TextView myLatitude, myLongitude;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myLatitude = (TextView)findViewById(R.id.Latitude);
myLongitude = (TextView)findViewById(R.id.Longitude);

myLocationManager = (LocationManager)getSystemService(
  Context.LOCATION_SERVICE);

myLocationListener = new MyLocationListener();

myLocationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER,
        0,
        0,
        myLocationListener);

//Get the current location in start-up
myLatitude.setText(String.valueOf(
  myLocationManager.getLastKnownLocation(
    LocationManager.GPS_PROVIDER).getLatitude()));

myLongitude.setText(String.valueOf(
 myLocationManager.getLastKnownLocation(
   LocationManager.GPS_PROVIDER).getLongitude()));

}

private class MyLocationListener implements LocationListener{

public void onLocationChanged(Location argLocation) {
// TODO Auto-generated method stub
myLatitude.setText(String.valueOf(
  argLocation.getLatitude()));
myLongitude.setText(String.valueOf(
  argLocation.getLongitude()));
}

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

public void onStatusChanged(String provider,
 int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
}


May be you have to Enable "Use wireless networks" and "Use GPS satellites" in Android Emulator.

And also Change GPS location of Android Emulator, to test the function of the Application.


Because te application read location in start-up, so you have to send GPS Location to Android Emulator at least once before the application start. Edited@11-11-2009


Thursday, November 5, 2009

Install Android SDK release 3 on Eclipse 3.5 Galileo, in Ubuntu 9.10

Both Adroid SDK and ubuntu have new release recently. There is a little bit variation in installation from the old version, so I re-write the installation procedure here. In this article, Eclipse 3.5 Galileo with Android SDK release 3 (for Android 2) will be installed on a fresh new ubuntu 9.10.

Environment:
OS: Linux ubuntu 9.10 for desktop or laptop (http://www.ubuntu.com/)
Eclipse: Eclipse 3.5 Galileo (bundled with ubuntu 9.10)
Android SDK: Android SDK release 3 (http://developer.android.com/)

Install Java:

Start a Terminal and type the command:
$ sudo apt-get install sun-java6-jdk



For Ubuntu 10.04 LTS, refer the article
"How to install sun-java6-jdk on Ubuntu 10.04 LTS?"

- Android Er@2010-05-04 -




Download Android SDK:

Download Android SDk release 3 from http://developer.android.com/sdk/index.html, extract to any folder you want. In my case, it is $home/android-sdk-linux/

Install Eclipse:

In ubuntu 9.10, Eclipse 3.5.1 is bundled currently. To install Eclipse on ubuntu, it can be downloaded from http://www.eclipse.org/, installe using Ubuntu Software Center (Application->Ubuntu Software Center in ubuntu menu bar), or installed using Synaptic Package Manager (System->Administration->Synaptic Package Manager in ubuntu menu bar). I tried install using the download version and Ubuntu Software Center, both cannot be installed without error, so Synaptic Package Manager is suggested to install Eclipse.
Involve Synaptic Package Manager from System->Administration of Ubuntu top menu.


Type Eclipse in te Quick search box, select eclipse in the list and mark for installation.


Additional required changes are listed, click Mark to accept.


Click Apply to start installation


Installing the ADT Plugin

Start Eclipse, Click Help->Install New Software... from the top menu.

Click Available Software Sites to check if http://download.eclipse.org/releases/galileo is available. If not, add it.

Add a new site https://dl-ssl.google.com/android/eclipse/



Select https://dl-ssl.google.com/android/eclipse/, and wait a moment. The available will be listed. click to select it and click Next.


Click Next again


Accept the terms and click Finish.


Wait, you will be asked for security warning, click OK.


Wait installation to finish, and accept re-start after finished.

Setup Android SDT inside Eclipse:

Click Window->Preference in Eclipse top menu.

Select Android on the left, and browse to your Android SDK folder, it's $home/android-sdk-linux/ in my setup.


Install Package:

Click Window->Android SDK and AVD Manager

Click Available Package from the left, expend and select your expected packages, and click Install Selected.


Accept All, and Install Accepted.


Restart Again.

Create AVD:
Refer to the article http://android-er.blogspot.com/2009/10/create-avd-for-android-2-sdk.html

Now, the installation is finished.

Wednesday, November 4, 2009

android.view.animation

android.view.animation provides classes that handle tweened animations.

In this exercise, animation is applied on a single view (AnalogClock) and on a whole layout (LinearLayout).



The main part is the class ViewAnimation extends Animation; initialize() initialize some setting, applyTransformation() will be called repeatly with difference value of interpolatedTime, between 0 to 1.

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mylayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/start1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Animation on LinearLayout"
/>
<Button
android:id="@+id/start2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Animation on AnalogClock"
/>
<AnalogClock
android:id="@+id/myClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>


AndroidViewAnimation.java
package com.exercise.AndroidViewAnimation;

import android.app.Activity;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;
import android.widget.AnalogClock;
import android.widget.Button;
import android.widget.LinearLayout;

public class AndroidViewAnimation extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button myStartButton1 = (Button)this.findViewById(R.id.start1);
myStartButton1.setOnClickListener(myStartButton1OnClickListener);
Button myStartButton2 = (Button)this.findViewById(R.id.start2);
myStartButton2.setOnClickListener(myStartButton2OnClickListener);
}

private Button.OnClickListener myStartButton1OnClickListener =
new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout myAnimation = (LinearLayout)findViewById(R.id.mylayout);
myAnimation.startAnimation(new ViewAnimation());
}
};

private Button.OnClickListener myStartButton2OnClickListener =
new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AnalogClock myAnimation = (AnalogClock)findViewById(R.id.myClock);
myAnimation.startAnimation(new ViewAnimation());
}
};

public class ViewAnimation extends Animation
{
int centerX, centerY;
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight)
{
super.initialize(width, height, parentWidth, parentHeight);
setDuration(5000);
setFillAfter(true);
setInterpolator(new LinearInterpolator());
centerX = width/2;
centerY = height/2;
}

@Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
// TODO Auto-generated method stub
final Matrix matrix = t.getMatrix();
matrix.setScale(interpolatedTime, interpolatedTime);
}
}
}


Download the files.

Tuesday, November 3, 2009

Cannot complete the install because one or more required items could not be found

I tried to install Android SDK and Eclipse Galileo on a fresh ubuntu 9.10 and found the error while installing ADT Plugin for Eclipse:

Cannot complete the install because one or more required items could not be found.
Software being installed: Android Development Tools 0.9.4.v200910220141-17704 (com.android.ide.eclipse.adt.feature.group 0.9.4.v200910220141-17704)
Missing requirement: Android Development Tools 0.9.4.v200910220141-17704 (com.android.ide.eclipse.adt.feature.group 0.9.4.v200910220141-17704) requires 'org.eclipse.wst.xml.core 0.0.0' but it could not be found


It's because there are some components missed in the setup, and the Galileo update site is not in the list of Available Software Sites.

To solve the problem:
Add "http://download.eclipse.org/releases/galileo" in the Available Software Sites.