Friday, July 08, 2011

contact application


Constructor
package com.contacts;

import android.graphics.Bitmap;

public class Constructor {

String name;
String number;
String email;
Bitmap image;

public Constructor(Bitmap image, String name, String number,String email) {
this.image=image;
this.name = name;
this.number = number;
this.email=email;


}
public Bitmap Image(){
return image;
}
public String Name() {
return name;
}

public String Number() {
return number;
}

public String Email() {
return email;
}

}


contact

package com.contacts;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class contact extends Activity {
Database data;
EditText name,number,email;
Button addimage,save,enterdetails,showdetails;
ImageView image;
Bitmap b;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        data=new Database(this);
        name=(EditText)findViewById(R.id.name);
        number=(EditText)findViewById(R.id.number);
        email=(EditText)findViewById(R.id.Email);
        addimage=(Button)findViewById(R.id.getimage);
        save=(Button)findViewById(R.id.save);
        enterdetails=(Button)findViewById(R.id.details);
        showdetails=(Button)findViewById(R.id.show);
        image=(ImageView)findViewById(R.id.imageView1);
        enterdetails.setEnabled(false);
        save.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
        if(name.getText().toString().trim().length()==0)
        {
        name.setError("Please Enter The Name");
        }
        else if(number.getText().toString().trim().length()==0)
        {
        number.setError("Please Enter The Mobile Number");
        }
        else if(email.getText().toString().trim().length()==0)
        {
        email.setError("Please Enter the Email Address");
        }
        else
        {
        String getname=name.getText().toString();
        String getnumber=number.getText().toString();
        String getemail=email.getText().toString();
        b=BitmapFactory.decodeResource(getResources(), R.drawable.icon); 
        System.out.println("b::::::::::::::::::"+b);
        data.open();
        data.insertTitle(1, getname, getnumber, getemail, b);
        data.close();
        name.setText("");
        number.setText("");
        email.setText("");
        }
        }
        });
       showdetails.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent in=new Intent(getApplicationContext(),showdetails.class);   
  startActivity(in);
}
       });
        
    }
}

database
package com.contacts;

import java.io.ByteArrayOutputStream;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.util.Log;
public class Database {
public static final String KEY_ROWID = "_id";
    public static final String KEY_UNIQUE="uni";
    public static final String KEY_NAME = "name";
    public static final String KEY_NUMBER="number";
    public static final String KEY_EMAIL="email";
    public static final String KEY_IMAGE="image";
    private static final String TAG = "DBAdapter";
    private static final String DATABASE_NAME = "contactsinfo";
    private static final String DATABASE_TABLE = "savecontacts";
    private static final int DATABASE_VERSION = 25;
    Bitmap bmp;
    String number,name,email;
    private static final String DATABASE_CREATE =
        "create table savecontacts (_id integer primary key autoincrement,"+ "name text not null,number text not null,email text not null,image blob not null);";
    
    private final Context context; 
    
    private DatabaseHelper DBHelper;
    private SQLiteDatabase bfd; 
    public Database(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }
        
    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    }    
    
    //---opens the database---
    public Database open() throws SQLException 
    {
       bfd = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---    
    public void close() 
    {
        DBHelper.close();
    }
    
    //---insert a title into the database---
    public long insertTitle(long _id,String name,String number,String email,Bitmap b) 
    {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
        b.compress(Bitmap.CompressFormat.PNG, 10, out);
        System.out.println("out::::::::"+out.toByteArray());
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_NAME, name);
        initialValues.put(KEY_NUMBER,number);
        initialValues.put(KEY_EMAIL,email);
        initialValues.put(KEY_IMAGE, out.toByteArray());
        
       return bfd.insert(DATABASE_TABLE, null, initialValues);
    }
    public Cursor getlistitems()
    {
    Cursor mCursor =
             bfd.query(true, DATABASE_TABLE, new String[] {
            KEY_NAME,
              KEY_NUMBER,
              KEY_EMAIL,
              KEY_IMAGE,
              }, 
              null,
              null,
              null, 
              null, 
              null, 
              null);
     if (mCursor != null) {
         mCursor.moveToFirst();
     }
     return mCursor;
    }
}


showdetails
package com.contacts;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class showdetails extends Activity {
Database data;
String name,email,number;
//Long number;
ListView contacts;
Button enterdetails,showdetails;
public DataAdapter notes;
byte[] image;
Bitmap img;
ArrayList<Constructor> DisplayData = new ArrayList<Constructor>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
data=new Database(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.show);
notes=new DataAdapter(this,R.layout.button, DisplayData);
contacts=(ListView)findViewById(R.id.list);
contacts.setAdapter(notes);
enterdetails=(Button)findViewById(R.id.details1);
showdetails=(Button)findViewById(R.id.show1);
showdetails.setEnabled(false);
enterdetails.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in=new Intent(getApplicationContext(),contact.class);   
  startActivity(in);
}
});
data.open();
Cursor getdetails=data.getlistitems();
if(getdetails.moveToFirst())
{
do{
 name=getdetails.getString(0);
 number=getdetails.getString(1);
 email=getdetails.getString(2);
 image=getdetails.getBlob(3);
 img=BitmapFactory.decodeByteArray(image,0,image.length);
 System.out.println("name::::::::::"+name);
 System.out.println("number::::::::::::::"+number);
 System.out.println("image::::::"+img);
 DisplayData.add(new Constructor(img,name,number,email));
}while(getdetails.moveToNext());
}
getdetails.close();
//System.out.println("image::::::::::::::"+image);
}
public class DataAdapter extends BaseAdapter{
   
    private Context context;
       private List<Constructor> buttonList;
    //private int rowResID;
    public DataAdapter(Context context, int rowResID,
    List<Constructor> buttonList ) { 
    this.context = context;
    //this.rowResID = rowResID;
    this.buttonList = buttonList;
    }
       public int getCount() {                        
           return buttonList.size();
       }
       public Object getItem(int position) {     
           return buttonList.get(position);
       }
       public long getItemId(int position) {  
           return position;
       }
       public View getView(final int position, View convertView, ViewGroup parent) { 
       
           final Constructor row = buttonList.get(position);
           LayoutInflater inflater = LayoutInflater.from( context );
    View v = inflater.inflate( R.layout.button, parent, false );
    final ImageView image1=(ImageView)v.findViewById(R.id.view);
    final TextView nametext = (TextView)v.findViewById( R.id.nametext);
    final TextView numbertext = (TextView)v.findViewById( R.id.numbertext );
    final TextView emailtext = (TextView)v.findViewById( R.id.emailtext );
    image1.setImageBitmap(row.Image());
       nametext.setText(row.Name());
       numbertext.setText(row.Number());
       emailtext.setText(row.Email());
       return v;
       }
      
   }

}


xml files

button.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scroller" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" >

<ImageView
android:id="@+id/view"
android:layout_width="50dp"
android:layout_height="40dp"/>
<LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
     >
<TextView 
android:id="@+id/nametext" 
android:layout_width="wrap_content"
android:textColor="#ffffff"
android:layout_height="wrap_content" 
  android:textStyle="bold"/> 
<TextView 
android:id="@+id/numbertext" 
android:layout_width="wrap_content"
android:textColor="#ffffff"
android:layout_height="wrap_content" 
  android:textStyle="bold"/> 
<TextView 
android:id="@+id/emailtext" 
android:layout_width="wrap_content"
android:textColor="#ffffff" 
android:layout_height="wrap_content" 
  android:textStyle="bold"/> 
</LinearLayout>
</LinearLayout>
</ScrollView>

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="40dp"
     >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="50dp"
    android:layout_marginLeft="20dp" 
    android:layout_marginRight="20dp"
    android:layout_marginTop="15dp"
    android:textColor="#ffffff"
    android:text="ENTER THE CONTACT DETAILS"
    android:textSize="20sp"
    />
</LinearLayout>
<LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="20dp"
     >
</LinearLayout>
<LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#2F4F4F"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
     >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="ENTER NAME"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="ENTER NUMBER"
    android:layout_marginLeft="10dp"
    />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/number"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="ENTER EMAIL"
    android:layout_marginLeft="10dp"
    />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/Email"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="10dp"
     >
</LinearLayout>
<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
     >
<Button 
android:layout_height="wrap_content" 
android:text="ADD IMAGE" 
android:id="@+id/getimage"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"/>
<ImageView 
android:id="@+id/imageView1" 
android:src="@drawable/icon"
android:layout_height="wrap_content"  
android:layout_width="fill_parent"
android:gravity="right">
</ImageView>

</LinearLayout>
<LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
     >
<Button 
android:layout_height="wrap_content" 
android:layout_gravity="center"
android:text="SAVE" 
android:id="@+id/save"
android:layout_width="300dp"/>
<LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="15dp"
     >
</LinearLayout>
<LinearLayout 
    android:orientation="horizontal"
    android:layout_gravity="bottom"
    android:background="#ffffff"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
     >
<Button 
android:layout_height="wrap_content" 
android:text="Enter Details" 
android:id="@+id/details"
android:layout_width="fill_parent"
android:layout_weight="0.5"/>
<Button 
android:layout_height="wrap_content" 
android:text="Show Contacts" 
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_weight="0.5"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

show.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">
    <ListView android:id="@+id/list"
        android:layout_width="fill_parent" 
        android:layout_height="0dip"
        android:layout_weight="1"
        android:stackFromBottom="false"
        android:transcriptMode="normal"/>
   <LinearLayout 
    android:orientation="horizontal"
    android:layout_gravity="bottom"
    android:background="#ffffff"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
     >
<Button 
android:layout_height="wrap_content" 
android:text="Enter Details" 
android:id="@+id/details1"
android:layout_width="fill_parent"
android:layout_weight="0.5"/>
<Button 
android:layout_height="wrap_content" 
android:text="Show Contacts" 
android:id="@+id/show1"
android:layout_width="fill_parent"
android:layout_weight="0.5"/>
</LinearLayout>
        
</LinearLayout>


manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.contacts"
      android:versionCode="1"
      android:versionName="1.0">


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".contact"
                  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=".showdetails">
   </activity>
    </application>
</manifest>

Friday, June 03, 2011

How to find the Location By giving latitude and longitude values In android

This tutorial will helps to find the location when we give latitude and longitude values in edit text boxes. First give the latitude value and longitude value then click on Go button then you will find the location where u have.


First create a new project then create a xml file like this


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="wrap_content"
    >
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
 
    >
<EditText
    android:id="@+id/id1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:layout_weight="0.4"
   
    />
<EditText
    android:id="@+id/id2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:layout_weight="0.4"
    />
 <Button
     android:id="@+id/get"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Get"
     />
</LinearLayout>
<com.google.android.maps.MapView
    android:id="@+id/myGMap"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:enabled="true"
     android:clickable="true"
    android:apiKey="0BV-zHYHhk4poftr7iQdExL03sI-TYH-qwxwflA"
    />
</LinearLayout>

After creating xml file then In java file code like this

 GetLocation.java:-

package com.wissen.android;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ZoomControls;
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 com.google.android.maps.Overlay;

public class GetLocation extends MapActivity implements LocationListener {
    /** Called when the activity is first created. */
   
    EditText txtlang,txtlat;
    Button getmap;
   
    Button btnSimple=null;
   
    MapView gMapView = null;
   
    MapController mc= null;
   
    Drawable defaultMarker= null;
   
    GeoPoint gp    = null;
    Double latitude,longitude;
    boolean clicked = false;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        // Creating TextBox displying Lat, Long
        txtlang = (EditText) findViewById(R.id.id1);
        txtlat = (EditText) findViewById(R.id.id2);
        getmap=(Button)findViewById(R.id.get);
        getmap.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v) {
           
            getview();
            clicked = true;
            System.out.println("clicked::::::::::::"+clicked);
            }
        });
       
    }
    public void getview(){
        if(clicked == true)   
        {
            System.out.println("entered");
            gMapView.removeAllViews();
            clicked = false;
            //gMapView.removeView(gMapView);
        }
        String Add=txtlang.getText().toString();
        latitude=Double.parseDouble(txtlang.getText().toString());
        longitude=Double.parseDouble(txtlat.getText().toString());
        System.out.println("lat::::::::::::"+latitude);
        System.out.println("logitude:::::::::::"+longitude);
       
        gMapView = (MapView) findViewById(R.id.myGMap);
        gp = new GeoPoint((int) (latitude * 1000000), (int) (longitude * 1000000));
        //gMapView.setSatellite(true);
        mc = gMapView.getController();
        mc.setCenter(gp);
        mc.setZoom(14);
        // Add a location mark
        MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
        List<Overlay> list = gMapView.getOverlays();
        list.add(myLocationOverlay);
       
        // Adding zoom controls to Map
        ZoomControls zoomControls = (ZoomControls) gMapView.getZoomControls();
        zoomControls.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));       
        gMapView.addView(zoomControls);
        gMapView.displayZoomControls(true);
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
        }
    /* This method is called when use position will get changed */
    public void onLocationChanged(Location location) {
        if (location != null) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            gp = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
            mc.animateTo(gp);
        }
    }
   
    public void onProviderDisabled(String provider) {
        // required for interface, not used
    }
   
    public void onProviderEnabled(String provider) {
        // required for interface, not used
    }
   
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // required for interface, not used
    }
   
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
   
   
   
    /* Class overload draw method which actually plot a marker,text etc. on Map */
    protected class MyLocationOverlay extends com.google.android.maps.Overlay {
       
        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
            Paint paint = new Paint();
           
            super.draw(canvas, mapView, shadow);
            // Converts lat/lng-Point to OUR coordinates on the screen.
            Point p = new Point();
            mapView.getProjection().toPixels(gp, p);
           
            paint.setStrokeWidth(1);
            paint.setARGB(255, 255, 255, 255);
            paint.setStyle(Paint.Style.STROKE);
           
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);       
           
            canvas.drawBitmap(bmp, p.x,p.y, paint);
       
            canvas.drawText("This is the location u selected ...", p.x, p.y, paint);
            return true;
        }
    }
}


Finally the Android Manifest file is

AndroidManifest.xml:-

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

Then it Look like this:-