list-view-android

ListView in android example with custom layout

ListView in android example with custom layout

A view group that show or display list of scrollable items. The list data are populated from array or other collection by Adapter class.Here I have one Activity class named  "ListViewActivity.java" and respective xml file "activity_list_view.xml" ,an adapter class named "AndroidListAdapter.java" and one data collection class "ListCollection.java" and a custom listview item layout named "list_item.xml".

we use local data here.

 

Now we will check the source code.

1. ListViewActivity.java


 

package com.androidfromhome.listview;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class ListViewActivity extends Activity {

    private ListView lv_android;
    private AndroidListAdapter list_adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        
        ListCollection.listCollectionArr=new ArrayList<ListCollection>();
        ListCollection.listCollectionArr.add(new ListCollection("Butterfly", "Butterflies are " +
                "part of the class of Insects in the order Lepidoptera", R.drawable.butterfly));
        ListCollection.listCollectionArr.add(new ListCollection("Green Scenery", "Green Scenery " +
                "is good for health", R.drawable.green_scenery));
        ListCollection.listCollectionArr.add(new ListCollection("Leaf", "A leaf is an organ " +
                "of a vascular plant and is the principal lateral appendage of the stem.", R.drawable.leaf));
        ListCollection.listCollectionArr.add(new ListCollection("One Rose", "A rose is a woody perennial " +
                "of the genus Rosa, within the family Rosaceae", R.drawable.one_rose));
        ListCollection.listCollectionArr.add(new ListCollection("Open Sky", "The sky (or celestial dome) is everything that lies above the surface of " +
                "the Earth, including the atmosphere and outer space.", R.drawable.sky));
        
    
        getWidget();
    }

    
    
    public void getWidget(){
        
        lv_android = (ListView) findViewById(R.id.lv_android);
        list_adapter=new AndroidListAdapter(ListViewActivity.this,R.layout.list_item, ListCollection.listCollectionArr);
        lv_android.setAdapter(list_adapter);
        
    }
    
}

 


 

2. AndroidListAdapter.java


 

package com.androidfromhome.listview;

import java.util.ArrayList;

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

public class AndroidListAdapter extends ArrayAdapter<ListCollection>{
    
    private final Context context;
    private final ArrayList<ListCollection> values;
    private ViewHolder viewHolder;
    private final int resourceId;

    public AndroidListAdapter(Context context, int resourceId,ArrayList<ListCollection> values) {
        super(context, resourceId, values);
        // TODO Auto-generated constructor stub
        
        this.context = context;
        this.values = values;
        this.resourceId = resourceId;
    }
    
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(resourceId, parent, false);
            
                
            viewHolder = new ViewHolder();
            viewHolder.tv_msg = (TextView) convertView.findViewById(R.id.tv_msg);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);
            
            
                convertView.setTag(viewHolder);
            
            
        }else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ListCollection list_obj=values.get(position);
        viewHolder.tv_msg.setText(list_obj.message);
        viewHolder.tv_title.setText(list_obj.title);
        viewHolder.iv_image.setImageResource(list_obj.image);
            
        return convertView;
    }
    
    
    
    
    
    public class ViewHolder {

         TextView tv_msg;
         ImageView iv_image;
         TextView tv_title;

    }

}


 

3. ListCollection.java


 

 

package com.androidfromhome.listview;

import java.util.ArrayList;

public class ListCollection {

    public static ArrayList<ListCollection> listCollectionArr=null;

    public String title="";
    public String message="";
    public int image=0;
    
    public ListCollection(String title,String message,int image){
        this.title=title;
        this.message=message;
        this.image=image;
        
    }
    
}


Now look at the xml layout code

a. activity_list_view.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.androidfromhome.listview.ListViewActivity" >
    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A758AB"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="Android From Home"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />
    </LinearLayout>

    <ListView
        android:id="@+id/lv_android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

 


b. list_item.xml


 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="#ffffff"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:gravity="center"
        android:padding="6dp" >

        <FrameLayout
            android:layout_width="62dp"
            android:layout_height="62dp"
            android:layout_margin="2dp" >

            <ImageView
                android:id="@+id/iv_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:padding="3dp" />

        </FrameLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tv_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:text="TextView"
                android:textColor="#000000"
                android:textSize="16dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tv_msg"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:maxLength="40"
                android:padding="2dp"
                android:text="Some Text Goes here.."
                android:textColor="#343434"
                android:textSize="12dp"
                android:textStyle="bold" />

        </LinearLayout>

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/arrow_list" />

    </LinearLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#dfdfdf" />

</LinearLayout>


Download Source Code

 

 

Related Post:-

Email send from android application example

Custom Alert Dialog in android with custom xml layout

Internet connection checking in android device

Custom Toast example in android example

Leave a Reply

Your email address will not be published. Required fields are marked *

Get Widget