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.....

2 comments:

  1. What is pocketAgentLocation in above example

    ReplyDelete
  2. You wrote very well your first blog.Thanks for sharing such a informative coding. waiting for your next blogs.


    Online Jon Portal Development

    ReplyDelete