ListView Containers. Resources. Creating a ListView

Similar documents
Creating a Custom ListView

Developing Android Applications Introduction to Software Engineering Fall Updated 1st November 2015

ListView (link) An ordered collection of selectable choices. key attributes in XML:

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

GUI Widget. Lecture6

Developing Android Applications

Accelerating Information Technology Innovation

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

Introductory Mobile App Development

Mobile Computing Fragments

INTRODUCTION COS MOBILE DEVELOPMENT WHAT IS ANDROID CORE OS. 6-Android Basics.key - February 21, Linux-based.

Mobile Programming Lecture 3. Resources, Selection, Activities, Intents

Practical 1.ListView example

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

Fragments. Fragments may only be used as part of an ac5vity and cannot be instan5ated as standalone applica5on elements.

Chapter 8 Positioning with Layouts

User Interface Development. CSE 5236: Mobile Application Development Instructor: Adam C. Champion Course Coordinator: Dr.

Adapter.

More Effective Layouts

Mobile Programming Lecture 2. Layouts, Widgets, Toasts, and Event Handling

Fragments. Lecture 11

Building User Interface for Android Mobile Applications II

Chapter 7: Reveal! Displaying Pictures in a Gallery

1. Simple List. 1.1 Simple List using simple_list_item_1

CHAPTER 4. Fragments ActionBar Menus

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 4. Workshop

ActionBar. import android.support.v7.app.actionbaractivity; public class MyAppBarActivity extends ActionBarActivity { }

Fragments. Lecture 10

Action Bar. Action bar: Top navigation bar at each screen The action bar is split into four different functional areas that apply to most apps.

Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment

Mobile Development Lecture 10: Fragments

Mobile User Interfaces

When programming in groups of people, it s essential to version the code. One of the most popular versioning tools is git. Some benefits of git are:

Android User Interface

Fragment Example Create the following files and test the application on emulator or device.

Android User Interface Android Smartphone Programming. Outline University of Freiburg

The Suggest Example layout (cont ed)

O X X X O O X O X. Tic-Tac-Toe. 5COSC005W MOBILE APPLICATION DEVELOPMENT Lecture 2: The Ultimate Tic-Tac-Toe Game

CS371m - Mobile Computing. User Interface Basics

Upon completion of the second part of the lab the students will have:

The drawable/tile empty.xml file

Agenda. Overview of Xamarin and Xamarin.Android Xamarin.Android fundamentals Creating a detail screen

Android App Development. Mr. Michaud ICE Programs Georgia Institute of Technology

EECS 4443 Mobile User Interfaces. More About Layouts. Scott MacKenzie. York University. Overview (Review)

Android Specifics. Jonathan Diehl (Informatik 10) Hendrik Thüs (Informatik 9)

Hello World. Lesson 1. Android Developer Fundamentals. Android Developer Fundamentals. Layouts, and. NonCommercial

Overview. Lecture: Implicit Calling via Share Implicit Receiving via Share Launch Telephone Launch Settings Homework

Multiple devices. Use wrap_content and match_parent Use RelativeLayout/ConstraintLayout Use configuration qualifiers

Mobila applikationer och trådlösa nät, HI1033, HT2013

EECS 4443 Mobile User Interfaces. More About Layouts. Scott MacKenzie. York University

10.1 Introduction. Higher Level Processing. Word Recogniton Model. Text Output. Voice Signals. Spoken Words. Syntax, Semantics, Pragmatics

Graphical User Interfaces

UI Fragment.

INTRODUCTION TO ANDROID

Lab 3. Accessing GSM Functions on an Android Smartphone

MVC Apps Basic Widget Lifecycle Logging Debugging Dialogs

Android Application Model I

Android development. Outline. Android Studio. Setting up Android Studio. 1. Set up Android Studio. Tiberiu Vilcu. 2.

Mobila applikationer och trådlösa nät, HI1033, HT2012

Eventually, you'll be returned to the AVD Manager. From there, you'll see your new device.

getcount getitem getitemid getview com.taxi Class MainActivity drawerlayout drawerleft drawerright...

Mobile Computing Practice # 2a Android Applications - Interface

Screen Slides. The Android Studio wizard adds a TextView to the fragment1.xml layout file and the necessary code to Fragment1.java.

LECTURE 08 UI AND EVENT HANDLING

Activities and Fragments

CS378 -Mobile Computing. User Interface Basics

CS371m - Mobile Computing. More UI Action Bar, Navigation, and Fragments

05. RecyclerView and Styles

ANDROID USER INTERFACE

Produced by. Mobile Application Development. Eamonn de Leastar

Android CardView Tutorial

Wireless Vehicle Bus Adapter (WVA) Android Library Tutorial

ANDROID APPS DEVELOPMENT FOR MOBILE GAME

Android Application Model I. CSE 5236: Mobile Application Development Instructor: Adam C. Champion, Ph.D. Course Coordinator: Dr.

User Interface: Layout. Asst. Prof. Dr. Kanda Runapongsa Saikaew Computer Engineering Khon Kaen University

CS371m - Mobile Computing. More UI Navigation, Fragments, and App / Action Bars

CS 4330/5390: Mobile Application Development Exam 1

Mobile Programming Lecture 7. Dialogs, Menus, and SharedPreferences

Mobile Computing Practice # 2c Android Applications - Interface

Lecture 6: Android XML, Inversion of Control, Timers (Handlers), Activity

Lab 1 - Setting up the User s Profile UI

TextView. A label is called a TextView. TextViews are typically used to display a caption TextViews are not editable, therefore they take no input

Initialising the Views (GameFragment) 5COSC005W MOBILE APPLICATION DEVELOPMENT Lecture 5: Ultimate Tic-Tac-Toe Game: The Interface (Part III)

Stanislav Rost CSAIL, MIT

Android Programming Lecture 2 9/7/2011

14.1 Overview of Android

CS 4518 Mobile and Ubiquitous Computing Lecture 4: WebView (Part 2) Emmanuel Agu

Programming with Android: Introduction. Layouts. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Programming with Android: Android for Tablets. Dipartimento di Scienze dell Informazione Università di Bologna

This lecture. The BrowserIntent Example (cont d)

EMBEDDED SYSTEMS PROGRAMMING UI and Android

Android Basics. Android UI Architecture. Android UI 1

Programming with Android: Widgets and Events. Luca Bedogni. Dipartimento di Scienze dell Informazione Università di Bologna

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Chapter 5 Flashing Neon FrameLayout

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Adapting to Data. Before we get to the fun stuff... Initial setup

Open Lecture Mobile Programming. Intro to Material Design

ES E 3 3 L a L b 5 Android development

Transcription:

ListView Containers Resources https://developer.android.com/guide/topics/ui/layout/listview.html https://developer.android.com/reference/android/widget/listview.html Creating a ListView A ListView is a container that displays a vertical list of other View objects. For example, we can create a ListView that contains a list of TextView objects where the text in each TextView comes from an array of Strings. A ListView is created by adding a ListView tag to the layout file for the activity in which we want to display the list. Be sure to give the ListView a unique id. <ListView android:id="@id/nameslistview" android:layout_width="match_parent" android:layout_height="wrap_content"...

When your ListView has more elements in it than can be displayed on the screen, a scrollbar automatically appears on the right. There is no need to put the ListView in a ScrollView layout! When the layout_height attribute is set to wrap_content, some of the objects in the list may appear behind other controls that are displayed on the screen above or below the ListView.. To avoid this problem you can place the ListView in a RelativeLayout or ConstraintLayout and specify that the ListView is above or below the other widgets in the activity. Populating ListViews with an Adapter In the oncreate() function of the activity that displays the ListView, we populate the ListView with content. To do so, we use an adapter. An adapter is an object that retrieves data from an external data source (e.g. array, ArrayList, Database) and creates one new View object for each element in the data source. Suppose we have an array named names. private String names[] = {"Alice, Bob, Ceci }; There are a number of built-in adapters. An ArrayAdapter is a generic class that takes an array of objects as an argument (the 3 rd argument) and creates a TextView object for each object in the array. To define an ArrayAdapter we call the ArrayAdapter s constructor, specifying the type of objects in the array, as shown below. ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.r.layout.simple_list_item_1, names);

The first argument is the activity that the ListView is in and the second argument is an Android layout provided in the SDK that displays each String in the array in a one line TextView object. Next, we instruct the ListView object to use the adapter by calling the ListView s setadapter() method. ListView listview = (ListView) findviewbyid(r.id.nameslistview); listview.setadapter(adapter); Handling OnClick Events for the TextView Objects in the ListView Each TextView element that is added to the ListView is clickable. To handle these events, after we create the adapter, we need to create a Listener object to listen for onitemclick events and handle them. Below is code that creates an instance of a Listener class. The class has one public method named onitemclicked(). When an item is clicked, onitemclicked() is called, passing into the method is a reference to the TextView object that was clicked. We can use this object to get the string that is in the TextView and, for example, display it in a toast. // Create a message handling object as an anonymous class. AdapterView.OnItemClickListener handler = new AdapterView.OnItemClickListener() { public void onitemclick(adapterview parent, View v, int position, long id) { String name = ((TextView) v).gettext().tostring(); Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();

}; } Last, we need to register the handler with the ListView. listview.setonitemclicklistener(handler); Displaying a ListView in a Fragment To display a ListView in a fragment you add the above code (with a few subtle changes) to the fragment s oncreateview() method. In the example below, we assume the fragment s layout is named names_list.xml, the ListView has an id named nameslistview, and the activity that displays the fragment is named MyViewPager. The subtle changes include the following: 1. You first need to inflate the fragment s xml file and save a reference to the root View object that the inflate() method returns and return it after you create the adapter. 2. Since oncreateview() is not an activity class you ll need to call getactivity() when you need a reference to the activity or a context. 3. If the names array is a field in the activity that displays the fragment, make the array public and static in the activity. For example, if the array is in a class named MyViewPager, then you can get a reference to the array using MyViewPager.names.

4. To get a reference to the ListView call findviewbyid() on the rootview object. @Override public View oncreateview(layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { View rootview = inflater.inflate(r.layout.names_list, container, false); ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.r.layout.simple_list_item_1, MyViewPager.names); ListView listview = (ListView) rootview.findviewbyid(r.id.nameslistview); listview.setadapter(adapter); AdapterView.OnItemClickListener handler = new AdapterView.OnItemClickListener() { public void onitemclick(adapterview parent, View v, int position, long id) { String name = ((TextView) v).gettext().tostring();

}; } /* Do something with the name */ Toast.makeText(getActivity().getApplicationContext(), name, Toast.LENGTH_SHORT).show(); listview.setonitemclicklistener(handler); } return rootview;