Monday, January 30, 2012

Custom adapter for ListView to create bubble shaped list rows


For this example, I have created a ListView which has a custom background, and custom rows representing a conversation in bubble format.

Here's the screenshot to make things clear:



Find the whole source code at this link.

The whole concept works like this-
  • You create two layouts:
    • One for Even rows
    • One for Odd rows
  • These layouts are designed keeping in mind that they'll be used as layouts of a row.
  • Your code has an array containing the data that will be displayed in the list view.
  • Inside your code, write a simple class extending BaseAdapter.
    • In this class we implement following methods of Android.Widget.Adapter super class, in order to get things working:
      • getCount() - returns the total number of elements in your data array
      • getIterm() - returns the data item associated with the specified position in the data set. Not implemented at this point.
      • getItemId() - returns the row id associated with the specified position in the list. Not implemented at this point.
      • getView() - returns the row that has to be drawn. This is the method we're interested in the most. It will be called every time the ListView draws a new row.
        Here, you can control what gets drawn in a particular row, by selecting a layout and setting data into it.
        In our example, we are checking whether the position of the row is even or odd, and according to that, we are setting the row's layout.
  • I have also set a background to the layout which holds the list.
  • A list has a transparent background in normal conditions which makes you see the image that you set as the layout background, but when the List is scrolled, it doesn't remain transparent any more.
  • If you want you background image to be visible while scrolling too, set cacheColorHint of your ListView.

Refer to the project for more, and revert for feedback.

Download the source code here.
Happy Coding!

8 comments:

  1. hi, the link for the source code dont works. Could you upload the source code?

    Thank you very much.

    ReplyDelete
  2. Hi

    I am interested in the source file but it doesnt't work for me either (Error 404).

    Stefan

    ReplyDelete
  3. Could you please upload the source files?

    Seems it is not working!

    Thanks!

    Greg

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Great blog, the link is broken. Please pass new link.

    ReplyDelete
  6. It’s almost customary for all these products to promise what seem to be the white elephants in the world of human interactions; happy relationship, success in attracting men, a sudden peaking of confidence levels and much more.
    sb game hacker
    game killer apk
    geometry dash 2.0 apk
    download freedom apk
    true skate mod apk
    gunship battle hack apk
    clash royale mod apk

    ReplyDelete