Jun 23, 2011

Set RelativeLayout.ALIGN_PARENT_TOP/RelativeLayout.ALIGN_PARENT_BOTTOM using Java code

Example to set RelativeLayout.ALIGN_PARENT_TOP/RelativeLayout.ALIGN_PARENT_BOTTOM, using Java code, by calling addRule() of RelativeLayout.LayoutParams. To re-locate views in run-time.

Set RelativeLayout.ALIGN_PARENT_TOP/RelativeLayout.ALIGN_PARENT_BOTTOM using Java code

Example:
package com.AndroidAlignParent;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class AndroidAlignParentActivity extends Activity {

LinearLayout myLayout;

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

myLayout = (LinearLayout)findViewById(R.id.myLayout);
Button btnAlignTop = (Button)findViewById(R.id.alignTop);
Button btnAlignBottom = (Button)findViewById(R.id.alignBottom);

btnAlignTop.setOnClickListener(new Button.OnClickListener(){

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

RelativeLayout.LayoutParams params
= new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
myLayout.setLayoutParams(params);

Toast.makeText(AndroidAlignParentActivity.this,
"ALIGN_PARENT_TOP",
Toast.LENGTH_LONG).show();
}});

btnAlignBottom.setOnClickListener(new Button.OnClickListener(){

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

RelativeLayout.LayoutParams params
= new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
myLayout.setLayoutParams(params);

Toast.makeText(AndroidAlignParentActivity.this,
"ALIGN_PARENT_BOTTOM",
Toast.LENGTH_LONG).show();
}});
}
}


<?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"
/>
<RelativeLayout
android:id="@+id/parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF333333"
>
<LinearLayout
android:id="@+id/myLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FF999999"
>
<Button
android:id="@+id/alignTop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Align Parent Top"/>
<Button
android:id="@+id/alignBottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Align Parent Bottom"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>

Jun 21, 2011

Detect Touch Event part III - get touch pressure and size

The MotionEvent object retured with pressure and size of touching. It can be get using getPressure() and getSize() method. The code in last post "Detect Touch Event, test on Custom View; part II" is further modified to show touching pressure and size in color and radius.

get touch pressure and size of MotionEvent

package com.TestSingleTouch;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TestSingleTouch extends Activity {

public class TouchView extends View {

private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float x, y;
boolean touching = false;

float pressure = 0; //Touch pressure
float size = 0; //Touch size
final static float PRESET_PRESSURE = 0xFF;
final static float PRESET_SIZE = 300;

public TouchView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
if(touching){

//paint.setStyle(Paint.Style.STROKE);
//paint.setStrokeWidth(1);
//paint.setColor(Color.WHITE);
//canvas.drawCircle(x, y, 50f, paint);

paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(1);
paint.setColor(0xFF000000
+ ((int)(PRESET_PRESSURE * pressure) <<16)
+ ((int)(PRESET_PRESSURE * pressure) << 8)
+ (int)(PRESET_PRESSURE * pressure));
canvas.drawCircle(x, y, (PRESET_SIZE * size), paint);
}
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec));
}

@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub

pressure = event.getPressure();
if(pressure > 1){
pressure = 1;
}

size =event.getSize();

String act;

int action = event.getAction();
switch(action){
case MotionEvent.ACTION_MOVE:
act = "ACTION_MOVE\n";
x = event.getX();
y = event.getY();

touching = true;
break;
case MotionEvent.ACTION_DOWN:
act = "ACTION_DOWN\n";
x = event.getX();
y = event.getY();

touching = true;
break;
case MotionEvent.ACTION_UP:
act = "ACTION_UP\n";
touching = false;
break;
default:
act = "XXX\n";
touching = false;
}

act += event.toString();
textView.setText(act);

invalidate();
return true;
}

}

TextView textView;

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

textView = new TextView(this);
textView.setText("Waiting");
TouchView touchView = new TouchView(this);
LinearLayout mainScreen = new LinearLayout(this);
mainScreen.setOrientation(LinearLayout.VERTICAL);
mainScreen.addView(textView);
mainScreen.addView(touchView);
setContentView(mainScreen);

}

}


Next:
- Detect Touch Event part IV - get touch area



Detect Touch Event, test on Custom View; part II.

Following the former post "Detect Touch Event, test on Custom View". In order to trace the trigged MotionEvent in onTouchEvent(), it's modified to have a TextView to display the event.

Test Single Touch

package com.TestSingleTouch;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TestSingleTouch extends Activity {

public class TouchView extends View {

private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float x, y;
boolean touching = false;

public TouchView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
if(touching){
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
paint.setColor(Color.WHITE);
canvas.drawCircle(x, y, 50f, paint);
}
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec));
}

@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub

String act;

int action = event.getAction();
switch(action){
case MotionEvent.ACTION_MOVE:
act = "ACTION_MOVE\n";
x = event.getX();
y = event.getY();
touching = true;
break;
case MotionEvent.ACTION_DOWN:
act = "ACTION_DOWN\n";
x = event.getX();
y = event.getY();
touching = true;
break;
case MotionEvent.ACTION_UP:
act = "ACTION_UP\n";
touching = false;
break;
default:
act = "XXX\n";
touching = false;
}

act += event.toString();
textView.setText(act);

invalidate();
return true;
}

}

TextView textView;

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

textView = new TextView(this);
textView.setText("Waiting");
TouchView touchView = new TouchView(this);
LinearLayout mainScreen = new LinearLayout(this);
mainScreen.setOrientation(LinearLayout.VERTICAL);
mainScreen.addView(textView);
mainScreen.addView(touchView);
setContentView(mainScreen);

}

}


next:
- Detect Touch Event part III - get touch pressure and size

Related Post:
- Detect Touch Event, test on Custom View
- Detect Touch Event
- Detect Multi-Touch Event
- Detect Multi-Touch Event, test on Custom View

Jun 19, 2011

Easy drawing current location and compass on the MapView, using MyLocationOverlay.

com.google.android.maps provide a usefull class MyLocationOverlay to draw current location (and also compass) on the Map.

MyLocationOverlay

To use MyLocationOverlay is simply:
  • add a object of MyLocationOverlay in your mapView Overlays
  • call enableMyLocation() and/or enableCompass() in onResume()
  • call disableMyLocation() and/or disableCompass() onPause()
  • add "android.permission.ACCESS_FINE_LOCATION" in AndroidManifest.xml


The main.xml and MyItemizedOverlay.java have no change, you can copy the code from previous post Using ItemizedOverlay to add marker on MapView.

AndroidMapViewActivity.java
package com.AndroidMapView;

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

import android.graphics.drawable.Drawable;
import android.os.Bundle;

public class AndroidMapViewActivity extends MapActivity {
 
 MyItemizedOverlay myItemizedOverlay = null;
 MyLocationOverlay myLocationOverlay = null;
 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       MapView mapView = (MapView) findViewById(R.id.mapview);
       mapView.setBuiltInZoomControls(true);
      
       Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
       int markerWidth = marker.getIntrinsicWidth();
       int markerHeight = marker.getIntrinsicHeight();
       marker.setBounds(0, markerHeight, markerWidth, 0);
      
       myItemizedOverlay = new MyItemizedOverlay(marker);
       mapView.getOverlays().add(myItemizedOverlay);
      
       GeoPoint myPoint1 = new GeoPoint(0*1000000, 0*1000000);
       myItemizedOverlay.addItem(myPoint1, "myPoint1", "myPoint1");
       GeoPoint myPoint2 = new GeoPoint(50*1000000, 50*1000000);
       myItemizedOverlay.addItem(myPoint2, "myPoint2", "myPoint2");
      
       myLocationOverlay = new MyLocationOverlay(this, mapView);
       mapView.getOverlays().add(myLocationOverlay);
       mapView.postInvalidate();

   }

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

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

 @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();
  myLocationOverlay.enableMyLocation();
  myLocationOverlay.enableCompass();
 }

 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  myLocationOverlay.disableMyLocation();
  myLocationOverlay.disableCompass();
 } 
}


AndroidManifest.xml, "android.permission.INTERNET" and "android.permission.ACCESS_FINE_LOCATION" is needed.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.AndroidMapView"
     android:versionCode="1"
     android:versionName="1.0">
   <uses-sdk android:minSdkVersion="4" />
   <application android:icon="@drawable/icon" android:label="@string/app_name">
    <uses-library android:name="com.google.android.maps" />
       <activity android:name=".AndroidMapViewActivity"
                 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.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
</manifest>


related: Implement own Custom MapView to move the routine works inside MapView.

Jun 17, 2011

Using ItemizedOverlay to add marker on MapView

Reference last exercise MapView and MapActivity, we are going to add overlay of ItemizedOverlay, to add marker on our MapView.

Using ItemizedOverlay to add marker on MapView

The main.xml and AndroidManifest.xml are kept as in last post MapView and MapActivity.

Add a new class MyItemizedOverlay.java extends ItemizedOverlay.
package com.AndroidMapView;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

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

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem>{

private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();

public MyItemizedOverlay(Drawable marker) {
super(boundCenterBottom(marker));
// TODO Auto-generated constructor stub

populate();
}

public void addItem(GeoPoint p, String title, String snippet){
OverlayItem newItem = new OverlayItem(p, title, snippet);
overlayItemList.add(newItem);
   populate();
}

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

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

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

}


Modify our main class AndroidMapViewActivity.java to add ItemizedOverlay on ur MapView.
package com.AndroidMapView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

public class AndroidMapViewActivity extends MapActivity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     MapView mapView = (MapView) findViewById(R.id.mapview);
     mapView.setBuiltInZoomControls(true);
  
     Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
     int markerWidth = marker.getIntrinsicWidth();
     int markerHeight = marker.getIntrinsicHeight();
     marker.setBounds(0, markerHeight, markerWidth, 0);

  
     MyItemizedOverlay myItemizedOverlay = new MyItemizedOverlay(marker);
     mapView.getOverlays().add(myItemizedOverlay);
  
     GeoPoint myPoint1 = new GeoPoint(0*1000000, 0*1000000);
     myItemizedOverlay.addItem(myPoint1, "myPoint1", "myPoint1");
     GeoPoint myPoint2 = new GeoPoint(50*1000000, 50*1000000);
     myItemizedOverlay.addItem(myPoint2, "myPoint2", "myPoint2");

 }

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

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

}


Related articles:
- Easy drawing current location and compass on the MapView, using MyLocationOverlay.
- Detect touch on marker in MapView

Un-reelated article:
- OpenStreetMap version using osmdroid


MapView and MapActivity

Using the Google Maps library, you can create your own map-viewing Activity.

MapView and MapActivity

By default the Android SDK includes the Google APIs add-on, which in turn includes the Maps external library. The Google APIs add-on requires Android 1.5 SDK or later release.

Create a Android project with build target called "Google APIs".

Modify AndroidManifest.xml to include uses-library "com.google.android.maps", and grant permission of "android.permission.INTERNET".
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidMapView"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".AndroidMapViewActivity"
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.INTERNET"/>
</manifest>


To embed a MapView in your app, you have to obtain a Maps API key, refer to this post.

Modify main.xml to add com.google.android.maps.MapView, with your own Maps API Key.
<?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"
/>
<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 Maps API key here" />
</LinearLayout>


Main activity, extends MapActivity.
package com.AndroidMapView;

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

import android.os.Bundle;

public class AndroidMapViewActivity extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
}

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


next:
- Using ItemizedOverlay to add marker on MapView

Jun 16, 2011

MapView and Maps API Key

The MapView class in the Maps external library is a very useful class that lets you easily integrate Google Maps into your application. It provides built-in map downloading, rendering, and caching of Maps tiles, as well as a variety of display options and controls. It provides a wrapper around the Google Maps API that lets your application request and manipulate Google Maps data through class methods, and it lets you work with Maps data as you would other types of Views.

Because MapView gives you access to Google Maps data, you need to register with the Google Maps service and agree to the applicable Terms of Service before your MapView will be able to obtain data from Google Maps. This will apply whether you are developing your application on the emulator or preparing your application for deployment to mobile devices.

Registering for a Maps API Key is simple, free, and has two parts:

- Registering the MD5 fingerprint of the certificate that you will use to sign your application. The Maps registration service then provides you a Maps API Key that is associated with your application's signer certificate.

- Adding a reference to the Maps API Key in each MapView, whether declared in XML or instantiated directly from code. You can use the same Maps API Key for any MapView in any Android application, provided that the application is signed with the certificate whose fingerprint you registered with the service.

Google document, Obtaining a Maps API Key, have instructions on how to obtain and use your Maps API Key:
  • Overview
  • Getting the MD5 Fingerprint of Your Signing Certificate

  • Getting the MD5 Fingerprint of the SDK Debug Certificate
  • Registering the Certificate Fingerprint with the Google Maps Service
  • Adding the Maps API Key to your Application
  • Final Steps to Enable MapView Elements


Related Post:
- MapView and MapActivity

Display a icon/image on button using Java code

The method setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

example:
        Button imageButton = (Button)findViewById(R.id.imagebutton);
imageButton.setCompoundDrawablesWithIntrinsicBounds(
0, //left
0, //top
R.drawable.icon, //right
0); //bottom


Display a icon/image on button using Java code

Related post:
- Display a icon/image on button, using XML.

Jun 15, 2011

Ready image file using build-in Gallery, with Intent.ACTION_PICK

We can start a activity with Intent.ACTION_PICK, to start Android build-in Gallery app to select image.

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RQS_GET_IMAGE);


The selected image can be retrieved using data.getData() in onActivityResult().

Uri imageUri = data.getData();

In order to make use of ShrinkBitmap() in last post "Reduce Bitmap size using BitmapFactory.Options.inSampleSize", re-write ShrinkBitmap(Uri uri, int width, int height), instead of ShrinkBitmap(String file, int width, int height); to get a reduced size bitmap.

package com.AndroidLoadImageView;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

public class AndroidLoadImageViewActivity extends Activity {

final static int RQS_GET_IMAGE = 1;

ImageView image;

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

image = (ImageView)findViewById(R.id.image);
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RQS_GET_IMAGE);

}

Bitmap ShrinkBitmap(Uri uri, int width, int height){

BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;

Bitmap bitmap = null;;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));

int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

if (heightRatio > 1 || widthRatio > 1)
{
if (heightRatio > widthRatio)
{
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}

bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return bitmap;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK){
if(requestCode == RQS_GET_IMAGE){
Uri imageUri = data.getData();
String imageFile = imageUri.toString();
Toast.makeText(AndroidLoadImageViewActivity.this,
imageFile,
Toast.LENGTH_LONG).show();
Bitmap bm = ShrinkBitmap(imageUri, 300, 300);
image.setImageBitmap(bm);
}

}
}
}


<?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"
/>
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Jun 14, 2011

Reduce Bitmap size using BitmapFactory.Options.inSampleSize

In the post "Load ImageView with JPG file in SD Card", the ImageView is loaded with bitmap in full-size. It cost too much resources for a mobile device, and easy to make the app closed unexpectly.

We can generate a shrinked bitmap using BitmapFactory.Options with inSampleSize; such that to reduce the needed resources greatly.

package com.AndroidLoadImageView;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class AndroidLoadImageViewActivity extends Activity {

String imagefile ="/sdcard/IMG_9331.JPG";

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

ImageView image = (ImageView)findViewById(R.id.image);
//Bitmap bm = BitmapFactory.decodeFile(imagefile);
Bitmap bm = ShrinkBitmap(imagefile, 300, 300);
image.setImageBitmap(bm);
}

Bitmap ShrinkBitmap(String file, int width, int height){

BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

if (heightRatio > 1 || widthRatio > 1)
{
if (heightRatio > widthRatio)
{
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}

bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
return bitmap;
}
}



next post:
- Ready image file using build-in Gallery, with Intent.ACTION_PICK

Jun 13, 2011

Load ImageView with JPG file in SD Card

android.graphics.BitmapFactory class provide a method decodeFile(String pathName) to decode a file path into a bitmap. The decoded bitmap can be loaded to ImageView using setImageBitmap() method of the ImageView.

Load ImageView with JPG file in SD Card

package com.AndroidLoadImageView;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class AndroidLoadImageViewActivity extends Activity {

String imagefile ="/sdcard/IMG_9331.JPG";

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

ImageView image = (ImageView)findViewById(R.id.image);
Bitmap bm = BitmapFactory.decodeFile(imagefile);
image.setImageBitmap(bm);
}
}


<?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"
/>
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>



Related:
- ImageView
- Reduce Bitmap size using BitmapFactory.Options.inSampleSize
- Read Exif of JPG file using ExifInterface

Jun 9, 2011

Pick contact using intent of Intent.ACTION_PICK, with People.CONTENT_URI

It's a example to pick from system contact, using intent of Intent.ACTION_PICK, with People.CONTENT_URI. And show how to retrieve the contact in onActivityResult(), using Cursor.

package com.AndroidPickContact;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidPickContact extends Activity {

final static int RQS_PICK_CONTACT = 1;

TextView contactName, contactNumber, contactEmail;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
contactName = (TextView)findViewById(R.id.contactname);
contactNumber = (TextView)findViewById(R.id.contactnumber);
contactEmail = (TextView)findViewById(R.id.contactemail);
Button buttonPickContact = (Button)findViewById(R.id.pickcontact);
buttonPickContact.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
startActivityForResult(intent, RQS_PICK_CONTACT);
}});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == RQS_PICK_CONTACT){
if(resultCode == RESULT_OK){
Uri contactData = data.getData();
Cursor cursor = managedQuery(contactData, null, null, null, null);
cursor.moveToFirst();
String name = cursor.getString(cursor.getColumnIndexOrThrow(People.NAME));
String number = cursor.getString(cursor.getColumnIndexOrThrow(People.NUMBER));
String email = cursor.getString(cursor.getColumnIndexOrThrow(People.PRIMARY_EMAIL_ID));
contactName.setText(name);
contactNumber.setText(number);
contactEmail.setText(email);
}
}
}
}


<?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"
/>
<Button
android:id="@+id/pickcontact"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pick Contact"
/>
<TextView
android:id="@+id/contactname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/contactnumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/contactemail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Related Post:
- Query data from the Contacts content provider

Jun 8, 2011

Android Coding@mobile


Blogger introduce mobile version recently. Please check my mobile version: http://android-coding.blogspot.com/?m=1

Android Coding@mobile

Settings.ACTION_LOCATION_SOURCE_SETTINGS vs. Settings.ACTION_SECURITY_SETTINGS

As described in my old post, we can "Start Location setting if GPS disabled" by start activity with intent of "Settings.ACTION_SECURITY_SETTINGS".

Thanks for Brian comments:

It is better to use Settings.ACTION_LOCATION_SOURCE_SETTINGS instead of Settings.ACTION_SECURITY_SETTINGS.

The reason is that some phones (such as HTC Desire) have moved the GPS settings out of the Security page. Using the suggested intent gets the user to the GPS settings wherever they are.


Set GPS

package com.AndroidGPS;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidGPS extends Activity {

TextView textGpsStatus;
Button buttonSetGPS_ACTION_LOCATION_SOURCE_SETTINGS;
Button buttonSetGPS_ACTION_SECURITY_SETTINGS;

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

buttonSetGPS_ACTION_LOCATION_SOURCE_SETTINGS
= (Button)findViewById(R.id.setgps_ACTION_LOCATION_SOURCE_SETTINGS);

buttonSetGPS_ACTION_LOCATION_SOURCE_SETTINGS.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}});

buttonSetGPS_ACTION_SECURITY_SETTINGS
= (Button)findViewById(R.id.setgps_ACTION_SECURITY_SETTINGS);

buttonSetGPS_ACTION_SECURITY_SETTINGS.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivity(intent);
}});

}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
displayGpsStatus();
}

private void displayGpsStatus(){
ContentResolver contentResolver = getBaseContext().getContentResolver();
boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.GPS_PROVIDER);
if(gpsStatus){
textGpsStatus.setText("GPS: ON");
}else{
textGpsStatus.setText("GPS: OFF");
}
}
}


<?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:id="@+id/gpsstatus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/setgps_ACTION_LOCATION_SOURCE_SETTINGS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Turn On/Off GPS Using ACTION_LOCATION_SOURCE_SETTINGS"
/>
<Button
android:id="@+id/setgps_ACTION_SECURITY_SETTINGS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Turn On/Off GPS Using ACTION_SECURITY_SETTINGS"
/>
</LinearLayout>



Jun 7, 2011

Get neighboring cell information, getNeighboringCellInfo().

TelephonyManager.getNeighboringCellInfo() return a List of neighboring cell information(NeighboringCellInfo), including Received Signal Strength and Cell ID location.

Get neighboring cell information, getNeighboringCellInfo().

package com.AndroidTelephonyManager;

import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidTelephonyManager extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textGsmCellLocation = (TextView)findViewById(R.id.gsmcelllocation);
TextView textMCC = (TextView)findViewById(R.id.mcc);
TextView textMNC = (TextView)findViewById(R.id.mnc);
TextView textCID = (TextView)findViewById(R.id.cid);


//retrieve a reference to an instance of TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();

String networkOperator = telephonyManager.getNetworkOperator();
String mcc = networkOperator.substring(0, 3);
String mnc = networkOperator.substring(3);
textMCC.setText("mcc: " + mcc);
textMNC.setText("mnc: " + mnc);

int cid = cellLocation.getCid();
int lac = cellLocation.getLac();
textGsmCellLocation.setText(cellLocation.toString());
textCID.setText("gsm cell id: " + String.valueOf(cid));

TextView Neighboring = (TextView)findViewById(R.id.neighboring);
List<NeighboringCellInfo> NeighboringList = telephonyManager.getNeighboringCellInfo();

String stringNeighboring = "Neighboring List- Lac : Cid : RSSI\n";
for(int i=0; i < NeighboringList.size(); i++){

String dBm;
int rssi = NeighboringList.get(i).getRssi();
if(rssi == NeighboringCellInfo.UNKNOWN_RSSI){
dBm = "Unknown RSSI";
}else{
dBm = String.valueOf(-113 + 2 * rssi) + " dBm";
}

stringNeighboring = stringNeighboring
+ String.valueOf(NeighboringList.get(i).getLac()) +" : "
+ String.valueOf(NeighboringList.get(i).getCid()) +" : "
+ dBm +"\n";
}

Neighboring.setText(stringNeighboring);
}
}


<?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:id="@+id/gsmcelllocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/mcc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/mnc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/cid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/neighboring"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

Start Google Maps using intent of ACTION_VIEW, with zoom level.

To start Google Maps using intent of ACTION_VIEW with zoom level, you can use with the Uri:
geo:Latitude,Longitude?z=<zoom level>

Start Google Maps using intent of ACTION_VIEW, with zoom level.

Modify the main.xml in last post "Start Google Maps using intent of ACTION_VIEW", include a SeekBar for user to set zoom level.
<?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:id="@+id/gsmcelllocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/mcc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/mnc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/cid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/lac"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/geo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/remark"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<SeekBar
android:id="@+id/zoom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:max="21" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Zoom Level" />
<Button
android:id="@+id/viewmap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="View Map"
/>
</LinearLayout>


Modify btnViewMap.setOnClickListener() to start activity with Uri with zoom level
package com.AndroidTelephonyManager;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidTelephonyManager extends Activity {

public class OpenCellID {
String mcc; //Mobile Country Code
String mnc; //mobile network code
String cellid; //Cell ID
String lac; //Location Area Code

Boolean error;
String strURLSent;
String GetOpenCellID_fullresult;

String latitude;
String longitude;

public Boolean isError(){
return error;
}

public void setMcc(String value){
mcc = value;
}

public void setMnc(String value){
mnc = value;
}

public void setCallID(int value){
cellid = String.valueOf(value);
}

public void setCallLac(int value){
lac = String.valueOf(value);
}

public String getLocation(){
return(latitude + " : " + longitude);
}

public String getLatitude(){
return latitude;
}

public String getLongitude(){
return longitude;
}

public void groupURLSent(){
strURLSent =
"http://www.opencellid.org/cell/get?mcc=" + mcc
+"&mnc=" + mnc
+"&cellid=" + cellid
+"&lac=" + lac
+"&fmt=txt";
}

public String getstrURLSent(){
return strURLSent;
}

public String getGetOpenCellID_fullresult(){
return GetOpenCellID_fullresult;
}

public void GetOpenCellID() throws Exception {
groupURLSent();
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(strURLSent);
HttpResponse response = client.execute(request);
GetOpenCellID_fullresult = EntityUtils.toString(response.getEntity());
spliteResult();
}

private void spliteResult(){
if(GetOpenCellID_fullresult.equalsIgnoreCase("err")){
error = true;
}else{
error = false;
String[] tResult = GetOpenCellID_fullresult.split(",");
latitude = tResult[0];
longitude = tResult[1];
}


}
}

int myLatitude, myLongitude;
OpenCellID openCellID;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textGsmCellLocation = (TextView)findViewById(R.id.gsmcelllocation);
TextView textMCC = (TextView)findViewById(R.id.mcc);
TextView textMNC = (TextView)findViewById(R.id.mnc);
TextView textCID = (TextView)findViewById(R.id.cid);
TextView textLAC = (TextView)findViewById(R.id.lac);
TextView textGeo = (TextView)findViewById(R.id.geo);
TextView textRemark = (TextView)findViewById(R.id.remark);

final SeekBar zoomBar = (SeekBar)findViewById(R.id.zoom);

Button btnViewMap = (Button)findViewById(R.id.viewmap);
btnViewMap.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

int zoom = zoomBar.getProgress()+2; //convert to 2..23
//set 1 will be Force Closed! I don't know why.

//geo:Latitude,Longitude?z=<zoom level>
String stringLoc
= "geo:"
+ openCellID.getLatitude() + "," + openCellID.getLongitude()
+ "?z=" + String.valueOf(zoom);

Toast.makeText(AndroidTelephonyManager.this, stringLoc, Toast.LENGTH_LONG).show();

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(stringLoc));
startActivity(intent);
}});

//retrieve a reference to an instance of TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();

String networkOperator = telephonyManager.getNetworkOperator();
String mcc = networkOperator.substring(0, 3);
String mnc = networkOperator.substring(3);
textMCC.setText("mcc: " + mcc);
textMNC.setText("mnc: " + mnc);

int cid = cellLocation.getCid();
int lac = cellLocation.getLac();
textGsmCellLocation.setText(cellLocation.toString());
textCID.setText("gsm cell id: " + String.valueOf(cid));
textLAC.setText("gsm location area code: " + String.valueOf(lac));

openCellID = new OpenCellID();

openCellID.setMcc(mcc);
openCellID.setMnc(mnc);
openCellID.setCallID(cid);
openCellID.setCallLac(lac);
try {
openCellID.GetOpenCellID();

if(!openCellID.isError()){
textGeo.setText(openCellID.getLocation());
textRemark.setText( "\n\n"
+ "URL sent: \n" + openCellID.getstrURLSent() + "\n\n"
+ "response: \n" + openCellID.GetOpenCellID_fullresult);
}else{
textGeo.setText("Error");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
textGeo.setText("Exception: " + e.toString());
}
}
}


next:
- Get neighboring cell information, getNeighboringCellInfo()

Jun 6, 2011

Start Google Maps using intent of ACTION_VIEW

To start Android geo viewer, Google Maps, we can start activity with intent "android.content.Intent.ACTION_VIEW", with Uri of "geo:latitude,longitude".

Modify last post "Get location of Cell ID, from opencellid.org using HttpGet()", start Google Maps with the location returned from OpenCellID.

geo:latitude,longitude

AndroidTelephonyManager.java
package com.AndroidTelephonyManager;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidTelephonyManager extends Activity {

public class OpenCellID {
String mcc; //Mobile Country Code
String mnc; //mobile network code
String cellid; //Cell ID
String lac; //Location Area Code

Boolean error;
String strURLSent;
String GetOpenCellID_fullresult;

String latitude;
String longitude;

public Boolean isError(){
return error;
}

public void setMcc(String value){
mcc = value;
}

public void setMnc(String value){
mnc = value;
}

public void setCallID(int value){
cellid = String.valueOf(value);
}

public void setCallLac(int value){
lac = String.valueOf(value);
}

public String getLocation(){
return(latitude + " : " + longitude);
}

public String getLatitude(){
return latitude;
}

public String getLongitude(){
return longitude;
}

public void groupURLSent(){
strURLSent =
"http://www.opencellid.org/cell/get?mcc=" + mcc
+"&mnc=" + mnc
+"&cellid=" + cellid
+"&lac=" + lac
+"&fmt=txt";
}

public String getstrURLSent(){
return strURLSent;
}

public String getGetOpenCellID_fullresult(){
return GetOpenCellID_fullresult;
}

public void GetOpenCellID() throws Exception {
groupURLSent();
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(strURLSent);
HttpResponse response = client.execute(request);
GetOpenCellID_fullresult = EntityUtils.toString(response.getEntity());
spliteResult();
}

private void spliteResult(){
if(GetOpenCellID_fullresult.equalsIgnoreCase("err")){
error = true;
}else{
error = false;
String[] tResult = GetOpenCellID_fullresult.split(",");
latitude = tResult[0];
longitude = tResult[1];
}


}
}

int myLatitude, myLongitude;
OpenCellID openCellID;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textGsmCellLocation = (TextView)findViewById(R.id.gsmcelllocation);
TextView textMCC = (TextView)findViewById(R.id.mcc);
TextView textMNC = (TextView)findViewById(R.id.mnc);
TextView textCID = (TextView)findViewById(R.id.cid);
TextView textLAC = (TextView)findViewById(R.id.lac);
TextView textGeo = (TextView)findViewById(R.id.geo);
TextView textRemark = (TextView)findViewById(R.id.remark);

Button btnViewMap = (Button)findViewById(R.id.viewmap);
btnViewMap.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String stringLoc = "geo:" + openCellID.getLatitude() + "," + openCellID.getLongitude();

Toast.makeText(AndroidTelephonyManager.this, stringLoc, Toast.LENGTH_LONG).show();

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(stringLoc));
startActivity(intent);
}});

//retrieve a reference to an instance of TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();

String networkOperator = telephonyManager.getNetworkOperator();
String mcc = networkOperator.substring(0, 3);
String mnc = networkOperator.substring(3);
textMCC.setText("mcc: " + mcc);
textMNC.setText("mnc: " + mnc);

int cid = cellLocation.getCid();
int lac = cellLocation.getLac();
textGsmCellLocation.setText(cellLocation.toString());
textCID.setText("gsm cell id: " + String.valueOf(cid));
textLAC.setText("gsm location area code: " + String.valueOf(lac));

openCellID = new OpenCellID();

openCellID.setMcc(mcc);
openCellID.setMnc(mnc);
openCellID.setCallID(cid);
openCellID.setCallLac(lac);
try {
openCellID.GetOpenCellID();

if(!openCellID.isError()){
textGeo.setText(openCellID.getLocation());
textRemark.setText( "\n\n"
+ "URL sent: \n" + openCellID.getstrURLSent() + "\n\n"
+ "response: \n" + openCellID.GetOpenCellID_fullresult);
}else{
textGeo.setText("Error");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
textGeo.setText("Exception: " + e.toString());
}
}
}


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:id="@+id/gsmcelllocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/mcc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/mnc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/cid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/lac"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/geo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/remark"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/viewmap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="View Map"
/>
</LinearLayout>


next:
- Start Google Maps using intent of ACTION_VIEW, with zoom level.


related post:
- Start Google Maps using intent with specified location

Get location of Cell ID, from opencellid.org using HttpGet().

A class OpenCellID was created to handle the http communication with opencellid.org. To simplify the job, request with "fmt=txt" is sent, such that we can simply splite the result to retrieve our latitude, longitude.

To know more about the project OpenCellID, refer to last post.

Get location of Cell ID, from opencellid.org using HttpGet()

We need the following permission in this example:
  • android.permission.ACCESS_COARSE_LOCATION
  • android.permission.ACCESS_FINE_LOCATION
  • android.permission.READ_PHONE_STATE
  • android.permission.INTERNET


package com.AndroidTelephonyManager;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.widget.TextView;

public class AndroidTelephonyManager extends Activity {

public class OpenCellID {
String mcc; //Mobile Country Code
String mnc; //mobile network code
String cellid; //Cell ID
String lac; //Location Area Code

Boolean error;
String strURLSent;
String GetOpenCellID_fullresult;

String latitude;
String longitude;

public Boolean isError(){
return error;
}

public void setMcc(String value){
mcc = value;
}

public void setMnc(String value){
mnc = value;
}

public void setCallID(int value){
cellid = String.valueOf(value);
}

public void setCallLac(int value){
lac = String.valueOf(value);
}

public String getLocation(){
return(latitude + " : " + longitude);
}

public void groupURLSent(){
strURLSent =
"http://www.opencellid.org/cell/get?mcc=" + mcc
+"&mnc=" + mnc
+"&cellid=" + cellid
+"&lac=" + lac
+"&fmt=txt";
}

public String getstrURLSent(){
return strURLSent;
}

public String getGetOpenCellID_fullresult(){
return GetOpenCellID_fullresult;
}

public void GetOpenCellID() throws Exception {
groupURLSent();
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(strURLSent);
HttpResponse response = client.execute(request);
GetOpenCellID_fullresult = EntityUtils.toString(response.getEntity());
spliteResult();
}

private void spliteResult(){
if(GetOpenCellID_fullresult.equalsIgnoreCase("err")){
error = true;
}else{
error = false;
String[] tResult = GetOpenCellID_fullresult.split(",");
latitude = tResult[0];
longitude = tResult[1];
}


}
}

int myLatitude, myLongitude;
OpenCellID openCellID;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textGsmCellLocation = (TextView)findViewById(R.id.gsmcelllocation);
TextView textMCC = (TextView)findViewById(R.id.mcc);
TextView textMNC = (TextView)findViewById(R.id.mnc);
TextView textCID = (TextView)findViewById(R.id.cid);
TextView textLAC = (TextView)findViewById(R.id.lac);
TextView textGeo = (TextView)findViewById(R.id.geo);
TextView textRemark = (TextView)findViewById(R.id.remark);

//retrieve a reference to an instance of TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();

String networkOperator = telephonyManager.getNetworkOperator();
String mcc = networkOperator.substring(0, 3);
String mnc = networkOperator.substring(3);
textMCC.setText("mcc: " + mcc);
textMNC.setText("mnc: " + mnc);

int cid = cellLocation.getCid();
int lac = cellLocation.getLac();
textGsmCellLocation.setText(cellLocation.toString());
textCID.setText("gsm cell id: " + String.valueOf(cid));
textLAC.setText("gsm location area code: " + String.valueOf(lac));

openCellID = new OpenCellID();

openCellID.setMcc(mcc);
openCellID.setMnc(mnc);
openCellID.setCallID(cid);
openCellID.setCallLac(lac);
try {
openCellID.GetOpenCellID();

if(!openCellID.isError()){
textGeo.setText(openCellID.getLocation());
textRemark.setText( "\n\n"
+ "URL sent: \n" + openCellID.getstrURLSent() + "\n\n"
+ "response: \n" + openCellID.GetOpenCellID_fullresult);
}else{
textGeo.setText("Error");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
textGeo.setText("Exception: " + e.toString());
}
}
}


<?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:id="@+id/gsmcelllocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/mcc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/mnc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/cid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/lac"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/geo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/remark"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Related Post:
- OpenCellID
- Convert cellLocation to real location (Latitude and Longitude), from "http://www.google.com/glm/mmap"
- Start Google Maps using intent of ACTION_VIEW

Jun 5, 2011

OpenCellID

We can get our approximate location using TelephonyManager to get our phone connected Network Operator (MCC+MNC), cell id and location area code.

Refer to:
- Get Network info using TelephonyManager
- Get cell location on a GSM phone, getCellLocation()

Last post introduce "Convert cellLocation to real location (Latitude and Longitude)" using api at "http://www.google.com/glm/mmap". But it's a non-public, non-official api, as I know.

It's an open source project, OpenCellID, aiming to create a complete database of CellID worldwide, with their locations. It provides free access to tools, data to not only create this database, but also retreive location informations.

Here is a example how to get location with the following cell id:
  • mcc = 454
  • mnc = 04
  • cellid = 43191
  • lac = 7500

http://www.opencellid.org/cell/get?mcc=454&mnc=04&cellid=43191&lac=7500
OpenCellID example

Alternatively, you can send with "fmt=txt" to request result in simple text format.
http://www.opencellid.org/cell/get?mcc=454&mnc=04&cellid=43191&lac=7500&fmt=txt
OpenCellID example in text format

In next post, I will have a example describe how to Get location of Cell ID, from opencellid.org using HttpGet().

Jun 4, 2011

Convert cellLocation to real location (Latitude and Longitude), using "http://www.google.com/glm/mmap"

The post "Get cell location on a GSM phone, getCellLocation()" get cell id and location area code. It's not a useful info with converted to real location.

"http://www.google.com/glm/mmap" is a non-public API to convert cellLocation to latitude and longitude. Some nice guy developed the method to retrieve location from cellLocation.

Convert cellLocation to Latitude and Longitude

We need the following permission in this example:
  • android.permission.ACCESS_COARSE_LOCATION
  • android.permission.ACCESS_FINE_LOCATION
  • android.permission.READ_PHONE_STATE
  • android.permission.INTERNET


package com.AndroidTelephonyManager;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.widget.TextView;

public class AndroidTelephonyManager extends Activity {

int myLatitude, myLongitude;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textGsmCellLocation = (TextView)findViewById(R.id.gsmcelllocation);
TextView textCID = (TextView)findViewById(R.id.cid);
TextView textLAC = (TextView)findViewById(R.id.lac);
TextView textGeo = (TextView)findViewById(R.id.geo);

//retrieve a reference to an instance of TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();

int cid = cellLocation.getCid();
int lac = cellLocation.getLac();
textGsmCellLocation.setText(cellLocation.toString());
textCID.setText("gsm cell id: " + String.valueOf(cid));
textLAC.setText("gsm location area code: " + String.valueOf(lac));

if(RqsLocation(cid, lac)){
textGeo.setText(
String.valueOf((float)myLatitude/1000000)
+ " : "
+ String.valueOf((float)myLongitude/1000000));
}else{
textGeo.setText("Can't find Location!");
}

}

private Boolean RqsLocation(int cid, int lac){

Boolean result = false;

String urlmmap = "http://www.google.com/glm/mmap";

try {
URL url = new URL(urlmmap);
URLConnection conn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.connect();

OutputStream outputStream = httpConn.getOutputStream();
WriteData(outputStream, cid, lac);

InputStream inputStream = httpConn.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);

dataInputStream.readShort();
dataInputStream.readByte();
int code = dataInputStream.readInt();
if (code == 0) {
myLatitude = dataInputStream.readInt();
myLongitude = dataInputStream.readInt();

result = true;

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return result;

}

private void WriteData(OutputStream out, int cid, int lac)
throws IOException
{
DataOutputStream dataOutputStream = new DataOutputStream(out);
dataOutputStream.writeShort(21);
dataOutputStream.writeLong(0);
dataOutputStream.writeUTF("en");
dataOutputStream.writeUTF("Android");
dataOutputStream.writeUTF("1.0");
dataOutputStream.writeUTF("Web");
dataOutputStream.writeByte(27);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(3);
dataOutputStream.writeUTF("");

dataOutputStream.writeInt(cid);
dataOutputStream.writeInt(lac);

dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.flush();
}

}


<?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:id="@+id/gsmcelllocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/cid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/lac"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/geo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


It's another open source project, OpenCellID, which provide tools, and database to retrieve location informations of CellID worldwide.

Related Post:
- Get location of Cell ID, from opencellid.org using HttpGet().

Get cell location on a GSM phone, getCellLocation()

TelephonyManager.getCellLocation() return the current location of the device.

We need the following permission in this example:
  • android.permission.ACCESS_COARSE_LOCATION
  • android.permission.ACCESS_FINE_LOCATION
  • android.permission.READ_PHONE_STATE


Get cell location on a GSM phone, getCellLocation()

package com.AndroidTelephonyManager;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.widget.TextView;

public class AndroidTelephonyManager extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textGsmCellLocation = (TextView)findViewById(R.id.gsmcelllocation);
TextView textCID = (TextView)findViewById(R.id.cid);
TextView textLAC = (TextView)findViewById(R.id.lac);

//retrieve a reference to an instance of TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();

int cid = cellLocation.getCid();
int lac = cellLocation.getLac();
textGsmCellLocation.setText(cellLocation.toString());
textCID.setText("gsm cell id: " + String.valueOf(cid));
textLAC.setText("gsm location area code: " + String.valueOf(lac));
}
}


<?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:id="@+id/gsmcelllocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/cid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/lac"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


next:
- Convert cellLocation to real location (Latitude and Longitude)

Infolinks In Text Ads