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

Slides:



Advertisements
Similar presentations
Programming with Android: Widgets and Events Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Advertisements

All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
 User Interface - Raeha Sandalwala.  Introduction to UI  Layouts  UI Controls  Menus and ‘Toasts’  Notifications  Other interesting UIs ◦ ListView.
Hello world Follow steps under the sections “Create an AVD” and “Create a New Android Project” at
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Android Application Development with Java UPenn CS4HS 2011 Chris Murphy
@2011 Mihail L. Sichitiu1 Android Introduction Hello Views Part 1.
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
Android Development (Basics)
Presenting Lists of Data. Lists of Data Issues involved – unknown number of elements – allowing the user to scroll Data sources – most common ArrayList.
Android: Layouts David Meredith
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
Better reference the original webpage :
Mobile Programming Lecture 1 Getting Started. Today's Agenda About the Eclipse IDE Hello, World! Project Android Project Structure Intro to Activities,
Android development the first app. Andoid vs iOS which is better? Short answer: neither Proponents on both sides For an iOS side, see this article on.
Chapter 5: Investigate! Lists, Arrays, and Web Browsers.
Mobile Programming Lecture 2 Layouts, Widgets, Toasts, and Event Handling.
Hello world Follow steps under the sections “Create an AVD” and “Create a New Android Project” at
Chapter 2: Simplify! The Android User Interface
Android Activities 1. What are Android Activities? Activities are like windows in an Android application An application can have any number of activities.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 5: Investigate! Android Lists, Arrays,
ANDROID – INTERFACE AND LAYOUT L. Grewe. Interfaces: Two Alternatives Code or XML  You have two ways you can create the interface(s) of your Application.
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Mobile Programming Lecture 6
DUE Hello World on the Android Platform.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
Frank Xu Gannon University.  Linear Layout  Relative Layout  Table Layout.
Mobile Computing Lecture#11 Adapters and Dialogs.
Chapter 2 The Android User Interface. Objectives  In this chapter, you learn to:  Develop a user interface using the TextView, ImageView, and Button.
INTRODUCTION TO ANDROID. Slide 2 Application Components An Android application is made of up one or more of the following components Activities We will.
ListView.
Cosc 5/4730 Android App Widgets. App Widgets App Widgets are miniature application views that can be embedded in other applications (such as the Home.
Create Navigation Drawer Team 2 Zhong Wang Jiaming Dong Philip Wu Lingduo Kong.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 7: Reveal! Displaying Pictures in a GridView.
Programming Mobile Applications with Android September, Albacete, Spain Jesus Martínez-Gómez.
Android Boot Camp for Developers Using Java, 3E
Chapter 7: Reveal! Displaying Pictures in a Gallery.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 2: Simplify! The Android User Interface.
Mobile Programming Lecture 5 Composite Views, Activities, Intents and Filters.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
Cosc 4730 Android Fragments. Fragments You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own.
Mobile Programming Lecture 12 HierarchyViewer, Linkify, Gestures, and Version Control.
Announcements Homework #2 will be posted after class due Thursday Feb 7, 1:30pm you may work with one other person No office hours tonight (sorry!) I will.
Mobile Programming Midterm Review
Mobile Programming Lecture 3 Debugging. Lecture 2 Review What widget would you use to allow the user to enter o a yes/no value o a range of values from.
Mobile Programming Lecture 7 Dialogs, Menus, and SharedPreferences.
MOBILE COMPUTING D10K-7D02 MC05: Android UI Design Dr. Setiawan Hadi, M.Sc.CS. Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran.
ListView and ExpandableListView
Recap of Part 1 Terminology Windows FormsAndroidMVP FormActivityView? ControlViewView? ?ServiceModel? Activities Views / ViewGroups Intents Services.
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Chapter 5: Investigate! Lists, Arrays, and Web Browsers.
Chapter 2: Simplify! The Android User Interface
Android Application Development 1 6 May 2018
CS499 – Mobile Application Development
several communicating screens
CS240: Advanced Programming Concepts
Android – Event Handling
Activities, Fragments, and Events
Android Development: Advanced Widgets using Adapters
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
Android Introduction Hello Views Part 1.
ITEC535 – Mobile Programming
Android Programming Lecture 6
Android ListView Demo.
ANDROID LISTS.
Android Lists and Fragments
Android Topics Custom ArrayAdapters
Android Topics Custom ArrayAdapters Creating an Event Listener
ListView ? BaseAdapter ?.
Mobile Programmming Dr. Mohsin Ali Memon.
Presentation transcript:

Mobile Programming Lecture 4 Resources, Selection, Activities, Intents

Announcement Reminder: o Homework 2 is due this Friday 11:59pm. You need to demo to me in class. o The second deadline is Sunday 11:59pm. You need to demo to me in Monday’s class. o Please export your project (.zip) and me. Midterm exam is on June 21 st. It is a Friday.

Lecture 3 Review Why shouldn't you use Toast to debug? How should you debug?

Agenda resources Setters and Getters Selection Views Android Components - Activity Android Components Intents More on the Android Manifest File

Resources Resources are stored in res/values/strings.xml You reference them in XML as

Resources You can also reference them in Java String buttonText = ""; Resources res = getResources(); buttonText = res.getString(R.string.string_name);

Referencing Resources You can store an array of strings if you need a collection instead of just a single string. This makes it resource instead resource! Open res/values/strings.xml in Resources view Add... > String Array o enter the name of the string_array, e.g. "countries" Repeat until you've added all values for the collection... o Add... > Item o Enter the name of the item

Setters and Getters for Views Most of a View's attributes can be set and get programmatically Example widgetXML attributeset methodget method EditTextandroid:textsetText(String)getText() Buttonandroid:onClick setOnClickListener(On ClickListener) - CheckBoxandroid:checkedsetChecked(Boolean)isChecked() SeekBarandroid:progresssetProgress(int)getProgress() SeekBarandroid:maxsetMax(int)getMax()

Selection Widgets - Spinner Similar to a drop-down list android:spinnerMode o "dialog" or "dropdown" android:prompt o e.g. "Select an option" o message to display when the spinner dialog is shown  only when android:spinnerMode="dialog" android:entries o e.g. o populate the spinner using this array

Selection Widgets - Spinner Similar to a drop-down list android:prompt o e.g. "Select an option" o message to display when the spinner dialog is shown android:spinnerMode o "dialog" or "dropdown" android:entries o e.g. o populate the spinner using this array If the list is static, you can again avoid Java by specifying the entries in XML!

Selection Widgets - Spinner Similar to a drop-down list android:prompt o e.g. "Select an option" o message to display when the spinner dialog is shown android:spinnerMode o "dialog" or "dropdown" android:entries o e.g. o populate the spinner using this array If you don't like XML, you can set the entries in Java also.

Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); }

Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } This is the Java code. If you choose to do this, then you don't need to set the android:entries attribute

Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } You should already know what this does.

Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } Since you don't like XML, to do it programmatically, you need to use an Adapter.

Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } This is Java stuff. If you don't know what it is, try removing it to see what happens.

Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } If you have resource already, then create an ArrayAdapter from that resource.

Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } First argument is of type Context, you can just past the "this" keyword here (because of the state of app)

Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } This is the resource that you're creating the ArrayAdapter from.

Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } 3rd argument expects of a TextView.

Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } android.R gives you resources that exist for use even before you create your app

Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } So instead of creating and using your own TextView for this purpose, use this one

Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } Attach the Adapter to the Spinner in order to populate the Spinner!

Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item); mySpinner.setAdapter(adapter); } Finally, this line of code is not required, but it makes your drop down look pretty.

Selection Widgets - Spinner Both ways give you a Spinner which uses predetermined entries How do you populate the Spinner at runtime, with when the entries are also determined at runtime?

Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); String[] countries = new String[]{"Brazil","China","Denmark"}; ArrayAdapter adapter = new ArrayAdapter (this, android.R.layout.simple_spinner_item, countries); mySpinner.setAdapter(adapter); }

Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); String[] countries = new String[]{"Brazil","China","Denmark"}; ArrayAdapter adapter = new ArrayAdapter (this, android.R.layout.simple_spinner_item, countries); mySpinner.setAdapter(adapter); } Create and initialize a String array

Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); String[] countries = new String[]{"Brazil","China","Denmark"}; ArrayAdapter adapter = new ArrayAdapter (this, android.R.layout.simple_spinner_item, countries); mySpinner.setAdapter(adapter); } Create an ArrayAdapter to populate the Spinner with

Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); String[] countries = new String[]{"Brazil","China","Denmark"}; ArrayAdapter adapter = new ArrayAdapter (this, android.R.layout.simple_spinner_item, countries); mySpinner.setAdapter(adapter); } 3rd argument accepts a List of Strings

Selection Widgets - ListView A ListView is similar to a Spinner they both show the user a list of items they both have the android:entries attribute, so they can both be set using the resource or the same ArrayAdapter

Selection Widgets - ListView  Adapters are used to provide the data to the ListView object.  The adapter also defines how each row is the ListView is displayed.  You can use pre-existing layout for each row.  You can also define customized layout for each row.  Extends BaseAdapter class.

Selection Widgets - ListView A ListView is similar to a Spinner with some exceptions, to name a few: A Spinner is normally used for forms, a ListView not so much A Spinner only shows one item at a time A Spinner item can be "selected", a ListView item can be "clicked" or "selected"

Selection Widgets - ListView A ListView is similar to a Spinner with some exceptions, to name a few: A Spinner is normally used for forms, a ListView not so much A Spinner only shows one item at a time A Spinner item can be "selected", a ListView item can be "clicked" or "selected" I added this in hindsight. On non-trouch-screen devices it can be selected

Events - Spinner OnItemSelected You can set an EventListener for when the user selects an item from the Spinner

Events - Spinner OnItemSelected mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() public void onItemSelected(AdapterView parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); public void onNothingSelected(AdapterView parent) { Toast.makeText(getApplicationContext(), "You didn't select anything", Toast.LENGTH_SHORT).show(); } });

Events - Spinner OnItemSelected mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() public void onItemSelected(AdapterView parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); public void onNothingSelected(AdapterView parent) { Toast.makeText(getApplicationContext(), "You didn't select anything", Toast.LENGTH_SHORT).show(); } }); Setter method for setting the EventListener

Events - Spinner OnItemSelected mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() public void onItemSelected(AdapterView parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); public void onNothingSelected(AdapterView parent) { Toast.makeText(getApplicationContext(), "You didn't select anything", Toast.LENGTH_SHORT).show(); } }); Anonymous class

Events - Spinner OnItemSelected mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() public void onItemSelected(AdapterView parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); public void onNothingSelected(AdapterView parent) { Toast.makeText(getApplicationContext(), "You didn't select anything", Toast.LENGTH_SHORT).show(); } }); When an item is selected...,

Events - Spinner OnItemSelected mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() public void onItemSelected(AdapterView parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); public void onNothingSelected(AdapterView parent) { Toast.makeText(getApplicationContext(), "You didn't select anything", Toast.LENGTH_SHORT).show(); } }); You can now reference the actual View (i.e., each entry in Spinner is a TextView)

Events - Spinner OnItemSelected mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() public void onItemSelected(AdapterView parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); public void onNothingSelected(AdapterView parent) { Toast.makeText(getApplicationContext(), "You didn't select anything", Toast.LENGTH_SHORT).show(); } }); The position of the view in the Adapter

Events - Spinner OnItemSelected mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() public void onItemSelected(AdapterView parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); public void onNothingSelected(AdapterView parent) { Toast.makeText(getApplicationContext(), "You didn't select anything", Toast.LENGTH_SHORT).show(); } }); The android:id attribute of the view

Events - Spinner OnItemSelected mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() public void onItemSelected(AdapterView parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); public void onNothingSelected(AdapterView parent) { Toast.makeText(getApplicationContext(), "You didn't select anything", Toast.LENGTH_SHORT).show(); } }); Cast the generic View to a TextView

Events - Spinner OnItemSelected mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() public void onItemSelected(AdapterView parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); public void onNothingSelected(AdapterView parent) { Toast.makeText(getApplicationContext(), "You didn't select anything", Toast.LENGTH_SHORT).show(); } }); Get the android:text value

Events - Spinner OnItemSelected Note that this we're using the OnItemSelectedListener instead of the OnItemClickListener for Spinner! If you try to use OnItemClickListener you may get errors Also, note the difference between these two listeners and OnClickListener o OnItemClickListener  an entry in the Spinner has been clicked, don't use this o OnClickListener  the Spinner has been clicked o OnItemSelectedListener  an entry in the Spinner has been selected

Events - ListView OnItemClick As mentioned before, a ListView item can be clicked, or selected In that case, you use OnItemClickListener for ListView or OnItemSelected Listener

Events - ListView OnItemClick myListView.setOnItemClickListener(new OnItemClickListener() public void onItemClick(AdapterView parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); } });

Android Application Components So far we've only been working with 1 of the 4 Android components, namely Activity

Android Application Components The 4 Android application components are 1.Activity 2.Broadcast Receiver 3.Content Provider 4.Service

Activity So far we've only been working with a single Activity Remember, an Activity is a single screen with a user interface Each Activity in an application is independent

Activity An Activity is the only Android application component with a UI!

Activity In Homework 2, you're creating a form that doesn't do anything after the form is submitted If you want to take the user to a different screen after the form is submitted, you should add a new Activity to your app.

Activity To add a new Activity open the AndroidManifest.xml file click on the Application tab at the bottom of the window under the Application Nodes section, click Add... select Activity in the Attributes for Activity section, click on the Name* linkName* Enter the name of your second Activity Finish

You can design the UI for your second Activity e.g. add another res/layout/xml_file.xml Now we need to figure out how to launch the second Activity B from the first Activity A Activity

Intent An Intent is an abstract description of an operation to be performed You can use it to launch another Activity B from within Activity A using an Intent Activity AActivity B Intent

Activity and Intent... public void onClick(View view) { Intent myIntent = new Intent(A.this, B.class); startActivity(intent); }...

Activity and Intent... public void onClick(View view) { Intent myIntent = new Intent(A.this, B.class); startActivity(intent); }... The name of the Activity the app is currently running

Activity and Intent... public void onClick(View view) { Intent myIntent = new Intent(A.this, B.class); startActivity(intent); }... The name of the Activity that should be launched

Activity and Intent... public void onClick(View view) { Intent myIntent = new Intent(A.this, B.class); startActivity(intent); }... Launch Activity B now

Activity - Android Manifest File If you added the second Activity B this way, then B will be automatically added to the Android Manifest file If you added it some other way, then Activity B may not be in the Manifest File. This is an easy to get a Force Close when you try to launch Activity B! Android needs to know what Activities your app may launch

Activity - ListActivity Sometimes the entire UI for your Activity may be a ListView In this case, you can extend ListActivity instead of extending Activity You don't need a layout.xml file for a ListActivity!

Activity - ListActivity public class MyListActivity extends ListActivity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String[] list = new String[]{"one","two","three"}; ArrayAdapter adapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, list); setListAdapter(adapter); }

Activity - ListActivity public class MyListActivity extends ListActivity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String[] list = new String[]{"one","two","three"}; ArrayAdapter adapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, list); setListAdapter(adapter); } extend ListActivity instead of Activity

Activity - ListActivity public class MyListActivity extends ListActivity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String[] list = new String[]{"one","two","three"}; ArrayAdapter adapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, list); setListAdapter(adapter); } We don't need to setContentView because we're extending ListActivity

Activity - ListActivity public class MyListActivity extends ListActivity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] list = new String[]{"one","two","three"}; ArrayAdapter adapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, list); setListAdapter(adapter); } We don't need to setContentView because we're extending ListActivity

Activity - ListActivity public class MyListActivity extends ListActivity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] list = new String[]{"Brazil","China","Denmark"}; ArrayAdapter adapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, list); setListAdapter(adapter); } We've seen this before in this lecture

Activity - ListActivity public class MyListActivity extends ListActivity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] list = new String[]{"Brazil","China","Denmark"}; ArrayAdapter adapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, list); setListAdapter(adapter); } We've also seen this before in this lecture

Activity - ListActivity public class MyListActivity extends ListActivity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] list = new String[]{"Brazil","China","Denmark"}; ArrayAdapter adapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, list); setListAdapter(adapter); } We call setListAdapter on this ListActivity instead of calling it on a ListView (we don't have a ListView specified in an XML file this time!)

Activity - ListActivity public class MyListActivity extends ListActivity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] list = new String[]{"Brazil","China","Denmark"}; ArrayAdapter adapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, list); setListAdapter(adapter); } This is equivalent to calling this.setListAdapter(adapter); i.e., using the this keyword to refer to this instance of MyListActivity

References The Busy Coder's Guide to Android Development - Mark Murphy The Busy Coder's Guide to Android Development - Mark Murphy Android Developers The Mobile Lab at Florida State University