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

PPTX
Android styles and themes
PPTX
form view
PPTX
Xml part3
PPSX
Session six ASP.net (MVC) View
PPTX
Xml part1
PPTX
Xml part2
PDF
HTML Foundations, pt 2
PPTX
Attributes
Android styles and themes
form view
Xml part3
Session six ASP.net (MVC) View
Xml part1
Xml part2
HTML Foundations, pt 2
Attributes

What's hot (20)

DOCX
Android list view tutorial by Javatechig
PPTX
ListView and Custom ListView on Android Development [Thai]
PPTX
Xml part4
PPT
Lecture Slides for List Views [Android ]
PPTX
PPTX
Oracle apps financial online training
PPTX
Introduction to Listview in Android
PPTX
Sitecore Knowledge Transfer 2018 (Template) day-2
PDF
Web Design Course: CSS lecture 5
PPT
Symfony Admin Generator - generator.yml
PPTX
Introduction to CSS
PDF
Web Design Course: CSS lecture 1
PPTX
Android Training (AdapterView & Adapter)
PDF
HTML - part 1
PPT
Entity Attribute Value (Eav)
PDF
Android ui adapter
PPT
Android - Values folder
KEY
DRYing Up Rails Views and Controllers
PPT
Jstl &amp; El
Android list view tutorial by Javatechig
ListView and Custom ListView on Android Development [Thai]
Xml part4
Lecture Slides for List Views [Android ]
Oracle apps financial online training
Introduction to Listview in Android
Sitecore Knowledge Transfer 2018 (Template) day-2
Web Design Course: CSS lecture 5
Symfony Admin Generator - generator.yml
Introduction to CSS
Web Design Course: CSS lecture 1
Android Training (AdapterView & Adapter)
HTML - part 1
Entity Attribute Value (Eav)
Android ui adapter
Android - Values folder
DRYing Up Rails Views and Controllers
Jstl &amp; El
Ad

Similar to Android ListView and Custom ListView (20)

PDF
Day 8: Dealing with Lists and ListViews
PDF
Day 8: Dealing with Lists and ListViews
PDF
List Views
PPTX
List adapter with multiple objects
PPTX
Building your first android app using Xamarin
PPTX
Building your first android app using xamarin (Gill Cleeren)
PPTX
chp 4 UI component hdjdjdduudfinalt.pptx
PDF
Android UI Tips, Tricks and Techniques
PDF
Android UI Development: Tips, Tricks, and Techniques
PPTX
Binding data with the AdapterView class.pptx
PDF
Android - Build User Interface
PPT
Android 2
PPTX
Android Chapter 4 part2 Types of View and View group
PDF
Listview and Adapter
PDF
Android Design Patterns
DOCX
Lecture exercise on activities
DOCX
Leture5 exercise onactivities
PDF
Tips & Tricks to spice up your Android app
DOC
Android Application DevlopmentManual-1.doc
PPTX
MDAD 5 - Android - Lists, adapters and recycling
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
List Views
List adapter with multiple objects
Building your first android app using Xamarin
Building your first android app using xamarin (Gill Cleeren)
chp 4 UI component hdjdjdduudfinalt.pptx
Android UI Tips, Tricks and Techniques
Android UI Development: Tips, Tricks, and Techniques
Binding data with the AdapterView class.pptx
Android - Build User Interface
Android 2
Android Chapter 4 part2 Types of View and View group
Listview and Adapter
Android Design Patterns
Lecture exercise on activities
Leture5 exercise onactivities
Tips & Tricks to spice up your Android app
Android Application DevlopmentManual-1.doc
MDAD 5 - Android - Lists, adapters and recycling
Ad

More from Sourabh Sahu (20)

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

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
Cell Types and Its function , kingdom of life
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Lesson notes of climatology university.
PPTX
Pharma ospi slides which help in ospi learning
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Institutional Correction lecture only . . .
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
human mycosis Human fungal infections are called human mycosis..pptx
O7-L3 Supply Chain Operations - ICLT Program
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Classroom Observation Tools for Teachers
Cell Types and Its function , kingdom of life
STATICS OF THE RIGID BODIES Hibbelers.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Lesson notes of climatology university.
Pharma ospi slides which help in ospi learning
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
102 student loan defaulters named and shamed – Is someone you know on the list?
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Institutional Correction lecture only . . .
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

Android ListView and Custom ListView