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>