Wednesday, December 22, 2010

custom list view in android

You can create custom listview in android and you can add any layout to this. Here is the code for this .

first you have create a xml file for listview as follows.

"mainllist.xml" and save it in the layout folder

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
 
 
  <ListView android:id="@+id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:headerDividersEnabled="true"
            android:cacheColorHint="#00000000"/>          
 
</LinearLayout>

You have call this from you activity which extends ListActivity  using
setContentView(R.layout.mainlistview);



Then you have to create the layout which you want to put inside the listview

 as follows

save it as "agentcontact.xml" in layout folder


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
 
  <TableRow android:id="@+id/subAgentContactTableRow"
            android:layout_width = "fill_parent"
            android:layout_height="wrap_content"
            android:gravity="left">
 
 <TextView android:id="@+id/agentContactListTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="15px"
android:textColor="#444444"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>

 <TableRow android:id="@+id/subAgentContactTableRow1"
           android:layout_width = "fill_parent"
           android:layout_height="wrap_content"
           android:gravity="right"
           android:paddingRight="5px">

 <Button android:id = "@+id/agentContactButton"
         android:text = "Contact"        
              android:layout_width = "wrap_content"
         android:layout_height = "wrap_content" />              
               
 </TableRow>
   </TableRow>
</LinearLayout>


Then we have to create a Custom ArrayAdapter calss which will handle all operation on the listview.
as follows.

class IconicAdapter extends ArrayAdapter {
Activity context;

IconicAdapter(Activity context) {

super(context, R.layout.agentcontact, pocketAgentLocation);
this.context=context;
}

public View getView(int position, View convertView,ViewGroup parent) {

LayoutInflater inflater=context.getLayoutInflater();
View agntContct=inflater.inflate(R.layout.agentcontact, null);


TextView label=(TextView)agntContct.findViewById(R.id.agentContactListTextView);
label.setText(pocketAgentLocation.get(position)._title);
label.setWidth(_listTextWidth);

Button _button  = (Button)agntContct.findViewById(R.id.agentContactButton);
_button .setOnClickListener(new OnClickListener() {

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

Toast.makeText(YOURCLASS.this,"Hii",Toast.LENGTH_LONG).show();
}
});
return(agntContct);
}
}

Thnaks.....

Sunday, December 12, 2010

How to attach file in email

Hi this is my first blog hope this will help you.
Thanks.............

 attaching file in email is simple here is my code.....


public void sendMail(String emailid,String mailbody){

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                               
emailIntent.setType("image/png");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+Environment.getExternalStorageDirectory()+"nameof your file"));
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emailid});
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Transaction Details");
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,mailbody);
    
        try {
        this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        } catch (android.content.ActivityNotFoundException ex) {
      
        Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }

}