Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.

Slides:



Advertisements
Similar presentations
CE881: Mobile and Social Application Programming Simon M. Lucas Menus and Dialogs.
Advertisements

Programming with Android: Widgets and Events Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
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.
@2011 Mihail L. Sichitiu1 Android Introduction Hello Views Part 1.
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 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.
 Understanding an activity  Starting an activity  Passing information between activities  Understanding intents  Understanding the activity lifecycle.
ANDROID UI – FRAGMENTS. Fragment  An activity is a container for views  When you have a larger screen device than a phone –like a tablet it can look.
Chapter 5: Investigate! Lists, Arrays, and Web Browsers.
Chapter 9: Customize! Navigating with Tabs on a Tablet App.
Chapter 2: Simplify! The Android User Interface
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 5: Investigate! Android Lists, Arrays,
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Mobile Programming Lecture 6
Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type.
Frank Xu Gannon University.  Linear Layout  Relative Layout  Table Layout.
Mobile Computing Lecture#11 Adapters and Dialogs.
1/29/ Android Programming: FrameLayout By Dr. Ramji M. Makwana Professor and Head, Computer Engineering Department A.D. Patel.
Chapter 2 The Android User Interface. Objectives  In this chapter, you learn to:  Develop a user interface using the TextView, ImageView, and Button.
ListView.
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.
Chapter 7: Reveal! Displaying Pictures in a Gallery.
Android – Fragments L. Grewe.
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 5/4730 Android Communications Intents, callbacks, and setters.
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.
Copyright© Jeffrey Jongko, Ateneo de Manila University Custom ListAdapters.
DKU-MUST Mobile ICT Education Center 11. AdapterView.
MOBILE COMPUTING D10K-7D02 MC05: Android UI Design Dr. Setiawan Hadi, M.Sc.CS. Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran.
Copyright© Jeffrey Jongko, Ateneo de Manila University Deconstructing HelloWorld.
ListView and ExpandableListView
Android Application Lifecycle and Menus
User Interface Layout Interaction. EventsEvent Handlers/Listeners Interacting with a user.
Tutorial and Demos on Android CSC 4320/ Operating Systems Spring 2016.
Mobile Programming Lecture 4 Resources, Selection, Activities, Intents.
Http :// developer. android. com / guide / topics / fundamentals. html.
ANDROID LAYOUTS AND WIDGETS. Slide 2 Introduction Parts of the Android screen Sizing widgets and fonts Layouts and their characteristics Buttons, checkboxes.
Cosc 5/4730 Support design library. Support Design library Adds (API 9+) back support to a number of 5.0 lollipop widgets and material design pieces –
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.
Lab7 – Appendix.
Introduction to android
CS499 – Mobile Application Development
UNIT 11 로봇 전화번호부 3/4 로봇 SW 콘텐츠 교육원 조용수.
several communicating screens
CS240: Advanced Programming Concepts
GUI Programming Fundamentals
Further android gui programming
Android – Event Handling
Android Widgets 1 7 August 2018
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 UI – FRAGMENTS UNIT II.
Android Programming Lecture 6
CIS 470 Mobile App Development
ANDROID LISTS.
Android Lists and Fragments
Android Topics Custom ArrayAdapters
BMI Android Application will take weight and height from the users to calculate Body Mass Index (BMI) with the information, whether user is underweight,
CIS 470 Mobile App Development
ListView ? BaseAdapter ?.
Activities and Fragments
Lasalle-App Tecnología Móvil.
CIS 694/EEC 693 Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

cosc 4730 Android TabActivity and ListView

TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity creates each tab and calls each activity (via an intent). An activity is called, when it’s tab is clicked. – Below is a Tab Activity, which then calls 3 activities Activity Tab1 Activity Tab2 Activity Tab3

Tab Activity Code. Resources res = getResources(); // Resource object to get Drawables TabHost tabHost = getTabHost(); // The activity TabHost TabHost.TabSpec spec; // Resusable TabSpec for each tab Intent intent; // Reusable Intent for each tab // Create an Intent to launch an Activity for the tab (to be reused) intent = new Intent().setClass(this, tab1.class); // Initialize a TabSpec for each tab and add it to the TabHost spec = tabHost.newTabSpec("artists").setIndicator("Tab1") //,res.getDrawable(R.drawable.ic_tab1)) //this is optional, if you want a picture..setContent(intent); tabHost.addTab(spec); //place the “tab” in the tabHost. //add more tabs tabHost.setCurrentTab(0); //set first tab to display. //You can also create “global” variables here as well. NOTE, you need to have each activity in the androidManifest.xml file as well.

Activity as the tab. The activity is unchanged, it needs it own layout and works just like normal. – But as a note, when the tab is not selected the activity is paused (onPause() is called) – When it active, the onResume will be called. For global variables, you can get the “parent class” and use them as an object. TabHostDemo parentclass = null; //declared at top //called in onCreate parentclass = (TabHostDemo) tab1.this.getParent();

Passing information between tabs. You can use global variables Or you can put information in the intent (in the onPause() Method). The tab to be displayed can then retrieve information from the intent in onResume() A very simple example is shown in the TabHostDemo.zip

Listview (and spinner) The spinner shown before is a very similar to a list view A listView can be the only widget on the screen and can get very complex with the adapter. – The items in the list are normally clickable. Simple listView

Listview continued A listView can just be another widget in the layout as well. Or a very complex, which multiple widgets in each item in the list – Also true for the spinner

Simple listView public class Simple extends ListActivity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // NOTE, there is no xml layout for this activity! String[] values = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" }; ArrayAdapter adapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, values); setListAdapter(adapter); } //This responses to the click protected void onListItemClick(ListView l, View v, int position, long id) { String item = (String) getListAdapter().getItem(position); Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show(); } The layout is how the items will be displayed

Changing the layout ArrayAdapter adapter = new ArrayAdapter (this, R.layout.rowlayout, R.id.label, values); setListAdapter(adapter); Uses a layout we created and the label is where the item is go. In this case with an picture.

rowlayout.xml Using our custom layout, the listView displays an picture (the same picture for all items) And we need a textview to display the “item” for each one. Which is called label in this case (you can choose whatever name). <ImageView android:layout_width="22dp" android:layout_height="22dp" android:layout_marginLeft="4dp" android:layout_marginRight="10dp" android:layout_marginTop="4dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" >

More complex ListViews. If you want to display more then one piece of information per item, then you can have not only change the layout, but extend the ArrayAdapter or BaseAdapter.

ArrayAdapter The ArrayAdapter already extends the BaseAdapter and provides a lot of built in methods. In the ListActivity (or Activity) you would do something like this: ArrayAdapter adapter = new InteractiveArrayAdapter(this, myModelList); setListAdapter(adapter); Where myModleList is a list – Where model is a class you created.

ArrayAdapter (2) public class InteractiveArrayAdapter extends ArrayAdapter { public test1(Context context, List list) { super(context,R.layout.rowbuttonlayout, list); // TODO Auto-generated constructor stub //store objects so you can access it below. this.context = context; this.list = list; public View getView(int position, View convertView, ViewGroup parent) { //this will you create the View, which is each list item. //This will look similar to an OnCreate. }

getView So for this one, we have TextView and checkBox. The List tells us if it’s checked or not. In getView, we create the View LayoutInflater inflator = context.getLayoutInflater(); convertView = inflator.inflate(R.layout.rowbuttonlayout, null); text = (TextView) convertView.findViewById(R.id.label); checkbox = (CheckBox) convertView.findViewById(R.id.check); //Tag is an like a temp space, in a widget where you can set some information as an Object Class in this case, the position variable, used when we change the check mark. checkbox.setTag(String.valueOf(position)); checkbox.setChecked(list.get(position).isSelected()); text.setText(list.get(position).getName()); return convertView;

getView (2) The checkbox listener. checkbox.setOnCheckedChangeListener(new public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { CheckBox cb = (CheckBox)buttonView; //first get the model item out of the list, using the position stored in Tag. Model temp = list.get( Integer.parseInt((String)cb.getTag())); //now update our Model with the correct information. temp.setSelected(cb.isChecked()); cb.setChecked(temp.isSelected()); //Not necessary since the GUI handles it. say we only want one "item" checked and all the other unchecked. String t = (String) cb.getTag(); //use the tag temp space to get back our current position in the list. int position = Integer.parseInt(t); for (int i=0; i<list.size();i++) { if (i!= position) list.get(i).setSelected(false); } notifyDataSetChanged(); //"redraw" any views that were checked. } });

Custom ListViews We want to very complex and provide our own interface, then normally we extend the baseAdapter to create “mini activities” for each item in the ListView. In this case a Phone class is created to hold all the Information, which passed to an extended baseAdapter.

BaseAdapter In a BaseAdapter you must implement the following methods: public int getCount() – How many items are in the data set represented by this Adapter. public Object getItem(int position) – Get the data item associated with the specified position in the data set. public long getItemId(int position) – Get the row id associated with the specified position in the list. public View getView(int position, View convertView, ViewGroup parent) – Like before, the view. You will likely create a constructor, just like before, except you don’t call super, because they isn’t a super constructor. – Get the list and context.

PhoneBookAdapter public class PhonebookAdapter extends BaseAdapter implements OnClickListener { private Context context; private List listPhonebook; public PhonebookAdapter(Context context, List listPhonebook) { this.context = context; this.listPhonebook = listPhonebook; public int getCount() { return listPhonebook.size(); public Object getItem(int position) { return listPhonebook.get(position); public long getItemId(int position) { return position; } getView will be complex like before, with the inflater and then setting up all the widgets In the layout with information. See the source code. ListDemo.zip

References See the TabHostDemo.zip and ListDemo.zip on the handout pages for source code. There are a lot more to ListView Also, you can do the same thing to a spinner as well. This allows you to create very complex “drop-down menu” to use in your app.

Q A &