SlideShare a Scribd company logo
Android
ListView
1
Sourabh Sahu
Under the Composite palette drag a
ListView onto the activity, give it an
id
2
Right click on res/layout choose
New/Other (or XML if it is there)
3
Choose Android/Android XML File
(not just XML file) and click Next
4
Give the file a name – remember the file name
convention small letters numbers underscores and
periods. Choose a Root Element. Click Finish.
5
Resulting XML in Graphical Layout
view and xml view
6
Code for String array, ArrayAdapter,
and ListView
7
Result so far in emulator. Note that
the list items are clickable
8
Code for the OnItemClickListener
9
Result
10
Custom List View
Creating a View template
Let’s create a xml layout that presents the items in a row in a
customised way.
Row_view.xml
11
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Marshmallow"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_marginTop="5dp"
android:text="Android 6.0"
android:textColor="@android:color/black" />
<ImageView
android:id="@+id/item_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@android:drawable/ic_dialog_info" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
12
13
     android:id="@+id/version_heading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="API: "
            android:textColor="@android:color/black"
            android:textStyle="bold" />
 
        <TextView
            android:id="@+id/version_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="23"
            android:textAppearance="?
android:attr/textAppearanceButton"
            android:textColor="@android:color/black"
            android:textStyle="bold" />
 
    </LinearLayout>
 
</RelativeLayout>
Create DataModel Class
14
public class DataModel {
 
    String name;
    String type;
    String version_number;
    String feature;
 
    public DataModel(String name, String type, String
version_number, String feature ) {
        this.name=name;
        this.type=type;
        this.version_number=version_number;
        this.feature=feature;
 
    }
 
    public String getName() {
        return name;
    }
     
15
    public String getType() {
        return type;
    }
     
    public String
getVersion_number() {
        return version_number;
    }
     
    public String getFeature() {
        return feature;
    }
     
}
Create an adapter
16
public class CustomAdapter extends ArrayAdapter<DataModel>
implements View.OnClickListener{
 
    private ArrayList<DataModel> dataSet;
    Context mContext;
 
    // View lookup cache
    private static class ViewHolder {
        TextView txtName;
        TextView txtType;
        TextView txtVersion;
        ImageView info;
    }
 
    public CustomAdapter(ArrayList<DataModel> data, Context context) {
        super(context, R.layout.row_item, data);
        this.dataSet = data;
        this.mContext=context;
 
    }
 
17
de
c void onClick(View v) {
position=(Integer) v.getTag();
ct object= getItem(position);
Model dataModel=(DataModel)object;
ch (v.getId())
se R.id.item_info:
nackbar.make(v, "Release date " +dataModel.getFeature(), Snackbar.L
     .setAction("No action", null).show();
reak;
te int lastPosition = -1;
18
@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        DataModel dataModel = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        ViewHolder viewHolder; // view lookup cache stored in tag
 
        final View result;
 
        if (convertView == null) {
 
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.row_item, parent, false);
            viewHolder.txtName = (TextView) convertView.findViewById(R.id.name);
            viewHolder.txtType = (TextView) convertView.findViewById(R.id.type);
            viewHolder.txtVersion = (TextView)
convertView.findViewById(R.id.version_number);
            viewHolder.info = (ImageView) convertView.findViewById(R.id.item_info);
 
            result=convertView;
 
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            result=convertView;
        }
19
 Animation animation = AnimationUtils.loadAnimation(mContext, (position >
lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
        result.startAnimation(animation);
        lastPosition = position;
 
        viewHolder.txtName.setText(dataModel.getName());
        viewHolder.txtType.setText(dataModel.getType());
        viewHolder.txtVersion.setText(dataModel.getVersion_number());
        viewHolder.info.setOnClickListener(this);
        viewHolder.info.setTag(position);
        // Return the completed view to render on screen
        return convertView;
    }
}
Add this to your onCreate
method
20
 listView=(ListView)findViewById(R.id.list);
 
        dataModels= new ArrayList<>();
 
        dataModels.add(new DataModel("Apple Pie", "Android 1.0", "1","September 23, 2008"));
        dataModels.add(new DataModel("Banana Bread", "Android 1.1", "2","February 9, 2009"));
        dataModels.add(new DataModel("Cupcake", "Android 1.5", "3","April 27, 2009"));
        dataModels.add(new DataModel("Donut","Android 1.6","4","September 15, 2009"));
        dataModels.add(new DataModel("Eclair", "Android 2.0", "5","October 26, 2009"));
        dataModels.add(new DataModel("Froyo", "Android 2.2", "8","May 20, 2010"));
        dataModels.add(new DataModel("Gingerbread", "Android 2.3", "9","December 6, 2010"));
        dataModels.add(new DataModel("Honeycomb","Android 3.0","11","February 22, 2011"));
        dataModels.add(new DataModel("Ice Cream Sandwich", "Android 4.0", "14","October 18,
2011"));
        dataModels.add(new DataModel("Jelly Bean", "Android 4.2", "16","July 9, 2012"));
        dataModels.add(new DataModel("Kitkat", "Android 4.4", "19","October 31, 2013"));
        dataModels.add(new DataModel("Lollipop","Android 5.0","21","November 12, 2014"));
        dataModels.add(new DataModel("Marshmallow", "Android 6.0", "23","October 5, 2015"));
 
        adapter= new CustomAdapter(dataModels,getApplicationContext());
 
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View
view, int position, long id) {
 
                DataModel dataModel= dataModels.get(position);
 
                Snackbar.make(view, dataModel.getName()
+"n"+dataModel.getType()+" API:
"+dataModel.getVersion_number(), Snackbar.LENGTH_LONG)
                        .setAction("No action", null).show();
            }
        });
    }
21
22

More Related Content

What's hot (20)

DOCX
Android list view tutorial by Javatechig
Javatechig Resources for Developers
 
PPTX
ListView and Custom ListView on Android Development [Thai]
Somkiat Khitwongwattana
 
PPTX
Xml part4
NOHA AW
 
PPT
Lecture Slides for List Views [Android ]
Nehil Jain
 
PPTX
Hira
hira elahi
 
PPTX
Oracle apps financial online training
magnifics
 
PPTX
Introduction to Listview in Android
technoguff
 
PPTX
Sitecore Knowledge Transfer 2018 (Template) day-2
Manish Puri
 
PDF
Web Design Course: CSS lecture 5
Gheyath M. Othman
 
PPT
Symfony Admin Generator - generator.yml
Ravi Mone
 
PPTX
Introduction to CSS
Shehzad Yaqoob
 
PDF
Web Design Course: CSS lecture 1
Gheyath M. Othman
 
PPTX
Android Training (AdapterView & Adapter)
Khaled Anaqwa
 
PDF
HTML - part 1
Fahad Masood
 
PPT
Entity Attribute Value (Eav)
Tâm
 
PDF
Android ui adapter
Krazy Koder
 
PPT
Android - Values folder
Maneesha Caldera
 
KEY
DRYing Up Rails Views and Controllers
James Gray
 
PPT
Jstl &amp; El
Bharat17485
 
Android list view tutorial by Javatechig
Javatechig Resources for Developers
 
ListView and Custom ListView on Android Development [Thai]
Somkiat Khitwongwattana
 
Xml part4
NOHA AW
 
Lecture Slides for List Views [Android ]
Nehil Jain
 
Oracle apps financial online training
magnifics
 
Introduction to Listview in Android
technoguff
 
Sitecore Knowledge Transfer 2018 (Template) day-2
Manish Puri
 
Web Design Course: CSS lecture 5
Gheyath M. Othman
 
Symfony Admin Generator - generator.yml
Ravi Mone
 
Introduction to CSS
Shehzad Yaqoob
 
Web Design Course: CSS lecture 1
Gheyath M. Othman
 
Android Training (AdapterView & Adapter)
Khaled Anaqwa
 
HTML - part 1
Fahad Masood
 
Entity Attribute Value (Eav)
Tâm
 
Android ui adapter
Krazy Koder
 
Android - Values folder
Maneesha Caldera
 
DRYing Up Rails Views and Controllers
James Gray
 
Jstl &amp; El
Bharat17485
 

Similar to Android ListView and Custom ListView (20)

PDF
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 
PDF
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 
PDF
List Views
Ahsanul Karim
 
PPTX
List adapter with multiple objects
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Building your first android app using Xamarin
Gill Cleeren
 
PPTX
Building your first android app using xamarin (Gill Cleeren)
Visug
 
PPTX
chp 4 UI component hdjdjdduudfinalt.pptx
Good490110
 
PDF
Android UI Development: Tips, Tricks, and Techniques
Edgar Gonzalez
 
PDF
Android UI Tips, Tricks and Techniques
Marakana Inc.
 
PPTX
Binding data with the AdapterView class.pptx
Gowthami476224
 
PDF
Android - Build User Interface
MingHo Chang
 
PPT
Android 2
San Bunna
 
PPTX
Android Chapter 4 part2 Types of View and View group
VaibhavSarode16
 
PDF
Listview and Adapter
Arif Huda
 
PDF
Android Design Patterns
Godfrey Nolan
 
DOCX
Leture5 exercise onactivities
maamir farooq
 
DOCX
Lecture exercise on activities
maamir farooq
 
PDF
Tips & Tricks to spice up your Android app
Jérémie Laval
 
DOC
Android Application DevlopmentManual-1.doc
KiranmaiBejjam1
 
PPTX
MDAD 5 - Android - Lists, adapters and recycling
Alexandru Radovici
 
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 
List Views
Ahsanul Karim
 
List adapter with multiple objects
baabtra.com - No. 1 supplier of quality freshers
 
Building your first android app using Xamarin
Gill Cleeren
 
Building your first android app using xamarin (Gill Cleeren)
Visug
 
chp 4 UI component hdjdjdduudfinalt.pptx
Good490110
 
Android UI Development: Tips, Tricks, and Techniques
Edgar Gonzalez
 
Android UI Tips, Tricks and Techniques
Marakana Inc.
 
Binding data with the AdapterView class.pptx
Gowthami476224
 
Android - Build User Interface
MingHo Chang
 
Android 2
San Bunna
 
Android Chapter 4 part2 Types of View and View group
VaibhavSarode16
 
Listview and Adapter
Arif Huda
 
Android Design Patterns
Godfrey Nolan
 
Leture5 exercise onactivities
maamir farooq
 
Lecture exercise on activities
maamir farooq
 
Tips & Tricks to spice up your Android app
Jérémie Laval
 
Android Application DevlopmentManual-1.doc
KiranmaiBejjam1
 
MDAD 5 - Android - Lists, adapters and recycling
Alexandru Radovici
 
Ad

More from Sourabh Sahu (20)

PPTX
Understanding GIT and Version Control
Sourabh Sahu
 
PPTX
Python Seaborn Data Visualization
Sourabh Sahu
 
PPTX
Mongo db Quick Guide
Sourabh Sahu
 
PPTX
Python Course
Sourabh Sahu
 
PPT
SeekBar in Android
Sourabh Sahu
 
PPTX
Android layouts
Sourabh Sahu
 
PPT
Activities
Sourabh Sahu
 
PPT
Android project architecture
Sourabh Sahu
 
PPT
Shared preferences
Sourabh Sahu
 
PPT
Content Providers in Android
Sourabh Sahu
 
PPT
SQLITE Android
Sourabh Sahu
 
PPT
Calendar, Clocks, DatePicker and TimePicker
Sourabh Sahu
 
PPT
Progress Dialog, AlertDialog, CustomDialog
Sourabh Sahu
 
PPT
AutocompleteTextView And MultiAutoCompleteTextView
Sourabh Sahu
 
PPT
Web view
Sourabh Sahu
 
PPT
Parceable serializable
Sourabh Sahu
 
PPT
Android Architecture
Sourabh Sahu
 
PPT
Android Installation Testing
Sourabh Sahu
 
PPT
Android Installation
Sourabh Sahu
 
PPTX
Learn Android
Sourabh Sahu
 
Understanding GIT and Version Control
Sourabh Sahu
 
Python Seaborn Data Visualization
Sourabh Sahu
 
Mongo db Quick Guide
Sourabh Sahu
 
Python Course
Sourabh Sahu
 
SeekBar in Android
Sourabh Sahu
 
Android layouts
Sourabh Sahu
 
Activities
Sourabh Sahu
 
Android project architecture
Sourabh Sahu
 
Shared preferences
Sourabh Sahu
 
Content Providers in Android
Sourabh Sahu
 
SQLITE Android
Sourabh Sahu
 
Calendar, Clocks, DatePicker and TimePicker
Sourabh Sahu
 
Progress Dialog, AlertDialog, CustomDialog
Sourabh Sahu
 
AutocompleteTextView And MultiAutoCompleteTextView
Sourabh Sahu
 
Web view
Sourabh Sahu
 
Parceable serializable
Sourabh Sahu
 
Android Architecture
Sourabh Sahu
 
Android Installation Testing
Sourabh Sahu
 
Android Installation
Sourabh Sahu
 
Learn Android
Sourabh Sahu
 
Ad

Recently uploaded (20)

PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
community health nursing question paper 2.pdf
Prince kumar
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 

Android ListView and Custom ListView