Saturday, June 30, 2012

Display photo when it is selected in Gallery using Android application

Gallery Example
Example show: Photo get display when it is selected in Gallery.
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"
    />
<ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon"></ImageView>
<Gallery android:id="@+id/gallery1" android:layout_width="match_parent" android:layout_height="wrap_content"></Gallery>
</LinearLayout>
For Background
attributes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <declare-styleable name="Gallery1">
 <attr name="android:galleryItemBackground"></attr>
 </declare-styleable>   
</resources>
Now moving in coding part.

package com.galleryexample;


import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class GallaryExampleActivity extends Activity {
    /** Called when the activity is first created. */
Integer[] pics={
        R.drawable.inv1,
        R.drawable.inv2,
        R.drawable.inv3,
        R.drawable.inv4,
        R.drawable.inv5,
        R.drawable.inv6,
        R.drawable.inv7,
        R.drawable.inv8,
        R.drawable.inv9
       
      };
       ImageView imgview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Gallery ga=(Gallery)findViewById(R.id.gallery1);
    ga.setAdapter(new ImageAdapter(this));
    imgview =(ImageView)findViewById(R.id.imageView1);
       
       // display image when photo is selected in gallery
   ga.setOnItemSelectedListener(new              Gallery.OnItemSelectedListener() {


     @Override
     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

     Toast.makeText(getBaseContext(), "You have selected picture " + (arg2+1) + "", Toast.LENGTH_SHORT).show();
     imgview.setImageResource(pics[arg2]);
    }


    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

    }
    });
       
   //display image when photo is clicked in gallery
/*  ga.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
     Toast.makeText(getBaseContext(), "You have selected picture " + (arg2+1) + "", Toast.LENGTH_SHORT).show();
     imgview.setImageResource(pics[arg2]);
    }
    }); */
}


//creating another class
    
    public class ImageAdapter extends BaseAdapter {
    private Context ctx;
    int imgback;
    public ImageAdapter(Context c)
    {
    ctx=c;
    TypedArray ta=c.obtainStyledAttributes(R.styleable.Gallery1);
    imgback=ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
    ta.recycle();
    }


    @Override
    public int getCount() {
    // TODO Auto-generated method stub
    return pics.length ;
    }


    @Override
    public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
    }


    @Override
    public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView iv=new ImageView(ctx);
    iv.setImageResource(pics[position]);
    iv.setScaleType(ImageView.ScaleType.FIT_XY);
    iv.setLayoutParams(new Gallery.LayoutParams(150,150));
    iv.setBackgroundResource(imgback);
    return iv;
    }
     }
}

output

Wednesday, June 20, 2012

Insert Data in SQLite Database using Android Application

Hello user, today i am going to show how to create database in SQLite and Learn to insert data in database.
Here two field i have taken no and name.
To set watermark on field take <editText android:hint="No" />


<linearlayout android:baselinealigned="false" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android=
"http://schemas.android.com/apk/res/android">
<textview android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/hello">
<edittext android:hint="No" android:id="@+id/txtNo" android:layout_height="wrap_content" android:layout_width="match_parent">
<requestfocus></requestfocus>
</edittext>
<edittext android:hint="Name" android:id="@+id/txtName" android:layout_height="wrap_content" android:layout_width="match_parent"></edittext>
<button android:id="@+id/Save" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Save"></button>
</textview></linearlayout>
Now Let start with coding.
DatabaseDemo.java

package com.databasedemo.db;


import android.app.Activity;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class DatabaseDemo1Activity extends Activity implements OnClickListener 
{
    /** Called when the activity is first created. */
    EditText etno,etname;
    Button btnSave;
    DatabaseAdapter dbAdapter;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dbAdapter=new DatabaseAdapter(getApplicationContext());
        
        etno=(EditText)findViewById(R.id.txtNo);
        etname=(EditText)findViewById(R.id.txtName);
        btnSave=(Button)findViewById(R.id.Save);
        btnSave.setOnClickListener(this);
    }
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.Save)
{
//Toast.makeText(getApplicationContext(), "save",Toast.LENGTH_LONG).show();
String no=etno.getText().toString();
String name=etname.getText().toString();
dbAdapter.open();
long inserted=dbAdapter.insertTest(no,name);
if(inserted>=0)
{
Toast.makeText(getApplicationContext(), "data saved",Toast.LENGTH_LONG).show();

etno.setText("");
etname.setText("");
}
else
{
Toast.makeText(getApplicationContext(), "data not saved",Toast.LENGTH_LONG).show();

}
dbAdapter.close();
}
}
}

Here i marked some text in red color, use it after creating DatabaseAdapter Class.
Now Take DatabaseOpenHelper class.


In DatabaseOpenHelper.java
In this class we will create database test_database.db and Table= test.
Make sure there must be space between create table and TABLE_NAME

package com.databasedemo.db;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;


public class DatabaseOpenHelper extends SQLiteOpenHelper {


public static final String DBName = "test_database.db";
public static final String TABLE_NAME = "test";
public static final String TABLE_SQL = "Create Table "+ TABLE_NAME 
+"(_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+"no TEXT, "
+"name TEXT);";

public DatabaseOpenHelper(Context context) {
super(context, DBName, null, 1);
// TODO Auto-generated constructor stub
}


@Override
public void onCreate(SQLiteDatabase database) {
// TODO Auto-generated method stub
          database.execSQL(TABLE_SQL);
}


@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub


}


}

To check whether Database is created or not check.
1. click on DDMS TAB.
2. click File Explorer tab.
3. click "data" to expand 
4. again click on "data" to expand
5. see the package name that we create."com.databasedemo.db"
6. in package you will see the "database" click on it
7. after clicking you will see the database name that we named.
do all this when you run application.
Now create DatabaseAdapter class.


In DatabaseAdapter.java

package com.databasedemo.db;


import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;


public class DatabaseAdapter {
SQLiteDatabase database;
DatabaseOpenHelper dbHelper;

public DatabaseAdapter(Context context)
{
       dbHelper=new DatabaseOpenHelper(context);
       
}
public void open()
{
database=dbHelper.getWritableDatabase();
}
public void close()
{
database.close();
}
public long insertTest(String no,String name)
{
ContentValues values=new ContentValues();
values.put("no", no);
values.put("name", name);

return database.insert("test",null, values);
}
}
In next session we will see how to retrieve data
OUTPUT

Wednesday, June 13, 2012

Send Email Using Android

This is a Example of Email.
We can use Intend Method to send 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"
    />
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
    <TextView android:text="To : " android:id="@+id/txtTo" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <EditText android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/editText1">
        <requestFocus></requestFocus>
    </EditText>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content">
    <TextView android:text="Subject :" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <EditText android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/editText2" android:inputType="textPostalAddress"></EditText>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout3" android:layout_width="match_parent" android:layout_height="wrap_content">
    <TextView android:text="Body :" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <EditText android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/editText3"></EditText>
</LinearLayout>
<Button android:text="Send" android:id="@+id/btnSend" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Gallery android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/gallery1"></Gallery>
</LinearLayout>



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;


public class EmailProjActivity extends Activity {
    /** Called when the activity is first created. */
EditText edTo,edSub,edBdy;
Button send;
Gallery Gal;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        edTo=(EditText)findViewById(R.id.editText1);
        edSub=(EditText)findViewById(R.id.editText2);
        edBdy=(EditText)findViewById(R.id.editText3);
        send=(Button)findViewById(R.id.btnSend);
        send.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String To=edTo.getText().toString();
String sub=edSub.getText().toString();
String body=edBdy.getText().toString();
Intent email=new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{To});
email.putExtra(Intent.EXTRA_SUBJECT, sub);
email.putExtra(Intent.EXTRA_TEXT,body);
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose the email format:"));
}
});
    }
}

DatePicker Example

This is a Example of DatePicker

<DatePicker android:id="@+id/datePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content">
</DatePicker>  


public class datetime extends Activity{
private TextView tv;
private DatePicker dp;
private int y,m,day;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.datetime);
        showDateTime();
}
public void showDateTime()
{
tv=(TextView)findViewById(R.id.txtViewdt);
dp=(DatePicker)findViewById(R.id.datePicker1);
final Calendar c=Calendar.getInstance();
y=c.get(Calendar.YEAR);
m=c.get(Calendar.MONTH);
day=c.get(Calendar.DAY_OF_MONTH);
tv.setText(new StringBuilder().append(m).append("-").append(day).append("-").append(y));
dp.init(y, m, day, null);
}
}