Monday, April 19, 2010

Creating dynamic Customized List view.

This article shows to create a dynamic custom view as a list. i have created a custom adapter and explore to create a custom listview.
As a result i have created the below method. In this blog will help you to create a customized list view.

Create two layout
    1. main layout
    2. custom view layout.

MainLayout - which is used to define the scroll view.
    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView android:id="@+id/ScrollView01"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="fill_parent" android:layout_width="fill_parent">
        <LinearLayout android:orientation="vertical"
            android:layout_width="fill_parent" android:id="@+id/mylayout1"
            android:layout_height="wrap_content">
        </LinearLayout>
    </ScrollView>


Custom Layout - Which Consists of actual design.
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <TextView android:text="@+id/TextView01" android:id="@+id/TextView01"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
        <Button android:text="@+id/Button01" android:id="@+id/Button01"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    </LinearLayout>




Create a Activity Class

public class DynamicListActivity extends Activity {
    /**
     * @see android.app.Activity#onCreate(Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listviewmain);
    LinearLayout l = (LinearLayout) findViewById(R.id.mylayout1);
    LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i = 0; i < 5; i++) {
        View customView = linflater.inflate(R.layout.customlistviewitem,
            null);
        TextView tv = (TextView) customView.findViewById(R.id.TextView01);
        Button btn = (Button) customView.findViewById(R.id.Button01);
        tv.setId(i);
        btn.setId(i);
        tv.setText("Location:" + i);
        btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(DynamicListActivity.this, v.getId() + "",
                Toast.LENGTH_LONG).show();
        }
        });
        l.addView(customView);
    }
    }
}


14 comments:

  1. OMG thank you so much - I have been going through the internet for this kind of solution for ages - and this is the only one that has worked for me.

    ReplyDelete
  2. This is great. Was wondering if there is any way to put the divider between rows.

    ReplyDelete
  3. OK - the following URL is helpful in answering the question I posted above:
    http://www.anddev.org/add_a_horizontal_line_at_the_top_of_a_linear_layout-t9698.html

    ReplyDelete
  4. Its Great.... Working Good.... Thanks a million, Ganesh Shanmugam Pillai

    ReplyDelete
  5. Its a nice,simple tutorial. I like it. Thanks.

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

    ReplyDelete
  7. Hey , Thanks for your post, it is helpful,
    but I have a problem. it is that only one item appear. i should have 3 .
    I iterated through an array. iterates array.size times and l.addView(customView) is adding I suppose all customViews. But only the first item appear.
    what am I missing ?
    Thanks for the Help .

    ReplyDelete
  8. louz,

    Ensure that you set your LinearLayout that you're adding the custom views on to vertical orientation. See if that helps :)

    ReplyDelete
  9. Works like a charm! Thanks, Ganesan.

    ReplyDelete
  10. Very helpful. I've been looking for an example like this for awhile now. Thanks!

    ReplyDelete
  11. this helped me alot when i was stucking with create multilevel expandable listview. Now i just add new Viewgroup as List item. But this solution can work with great performance if the list help less item. If it has a big amount of items the building view performance not great anymore :)

    ReplyDelete
  12. Actually when someone doesn't understand then its up to other viewers that they will assist, so here it takes place.

    Here is my web site - 대구오피

    ReplyDelete
  13. Nice post. Appconsultio offers ongoing maintenance and support services to keep the apps up-to-date and functioning smoothly as new versions of the Android OS are released or as the app's requirements evolve. Our Mobile Application Developers in Pune have crafted an innovative app that is revolutionizing the way people interact with technology.

    ReplyDelete