1. Create database using any SQLite brwoser
2. put the database file in assets folder
3. create the class which extends SQLiteOpenHelper class as below and put database copying code in constructor
-----------------------------------------------------
package com.DBDemo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class DataHelper extends SQLiteOpenHelper {
public static final String DBName="LawDB";
Context ctx;
public DataHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
ctx=context;
try {
String s=ctx.getApplicationInfo().dataDir+"/databases";
File dbDir=new File(s);
if(!dbDir.exists())
{
dbDir.mkdir();
}
if(dbDir.exists())
{
File dbFile=new File(dbDir, DBName);
if(!dbFile.exists())
{
AssetManager asm=ctx.getAssets();
InputStream is=asm.open(DBName);
OutputStream os=new FileOutputStream(dbDir.getPath()+"/"+DBName);
while(true)
{
int i=is.read();
if(i!=-1)
os.write(i);
else
break;
}
is.close();
os.close();
}
}
Toast.makeText(ctx, s,5).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
-------------------------------------------------------------------------------------------
Home » All posts
Saturday, October 27, 2012
Monday, April 9, 2012
Get geo cordinates on Android MapView click
Add the method below to your class extending Overlay
@Override
public boolean onTap(GeoPoint point, MapView mapView)
{
Context contexto = mapView.getContext();
String msg = "Lat: " + point.getLatitudeE6()/1E6 + " - " +
"Lon: " + point.getLongitudeE6()/1E6;
Toast toast = Toast.makeText(contexto, msg, Toast.LENGTH_SHORT);
toast.show();
return true;
}
@Override
public boolean onTap(GeoPoint point, MapView mapView)
{
Context contexto = mapView.getContext();
String msg = "Lat: " + point.getLatitudeE6()/1E6 + " - " +
"Lon: " + point.getLongitudeE6()/1E6;
Toast toast = Toast.makeText(contexto, msg, Toast.LENGTH_SHORT);
toast.show();
return true;
}
Friday, April 6, 2012
Adding Multiple Markers on Android Map View
package com.AndroidMapDemo;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Toast;
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 MapDemoActivity extends MapActivity {
/** Called when the activity is first created. */
MapView mv;
MapController mc;
GeoPoint gp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mv=(MapView) findViewById(R.id.myMapView);
mv.setBuiltInZoomControls(true);
mc= mv.getController();
mc.setZoom(16);
mc.animateTo(new GeoPoint(22719467,75853043));
Drawable marker=getResources().getDrawable(android.R.drawable.star_on);
int markerWidth = marker.getIntrinsicWidth();
int markerHeight = marker.getIntrinsicHeight();
marker.setBounds(0, markerHeight, markerWidth, 0);
MyItemizedOverlay mio = new MyItemizedOverlay(marker);
mv.getOverlays().add(mio);
mio.addItem(new GeoPoint(22719467,75853043),
"KB", "Khajuri Bazar");
mio.addItem(new GeoPoint(22721486,75859823),
"NG",
"Nagar Nigam");
mio.addItem(new GeoPoint( 22718399,75854759),
"RJ",
"Rajwada");
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
class MyItemizedOverlay extends ItemizedOverlay
{
private List items=new ArrayList();
public MyItemizedOverlay(Drawable defaultMarker) {
super(defaultMarker);
// TODO Auto-generated constructor stub
boundCenterBottom(defaultMarker);
populate();
}
public void addItem(GeoPoint p, String title, String snippet){
OverlayItem newItem = new OverlayItem(p, title, snippet);
items.add(newItem);
populate();
}
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return(items.get(i));
}
@Override
public int size() {
// TODO Auto-generated method stub
return(items.size());
}
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);
}
@Override
protected boolean onTap(int i) {
Toast.makeText(MapDemoActivity.this,
items.get(i).getSnippet(),
Toast.LENGTH_SHORT).show();
return(true);
}
}
}
import java.util.ArrayList;
import java.util.List;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Toast;
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 MapDemoActivity extends MapActivity {
/** Called when the activity is first created. */
MapView mv;
MapController mc;
GeoPoint gp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mv=(MapView) findViewById(R.id.myMapView);
mv.setBuiltInZoomControls(true);
mc= mv.getController();
mc.setZoom(16);
mc.animateTo(new GeoPoint(22719467,75853043));
Drawable marker=getResources().getDrawable(android.R.drawable.star_on);
int markerWidth = marker.getIntrinsicWidth();
int markerHeight = marker.getIntrinsicHeight();
marker.setBounds(0, markerHeight, markerWidth, 0);
MyItemizedOverlay mio = new MyItemizedOverlay(marker);
mv.getOverlays().add(mio);
mio.addItem(new GeoPoint(22719467,75853043),
"KB", "Khajuri Bazar");
mio.addItem(new GeoPoint(22721486,75859823),
"NG",
"Nagar Nigam");
mio.addItem(new GeoPoint( 22718399,75854759),
"RJ",
"Rajwada");
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
class MyItemizedOverlay extends ItemizedOverlay
{
private List
public MyItemizedOverlay(Drawable defaultMarker) {
super(defaultMarker);
// TODO Auto-generated constructor stub
boundCenterBottom(defaultMarker);
populate();
}
public void addItem(GeoPoint p, String title, String snippet){
OverlayItem newItem = new OverlayItem(p, title, snippet);
items.add(newItem);
populate();
}
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return(items.get(i));
}
@Override
public int size() {
// TODO Auto-generated method stub
return(items.size());
}
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);
}
@Override
protected boolean onTap(int i) {
Toast.makeText(MapDemoActivity.this,
items.get(i).getSnippet(),
Toast.LENGTH_SHORT).show();
return(true);
}
}
}
Tuesday, December 13, 2011
Android GPS Sample Application
Read Conceptual part from developer.android.com
===========================================================
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"
android:id="@+id/tv"
/>
</LinearLayout>
=======================================================
AndroidManifest.XML
=======================================================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="vegam.gps_demo"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".gps_demo"
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-permission
android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission
android:name="android.permission.CONTROL_LOCATION_UPDATES" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
=======================================================
Code Part
=======================================================
package vegam.gps_demo;
import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.util.Log;
import android.widget.TextView;
public class gps_demo extends Activity implements LocationListener {
private LocationManager lm;
private TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.tv);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, this);
Location l1=new Location(LocationManager.GPS_PROVIDER);
String lat = String.valueOf(l1.getLatitude());
String lon = String.valueOf(l1.getLongitude());
Log.e("GPS", "location changed: lat="+lat+", lon="+lon);
tv.setText("lat="+lat+", lon="+lon);
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
String lat = String.valueOf(location.getLatitude());
String lon = String.valueOf(location.getLongitude());
Log.e("GPS", "location changed: lat="+lat+", lon="+lon);
tv.setText("lat="+lat+", lon="+lon);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Log.e("GPS", "provider disabled " + provider);
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Log.e("GPS", "provider enabled " + provider);
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Log.e("GPS", "status changed to " + provider + " [" + status + "]");
}
}
=================================================================
What to do to activate GPS on avd
1. do telnet to emulator by typing : telnet localhost 5554
where 5554 is the name of avd running
2.with the telnet window set/change the geo code by typing
geo fix 72.74 45.6
the changes will appear on avd
If you are using windowsXP for development then you can inspire yourself with the image below;
===========================================================
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"
android:id="@+id/tv"
/>
</LinearLayout>
=======================================================
AndroidManifest.XML
=======================================================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="vegam.gps_demo"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".gps_demo"
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-permission
android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission
android:name="android.permission.CONTROL_LOCATION_UPDATES" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
=======================================================
Code Part
=======================================================
package vegam.gps_demo;
import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.util.Log;
import android.widget.TextView;
public class gps_demo extends Activity implements LocationListener {
private LocationManager lm;
private TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.tv);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, this);
Location l1=new Location(LocationManager.GPS_PROVIDER);
String lat = String.valueOf(l1.getLatitude());
String lon = String.valueOf(l1.getLongitude());
Log.e("GPS", "location changed: lat="+lat+", lon="+lon);
tv.setText("lat="+lat+", lon="+lon);
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
String lat = String.valueOf(location.getLatitude());
String lon = String.valueOf(location.getLongitude());
Log.e("GPS", "location changed: lat="+lat+", lon="+lon);
tv.setText("lat="+lat+", lon="+lon);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Log.e("GPS", "provider disabled " + provider);
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Log.e("GPS", "provider enabled " + provider);
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Log.e("GPS", "status changed to " + provider + " [" + status + "]");
}
}
=================================================================
What to do to activate GPS on avd
1. do telnet to emulator by typing : telnet localhost 5554
where 5554 is the name of avd running
2.with the telnet window set/change the geo code by typing
geo fix 72.74 45.6
the changes will appear on avd
If you are using windowsXP for development then you can inspire yourself with the image below;
Monday, November 28, 2011
stop watch
//generate a Chronometer in xml
<Chronometer android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
></Chronometer>
//go to java code
//Create a object of Chronometer
Chronometer cm=(Chronometer)findViewById(R.id.chronometer);
//start the Chronometer
cm.start();
//above code will show you stop watch on your activity
//for stop
cm.stop();
//for restart time
<Chronometer android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
></Chronometer>
//go to java code
//Create a object of Chronometer
Chronometer cm=(Chronometer)findViewById(R.id.chronometer);
//start the Chronometer
cm.start();
//above code will show you stop watch on your activity
//for stop
cm.stop();
//for restart time
cm.getBase(SystemClock.elapsedRealtime());
Friday, November 18, 2011
Saturday, November 5, 2011
webView Sample
package com.mypack;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class myact extends Activity {
/** Called when the activity is first created. */
WebView mWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
// mWebView.loadData("Hello, world!","text/html", "UTF-8");
// mWebView.setWebViewClient(new HelloWebViewClient());
}
}
Subscribe to:
Posts (Atom)

