Thursday, October 4, 2012

Android custom Listview

- Kalimuthu Regaswamy
Most of android mobile applications has a listview that displays something. And its been a practise by all android developes to add a view of their taste, just by customizing the listview to what required. This blog explains how to create a custom listview in android mobile. I hope this blog will help any android application builders who need to customize listview. The view I am trying out here has a image, two textview and a multiple choice option. 


Designing Listview:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="394dp"
android:layout_margin="5dp"
android:layout_weight="0.95"
android:cacheColorHint="#0000"
android:clipToPadding="true"
android:dividerHeight="1px"
android:scrollbars="none"
android:soundEffectsEnabled="true" >
</ListView>
</RelativeLayout>

Designing custom cell:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
android:id="@+id/Listimage"
android:layout_width="96px"
android:layout_height="96px"
android:layout_alignParentLeft="true"
android:layout_margin="1dp"
android:layout_marginTop="21dp"
android:background="@drawable/ic_launcher" />

<TextView
android:id="@+id/name"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="119dp"
android:layout_toRightOf="@+id/Listimage"
android:ellipsize="end"
android:text="TextView"
android:textSize="20px"
android:textStyle="bold" />

<TextView
android:id="@+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/Listimage"
android:layout_alignLeft="@+id/name"
android:layout_marginBottom="14dp"
android:layout_marginLeft="16dp"
android:text="TextView"
android:textSize="15px" />

<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/no"
android:layout_alignParentRight="true"
/>
</RelativeLayout>

Creating pojo class:
The pojo class here gets and sets the needed objects

public class Pojo {
public String name;
public String no;
public String desc;
public String post;
private Boolean isSelected;
public Boolean getIsSelected() {
 return isSelected;
}
public void setIsSelected(Boolean isSelected) {
 this.isSelected = isSelected;
}
public String getName() {
 return name;
}
public void setName(String name) {
 this.name = name;
}
public String getNo() {
 return no;
}
public void setNo(String no) {
 this.no = no;
}
public String getDesc() {
 return desc;
}
public void setDesc(String desc) {
 this.desc = desc;
}
public String getPost() {
 return post;
}
public void setPost(String post) {
 this.post = post;
}
}

Coding list adapter :I have written a list adapter which actullay adds the custom cells designed into the listview. Adapter,  here is the bridge between a ListView and the data that backs the list. The ListView can display any data provided that it is wrapped in a ListAdapter.

public class MyCustomArrayAdapter extends ArrayAdapter<Pojo> {

 private final List<Pojo> list;
 private final Activity context;
 ViewHolder viewHolder=null ;
 public MyCustomArrayAdapter(Activity context, List<Pojo> list) {
 super(context, R.layout.customlist, list);
 this.context = context;
 this.list = list;
}

static class ViewHolder {
 protected TextView name,no;
 protected ImageView image;
 protected CheckBox checkbox;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
 View view = null;
 if (convertView == null) {
  LayoutInflater inflator = context.getLayoutInflater();
  view = inflator.inflate(R.layout.customlist, null);
  viewHolder = new ViewHolder();
  viewHolder.name = (TextView) view.findViewById(R.id.name);
  viewHolder.name.setTextColor(Color.BLACK);
  viewHolder.name.setEllipsize(TruncateAt.MIDDLE);
  viewHolder.no = (TextView) view.findViewById(R.id.no);
  viewHolder.no.setTextColor(Color.BLACK);
  viewHolder.image = (ImageView)         
  view.findViewById(R.id.Listimage);
  viewHolder.name.setText(list.get(position).getName());
  viewHolder.no.setText(list.get(position).getNo());
  viewHolder.checkbox =(CheckBox) view.findViewById(R.id.check);
  viewHolder.checkbox.setOnCheckedChangeListener(new 
   CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView,
    boolean isChecked) {
     Pojo element = (Pojo) viewHolder.checkbox
     .getTag();
     element.setIsSelected(buttonView.isChecked());
    }
  });
  viewHolder.image.setBackgroundResource(R.drawable.ic_launcher);
  viewHolder.checkbox.setTag(list.get(position));
  } else {
   view = convertView;
   ((ViewHolder)  
            view.getTag()).checkbox.setTag(list.get(position));             
    }
  return view;
 }

Main activity class:
This is where the list is loaded. Here I have replaced the default array adapter with the custom adapter.

public class MainActivity extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 ArrayAdapter<Pojo> adapter = new MyCustomArrayAdapter(
 MainActivity.this, getData());
 setListAdapter(adapter);
}
public List<Pojo> getData(){
 List<Pojo> addList =new ArrayList<Pojo>();
 Pojo objectPojo=new Pojo();
 objectPojo.setName("muthu");
 objectPojo.setNo("1234");
 objectPojo.setDesc("android ");
 objectPojo.setPost("Developer");
 objectPojo.setIsSelected(false);
 addList.add(objectPojo);
 objectPojo=new Pojo();
 objectPojo.setName("kali");
 objectPojo.setNo("3456");
 objectPojo.setDesc("iPhone ");
 objectPojo.setPost("Developer");
 objectPojo.setIsSelected(false);
 addList.add(objectPojo);

 objectPojo=new Pojo();
 objectPojo.setName("arasu");
 objectPojo.setNo("5678");
 objectPojo.setDesc("windows ");
 objectPojo.setPost("Developer");
 objectPojo.setIsSelected(false);
 addList.add(objectPojo);
 objectPojo=new Pojo();
 objectPojo.setName("anbu");
 objectPojo.setNo("8901");
 objectPojo.setDesc("mac ");
 objectPojo.setPost("Developer");
 objectPojo.setIsSelected(false);
 addList.add(objectPojo);
 return addList;
 }
}
Android developers can just copy and paste the codes to get the listview customized as shown above. 





217 comments:

«Oldest   ‹Older   201 – 217 of 217
Hira Nakliyat said...

Sultangazi evden eve nakliyat ailesi olarak yaptığınız çalışmalarının devamını bekler bizede yeni yorum kapıları açmanızı temenni ederiz görüşmek üzere hoş kalın hoşça kalın.
https://www.sultangazievdenevenakliyat.net

Justin Tyler said...

On the off chance that the QB online login issue continues, check for rejuvenates and establish the earlier series variant on your internet browser.

Find a ProAdvisor

Singapore Translators said...

Singapore Translators provide translation services singapore in 200+ languages we have 500 members team and provide your trasnalated document in 24 hours.

Anonymous said...
This comment has been removed by the author.
Al Eizba said...

Thanks for posting a valuable and informative blog. Further, If you are looking to buy villas in Nad Al Sheba 4 visit our website

Sophie Martin said...

Nice piece of information. Kindly check out my new blog
Smadav Pro Crack 2023
Format Factory Crack 2023
Bulk Image Downloader Patch 2023
IObit Uninstaller Patch 2023
Wincatalog Crack 2023

Cyfuture Cloud said...

This blog in very informative for us

ddrr aass said...


Massage in Paris |

Massage in Berlin |


Massage in Prague |

massage in milan |


Massage in Kiev |

Massage in Kuala Lumpur |


Anonymous said...

informative blog, got good content from uhh , help me alot thanks. I juss tried something informative here reactjs classes in pune

William Jack said...

Well-written information. Very impressive and to the point
Have a look at my new blog
Tweakbit Driver Updater Crack 2023

Makkaan said...

Great post thanks for sharing such like these post very informative also check Real Estate investment in islamabad

Hones-T Painting LLC said...

At Bavvo, access top affordable Skill development and business management courses designed to boost your skills and finances in Dubai

Lol Prikol said...

Thank you!

Suired said...

An Android custom ListView allows for tailored display of list items with various layouts and interactivity, enhancing user interface design. To fully utilize such features in your app development, try to register new tm sim for seamless data connectivity.


Katti said...

Gulf VPN stands out as a top-tier VPN service, offering a winning combination of speed, security, and convenience. Whether you're concerned about online privacy, want to bypass geo-restrictions, or simply need a reliable VPN for everyday use, Gulf VPN delivers on all fronts. With its robust features, user-friendly interface, and commitment to customer satisfaction, Gulf VPN is your trusted companion for safe and unrestricted internet access.
https://play.google.com/store/apps/details?id=com.botsolutions.gulfvpn&pcampaignid=web_share

johni said...

SIM registration, short for Subscriber Identity Module registration, is a mandatory process in many countries where mobile phone users are required to register their SIM cards with their personal information. This process typically involves submitting identification details such as a government-issued ID, and in some cases, biometric data like fingerprints or facial recognition. The main purpose of sim registration process is to link each SIM card to the identity of the user, making it easier for authorities to track mobile phone activities and prevent misuse, such as fraud, terrorism, and other criminal activities.

www.galaxyeduworld.com/ said...

Great Article food and nutrition colleges
Bangalore is home to some of the top institutions offering courses in Food and Nutrition, blending scientific knowledge with practical expertise in dietetics, food science, and nutrition management. These programs are designed to provide students with a deep understanding of the connection between food, health, and disease, preparing them for diverse careers in healthcare, research, food production, and dietary planning.

«Oldest ‹Older   201 – 217 of 217   Newer› Newest»