Saturday, September 28, 2013

Dynamic List-View Demo in Android | Dynamically Increase row of list-view in Android | Custom Dynamic list-view in Android

Hello Friends!

Today I am going to post a logical code for add item in list-view dynamically. And increase size of row dynamically.


1)CustomListView.java
package com.manish.customlistview;

import java.util.ArrayList;

import android.os.Bundle;
import android.widget.ListView;
import android.app.Activity;
/**
 *
 * @author manish
 *
 */

public class CustomListView extends Activity {
       ArrayList<Item> imageArry = new ArrayList<Item>();
       CustomImageAdapter adapter;

       @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
              // add image and text in arraylist
              String Video[]={"xyz","abcd"};
              String Song[]={"xyz","abcd","pqerttt","yuioo"};
              String Movi[]={"xyz","abcd","qwer","yuioo","tyyuu","ggggggg","yuioo","tyyuu","ggggggg"};
             
              imageArry.add(new Item(R.drawable.facebook, Song));
              imageArry.add(new Item(R.drawable.outlook, Video));
              imageArry.add(new Item(R.drawable.google, Movi));
             
              // add data in contact image adapter
              adapter = new CustomImageAdapter(this, R.layout.list, imageArry);
              ListView dataList = (ListView) findViewById(R.id.list);
              dataList.setAdapter(adapter);

       }

}


2)CustomImageAdapter.java
package com.manish.customlistview;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
 *
 * @author manish
 *
 */

public class CustomImageAdapter extends ArrayAdapter<Item> {
       Context context;
       int layoutResourceId;
       LinearLayout linearMain;
       ArrayList<Item> data = new ArrayList<Item>();

       public CustomImageAdapter(Context context, int layoutResourceId,
                     ArrayList<Item> data) {
              super(context, layoutResourceId, data);
              this.layoutResourceId = layoutResourceId;
              this.context = context;
              this.data = data;
       }

       @Override
       public View getView(int position, View convertView, ViewGroup parent) {
              View row = convertView;

              if (row == null) {
                     LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                     row = inflater.inflate(layoutResourceId, parent, false);

                     linearMain = (LinearLayout) row.findViewById(R.id.lineraMain);

                     Item myImage = data.get(position);
                     for (int j = 0; j < myImage.getName().length; j++) {
                           TextView label = new TextView(context);
                           label.setText(myImage.name[j]);
                           linearMain.addView(label);
                     }
                     ImageView image = new ImageView(context);
                     int outImage = myImage.image;
                     image.setImageResource(outImage);
                     linearMain.addView(image);
              }

              return row;

       }

}

3)Item.java
package com.manish.customlistview;
/**
 *
 * @author manish
 *
 */
public class Item {

       int image;
       String name[];
      
       public Item(int image, String[] name) {
              super();
              this.image = image;
              this.name = name;
       }
       public int getImage() {
              return image;
       }
       public void setImage(int image) {
              this.image = image;
       }
       public String[] getName() {
              return name;
       }
       public void setName(String[] name) {
              this.name = name;
       }

}

4)main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:layout_weight="0.55" >
    </ListView>

</LinearLayout>

5)list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/lineraMain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dp" >

</LinearLayout>

6)AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.manish.customlistview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.manish.customlistview.CustomListView"
            android:label="@string/title_activity_custom_list_view" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>

Download Zip Code

Thanks!

18 comments:

  1. Manish nice profile pic :)
    And blog too...

    ReplyDelete
    Replies
    1. Thanks for post and for pic, like on my facebook page :)

      Delete
  2. very good and helpful code !! :)

    ReplyDelete
  3. hi, this is not related to the post, but how do i set the listview height fixed for all the rows? some of my listrow can haveve 1 line some have 2-3 lines depending on the TEXT i get from feeds and depending on the orientation. i mean text can occupy 3 lines in portrait but 1 line in landscape. so how can i have each occupy the same height ?

    ReplyDelete
    Replies
    1. Hi, why not this post related? I am creating listview item like image and textview dynamically in java code then why not it's related to post? and one more thing you can create any number of size row dynamically.
      And yes if it is not related to you, you can search on google for other post.

      Thanks,

      Delete
  4. Great Help Thanks! May I also ask how can i display my text and image from json to this view? do you have sample of that? big help thanks

    ReplyDelete
  5. I am sorry Kevin i don't have demo for parsing JSON but it is very simple you can search it on google. and for display in that list you can put that instead of imageArray.

    ReplyDelete
  6. Manish im new to androoid programming i want to learn how to send a httprequest to a server and then recieve a json response ans then i want to genereate a listview based on the json response

    ReplyDelete
  7. thanks a lot dude...very simple tut..!

    ReplyDelete
  8. this is not working if i have more then 3 or 4 view items(:

    ReplyDelete
  9. means not working if convert view is not null..if convert view is being recycled then it gives wiered behaviour

    ReplyDelete
  10. Regarded Sir/Mam, Good Morning. I am trying to make an ExpandableListView. My requirement i already attached you. how can i insert value in that l,list. What I made for this, I already attached all files. complete source code was not accepted by google server so i am sending you individually. Please mam let me know how to display child's value in form of total. ... Suppose I have one parent named as Cloth, so structure should be like this

    Cloth (20)
    |
    |------Pant (15)
    | |
    | |----Denim (5)
    | |----Shirt (10)
    | |
    |-------Jeans ( 5 )

    I have to display total . How to do this..

    ReplyDelete
    Replies
    1. Check this post and modify according to your need-
      http://www.androidhub4you.com/2014/07/android-multilevel-expandable-listview.html

      Delete
  11. Hi Manish!
    I did the same thing, making new views...
    But the problem is when listview is scrolled faster, it skips some of the dynamically created views :/

    ReplyDelete
  12. Amazing, thank you

    Can you explain how to use for SimpleCursorAdapter

    Database like this :
    _id > company >apps
    0 Google Gmail,youtube,map..

    ReplyDelete
  13. Amazing, thank you

    Can you explain how to use for SimpleCursorAdapter

    Database like this :
    _id | company |apps
    0 |Google |Gmail,youtube,map..

    ReplyDelete