Android Selection Widgets Yuliana Setiowati Rizky Yuniar Hakkun 1Politeknik Elektronika Negeri Surabaya.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to XHTML Programming the World Wide Web Fourth edition.
Advertisements

Android Application Development Tutorial. Topics Lecture 6 Overview Programming Tutorial 3: Sending/Receiving SMS Messages.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 Created by Cheryl M. Hughes The Web Wizards Guide to XML by Cheryl M. Hughes.
BASIC SKILLS AND TOOLS USING ACCESS
15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
Microsoft Access 2007 Advanced Level. © Cheltenham Courseware Pty. Ltd. Slide No 2 Forms Customisation.
Prepared by: Workforce Enterprise Services For: The Illinois Department of Commerce and Economic Opportunity Bureau of Workforce Development ENTRY OF EMPLOYER.
Local Customization Chapter 2. Local Customization 2-2 Objectives Customization Considerations Types of Data Elements Location for Locally Defined Data.
Process a Customer Chapter 2. Process a Customer 2-2 Objectives Understand what defines a Customer Learn how to check for an existing Customer Learn how.
Office 2003 Introductory Concepts and Techniques M i c r o s o f t Windows XP Project An Introduction to Microsoft Windows XP and Office 2003.
1.
1 School Administrators Guide Standards-based Report Card (SBRC) Special Access/Privileges for School Administrators Interim Reporting Interim Reporting.
PP Test Review Sections 6-1 to 6-6
1 IMDS Tutorial Integrated Microarray Database System.
Microsoft Access.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
Microsoft Office Word is an example of ____ software. a. Database b
Benchmark Series Microsoft Excel 2013 Level 2
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
 Copyright I/O International, 2013 Visit us at: A Feature Within from Item Class User Friendly Maintenance  Copyright.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
Android UserInterfaces Nasrullah Niazi. overView All user interface elements in an Android app are built using View and ViewGroup objects. A View is an.
1 How Do I Order From.decimal? Rev 05/04/09 This instructional training document may be updated at anytime. Please visit and check the.
2004 EBSCO Publishing Presentation on EBSCOadmin.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. a lesson approach Microsoft® PowerPoint 2010 © 2011 The McGraw-Hill Companies,
Essential Cell Biology
PSSA Preparation.
Chapter 11 Creating Framed Layouts Principles of Web Design, 4 th Edition.
Chapter 13 Web Page Design Studio
RefWorks: The Basics October 12, What is RefWorks? A personal bibliographic software manager –Manages citations –Creates bibliogaphies Accessible.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces.
South Dakota Library Network MetaLib User Interface South Dakota Library Network 1200 University, Unit 9672 Spearfish, SD © South Dakota.
Registry and Referral System HCW/PSW Staff User Manual
Android User Interface
Chapter 16 Graphical User Interfaces John Keyser’s Modifications of Slides by Bjarne Stroustrup
Android Using Menus Yuliana Setiowati Rizky Yuniar Hakkun Ahmad Syauqi Ahsan 1Politeknik Elektronika Negeri Surabaya.
Programming with Android: Widgets and Events Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml.
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: Layouts David Meredith
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,
Frank Xu Gannon University.  Linear Layout  Relative Layout  Table Layout.
Chapter 2 The Android User Interface. Objectives  In this chapter, you learn to:  Develop a user interface using the TextView, ImageView, and Button.
ListView.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 7: Reveal! Displaying Pictures in a GridView.
User Interfaces: Part 1 (View Groups and Layouts).
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.
© 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.
ANDROID – DRAWING IMAGES – SIMPLE EXAMPLE IN INTERFACE AND EVENT HANDLING L. Grewe.
DKU-MUST Mobile ICT Education Center 11. AdapterView.
ListView and ExpandableListView
Adapters 1 CS440. Adapters (again!)  Adapters are bridging classes, that bind data to user-interface Views. The adapter is responsible for creating the.
CS378 - Mobile Computing User Interface Basics. User Interface Elements View – Control – ViewGroup Layout Widget (Compound Control) Many pre built Views.
Building User Interfaces Basic Applications
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.
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.
Further android gui programming
Mobile Application Development BSCS-7 Lecture # 8
Android Development: Advanced Widgets using Adapters
Politeknik Elektronika Negeri Surabaya
Building User Interfaces Basic Applications
Presentation transcript:

Android Selection Widgets Yuliana Setiowati Rizky Yuniar Hakkun 1Politeknik Elektronika Negeri Surabaya

Outline ListView Spinner AutoCompleteTextView Gallery View GridView 2Politeknik Elektronika Negeri Surabaya

Selection Widgets RadioButtons and CheckButtons are suitable for selecting from a smallset of options. When the pool of choices is larger other widgets are more appropriate, those include classic UI controls such as:listboxes, comboboxes, drop-down lists, picture galleries, etc. Android offers a framework of data adapters that provide a common interface to selection lists ranging from static arrays to database contents. Selection views –widgets for presenting lists of choices –are handed an adapter to supply the actual choices. 3Politeknik Elektronika Negeri Surabaya

Selection Widgets Displaying/Selecting Options Destination layout Holding a ListView 4Politeknik Elektronika Negeri Surabaya

Using ArrayAdapter The easiest adapter to use is ArrayAdapter The ArrayAdapter constructor takes three parameters: 1.The Context to use (typically this will be your activity instance) 2.The resource ID of a view to use (such as the built-in system resource android.R.layout.simple_list_item_1as shown above) 3.The actual (source) array or list of items to show String[] items={"this", "is", "a","really", "silly", "list"}; new ArrayAdapter (this,android.R.layout.simple_list_item_1,items); 5Politeknik Elektronika Negeri Surabaya

Selection Widgets List Views The ListView view displays a list of items in a vertically scrolling list 6Politeknik Elektronika Negeri Surabaya

Selection Widgets 1.In the XML layout we use a ListView widget called android:id/list (built-in definition using multiple lines, black background, light-gray separator line, horiz. scroll- bar) 2. Later in the setting of the ArrayAdapter we make a reference to android.R.layout.simple_list_item_1(details representation of a single entry in the list) 7Politeknik Elektronika Negeri Surabaya

Selection Widgets Example 1 Example 1: A simple list When an item is selected, a message will be displayed. 8Politeknik Elektronika Negeri Surabaya

Selection Widgets Example 1 Example 1: A simple list listview.xml <LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:layout_width="fill_parent" android:layout_height="fill_parent" /> 9Politeknik Elektronika Negeri Surabaya

Selection Widgets Example 1 Example 1: A simple package net.learn2develop.AndroidViews; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import android.app.ListActivity; public class ListViewExample extends ListActivity { String[] presidents = { "Dwight D. Eisenhower“, "John F. Kennedy", "Lyndon B. Johnson", "Richard Nixon", "Gerald Ford“, "Jimmy Carter", "Ronald Reagan", "George H. W. Bush", "Bill Clinton", "George W. Bush", "Barack Obama" public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listview); setListAdapter(new ArrayAdapter (this, android.R.layout.simple_list_item_1, presidents)); } public void onListItemClick( ListView parent, View v, int position, long id) { Toast.makeText(this, "You have selected " + presidents[position], Toast.LENGTH_SHORT).show(); } 10Politeknik Elektronika Negeri Surabaya

Selection Widgets Example 1 Example 1: A simple list Modify the AndroidManifest.xml file to register the new activity <manifest xmlns:android=" package="net.learn2develop.AndroidViews" android:versionCode="1" android:versionName="1.0.0"> <activity android:name=".ViewsActivity" <activity android:name=".ListViewExample" /> 11Politeknik Elektronika Negeri Surabaya

Selection Widgets Example 1 Example 1: A simple list Modify the ViewsActivity.java file as follows to start the ListViewExample activity: package net.learn2develop.AndroidViews; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class ViewsActivity extends Activity { /** Called when the activity is first created. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startActivity(new Intent( this, ListViewExample.class)); } 12Politeknik Elektronika Negeri Surabaya

Selection Widgets Spinner In Android, the Spinner is the equivalent of the drop- down selector. The Spinner view displays an item at a time from a list and lets the users choose among them. Spinners have the same functionality of a ListView but take less space. As with ListView, you provide the adapter for linking data to child views using setAdapter() Add a listener object to capture selections made from the list with setOnItemSelectedListener(). 13Politeknik Elektronika Negeri Surabaya

Selection Widgets Example 2. Using the Spinner When an item in the Spinner view is selected, you will use the Toast class to display the item selected. 14Politeknik Elektronika Negeri Surabaya

Selection Widgets Example 2 Example 2. Using the Spinner Add a new file to the res/layout folder and name it as spinner.xml <LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawSelectorOnTop="true" /> 15Politeknik Elektronika Negeri Surabaya

Selection Widgets Example 2 Example 2. Using the Spinner package net.learn2develop.AndroidViews; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; import android.widget.AdapterView.OnItemSelectedListener; public class SpinnerExample extends Activity { String[] presidents = { "Dwight D. Eisenhower", "John F. Kennedy", "Lyndon B. Johnson", "Richard Nixon", "Gerald Ford", "Jimmy Carter", "Ronald Reagan", "George H. W. Bush", "Bill Clinton", "George W. Bush", "Barack Obama" } ; 16Politeknik Elektronika Negeri Surabaya

Selection Widgets Example 2 Example 2. Using the Spinner Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.spinner); s1 = (Spinner) findViewById(R.id.spinner1); ArrayAdapter adapter = new ArrayAdapter (this, android.R.layout.simple_spinner_item, presidents); s1.setAdapter(adapter); s1.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) { int index = s1.getSelectedItemPosition(); Toast.makeText(getBaseContext(), "You have selected item : " + presidents[index], Toast.LENGTH_SHORT).show(); } public void onNothingSelected(AdapterView arg0) {} }); } The above program creates an ArrayAdapter object and associates it with the Spinner view. When an item in the Spinner view is selected, you will use the Toast class to display the item selected 17Politeknik Elektronika Negeri Surabaya

Selection Widgets Example 2 Modify the AndroidManifest.xml <manifest xmlns:android=" package="net.learn2develop.AndroidViews" android:versionCode="1" android:versionName="1.0.0"> <activity android:name=".ViewsActivity" <activity android:name=".SpinnerExample" /> 18Politeknik Elektronika Negeri Surabaya

Selection Widgets Modify the ViewsActivity.java file to start the SpinnerExample activity package net.learn2develop.AndroidViews; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class ViewsActivity extends Activity { /** Called when the activity is first created. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); startActivity(new Intent(this, SpinnerExample.class)); } 19Politeknik Elektronika Negeri Surabaya

Selection Widgets Display item in Spinner using radio buttons ArrayAdapter adapter = new ArrayAdapter (this, android.R.layout.simple_spinner_dropdown_item, presidents); 20Politeknik Elektronika Negeri Surabaya

Selection Widgets AutoCompleteTextView With auto-completion, as the user types, the text is treated as a prefix filter, comparing the entered text as a prefix against a list of candidates. Matches are shown in a selection list that, like with Spinner, folds down from the field. The user can either type out a new entry (e.g., something not in the list) or choose an entry from the list to be the value of the field. AutoCompleteTextView subclasses EditText, so you can configure all the standard look-and-feel aspects, such as font face and color. AutoCompleteTextView has a android:completionThreshold property, to indicate the minimum number of characters a user must enter before the list filtering begins.

Selection Widgets AutoCompleteTextView Select this

Selection Widgets Example 3. AutoCompleteTextView

Selection Widgets Example 3. AutoCompleteTextView

Selection Widgets Example 3. AutoCompleteTextView

Selection Widgets Gallery Views The Gallery widget provides a set of options depicted as images. Image choices are offered on a contiguous horizontal mode, you may scroll across the image-set. The programmer must supply an ImageAdapter to indicate what to do when an individual image is selected/clicked. 26Politeknik Elektronika Negeri Surabaya

Selection Widgets Gallery Views Example 4 : The Gallery is a view that shows items (such as images) in a center-locked, horizontal scrolling list. 27Politeknik Elektronika Negeri Surabaya

Selection Widgets Gallery Views To see how the Gallery view works, add a new file to the res/layout folder and name it as displayview.xml <LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Images of San Francisco" /> <Gallery android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:layout_width="320px" android:layout_height="250px" android:scaleType="fitXY" /> 28Politeknik Elektronika Negeri Surabaya

Selection Widgets Gallery Views Add a new file to the res/values folder and name it as attrs.xml 29Politeknik Elektronika Negeri Surabaya

Selection Widgets Gallery Views Add a few images to the res/drawable folder 30Politeknik Elektronika Negeri Surabaya

Selection Widgets Gallery Views Add a new class to the src/net.learn2develop.AndroidViews folder and name it as DisplayViewsExample.java package net.learn2develop.AndroidViews; import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class DisplayViewsExample extends Activity { //---the images to display--- Integer[] imageIDs = { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7 }; 31Politeknik Elektronika Negeri Surabaya

Selection Widgets Gallery public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.displayview); Gallery gallery = (Gallery) findViewById(R.id.gallery1); gallery.setAdapter(new ImageAdapter(this)); gallery.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { //Toast.makeText(getBaseContext(), "pic" + (position + 1) + " selected", //Toast.LENGTH_SHORT).show(); //---display the images selected--- ImageView imageView = (ImageView) findViewById(R.id.image1); imageView.setImageResource(imageIDs[position]); } }); } 32Politeknik Elektronika Negeri Surabaya

Selection Widgets Gallery Views public class ImageAdapter extends BaseAdapter { private Context context; private int itemBackground; public ImageAdapter(Context c) { context = c; //---setting the style--- TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); itemBackground = a.getResourceId( R.styleable.Gallery1_android_galleryItemBackground, 0); a.recycle(); } //---returns the number of images--- public int getCount() { return imageIDs.length; } 33Politeknik Elektronika Negeri Surabaya

Selection Widgets Gallery Views //---returns the ID of an item--- public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } //---returns an ImageView view--- public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(context); imageView.setImageResource(imageIDs[position]); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setLayoutParams(new Gallery.LayoutParams(150, 120)); imageView.setBackgroundResource(itemBackground); return imageView; } You create the ImageAdapter class (which extends the BaseAdapter class) so that it can bind to the Gallery view with a series of ImageView views. The ImageView view is used to display images. When an image in the Gallery view is selected (or clicked), the position of the image selected is displayed. 34Politeknik Elektronika Negeri Surabaya

Selection Widgets Gallery Views Modify the AndroidManifest.xml file to register the new activity <manifest xmlns:android=" package="net.learn2develop.AndroidViews" android:versionCode="1" android:versionName="1.0.0"> <activity android:name=".ViewsActivity" <activity android:name=".DisplayViewsExample" /> 35Politeknik Elektronika Negeri Surabaya

Selection Widgets Gallery Views Modify the ViewsActivity.java file as follows to start the DisplayViewsExample activity public class ViewsActivity extends Activity { /** Called when the activity is first created. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startActivity(new Intent(this, DisplayViewsExample.class)); } 36Politeknik Elektronika Negeri Surabaya

Selection Widgets GridView The GridView view shows items in two- dimensional scrolling grid. You can use the GridView view together with ImageView views to display a series of images. 37Politeknik Elektronika Negeri Surabaya

Selection Widgets GridView GridViewSome properties used to determine the number of columns and their sizes: android:numColumns spells out how many columns there are, or, if you supply a value of auto_fit, Android will compute the number of columns based on available space and the properties listed below. android:vertical Spacing and its counterpart android:horizontalSpacingindicate how much whitespace there should be between items in the grid. android:columnWidth indicates how many pixels wide each column should be. android:stretchMode indicates, for grids with auto_fit for android:numColumns, what should happen for any available space not taken up by columns or spacing. Politeknik Elektronika Negeri Surabaya38

Selection Widgets GridView Example: Fitting the View Suppose the screen is 320pixels wide, and we have android:columnWidth set to 100px and android:horizontalSpacing set to 5px. Three columns would use 310pixels (three columns of 100 pixels and two whitespaces of 5 pixels). With android:stretchMode set to columnWidth, the three columns will each expand by 3-4 pixels to use up the remaining 10 pixels. With android:stretchMode set to spacingWidth, the two internal whitespaces will each grow by 5 pixels to consume the remaining 10 pixels. Politeknik Elektronika Negeri Surabaya39

Selection Widgets GridView Example 5 40Politeknik Elektronika Negeri Surabaya

Selection Widgets GridView Modify the displayview.xml file <GridView xmlns:android=" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="auto_fit" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:columnWidth="90dp" android:stretchMode="columnWidth" android:gravity="center" /> 41Politeknik Elektronika Negeri Surabaya

Selection Widgets GridView package net.learn2develop.AndroidViews; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class DisplayViewsExample extends Activity { //---the images to display--- Integer[] imageIDs = { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7 }; 42Politeknik Elektronika Negeri Surabaya

Selection Widgets public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.displayview); GridView gridView = (GridView) findViewById(R.id.gridview); gridView.setAdapter(new ImageAdapter(this)); gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(getBaseContext(), "pic" + (position + 1) + " selected", Toast.LENGTH_SHORT).show(); } }); } 43Politeknik Elektronika Negeri Surabaya You create the ImageAdapter class (which extends the BaseAdapter class) so that it can bind to the Gallery view with a series of ImageView views. The ImageView view is used to display images. When an image in the Gallery view is selected (or clicked), the position of the image selected is displayed.

Selection Widgets GridView public class ImageAdapter extends BaseAdapter { private Context context; public ImageAdapter(Context c) { context = c; } //---returns the number of images--- public int getCount() { return imageIDs.length; } //---returns the ID of an item--- public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } 44Politeknik Elektronika Negeri Surabaya

Selection Widgets GridView //---returns an ImageView view--- public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(context); imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(5, 5, 5, 5); } else { imageView = (ImageView) convertView; } imageView.setImageResource(imageIDs[position]); return imageView; } 45Politeknik Elektronika Negeri Surabaya