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

Slides:



Advertisements
Similar presentations
Programming with Android: Data management
Advertisements

Programming with Android: Calculator Example
Programming with Android: Activities
Programming with Android: Network Operations
Programming with Android: Application Resources Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Android UserInterfaces Nasrullah Niazi. overView All user interface elements in an Android app are built using View and ViewGroup objects. A View is an.
CE881: Mobile and Social Application Programming Simon M. Lucas Layouts.
Android User Interface
Chapter 3: Engage! Android User Input, Variables, and Operations
Android Projects 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.
 User Interface - Raeha Sandalwala.  Introduction to UI  Layouts  UI Controls  Menus and ‘Toasts’  Notifications  Other interesting UIs ◦ ListView.
User Interface Classes.  Design Principles  Views & Layouts  Event Handling  Menus  Dialogs.
Filip Debelić What is it? Android is a mobile operating system (OS) based on the Linux kernel and currently developed by Google Android,
By: Jeremy Smith.  Introduction  Droid Draw  Add XML file  Layouts  LinearLayout  RelativeLayout  Objects  Notifications  Toast  Status Bar.
Layout and Control in UI The user interface (UI) is the graphical interface user can see and interact with your app comprising UI controls like textbox,
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)
CS378 - Mobile Computing User Interface Basics MIKE!! LOOK HERE FOR intercepting the ListView items:
Android: Layouts David Meredith
Android Application Development 2013 PClassic Chris Murphy 1.
Android UI, and Networking. Can do most networking on Android Bluetooth only on 2.0, Not supported with version 1.6.
Mobile Programming Lecture 1 Getting Started. Today's Agenda About the Eclipse IDE Hello, World! Project Android Project Structure Intro to Activities,
Introduction to Android Programming Content Basic environmental structure Building a simple app Debugging.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail.
Chapter 5: Investigate! Lists, Arrays, and Web Browsers.
Chapter 2: Simplify! The Android User Interface
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Tip Calculator App Building an Android App with Java © by Pearson Education, Inc. All Rights Reserved.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 5: Investigate! Android Lists, Arrays,
Chapter 5 Creating User Interfaces GOALS and OBJECTIVES Begin our application by creating our user interface. More than one way to create a user interface.
Understanding Hello Android 1 CS300. Activity  Similar to a form  Base class for the visual, interactive components of your application  Android API.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
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.
INTRODUCTION TO ANDROID. Slide 2 Application Components An Android application is made of up one or more of the following components Activities We will.
Engage! Android User Input, Variables,
Programming Mobile Applications with Android September, Albacete, Spain Jesus Martínez-Gómez.
Android Boot Camp for Developers Using Java, 3E
Programming with Android: Layouts, Widgets and Events Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
User Interfaces: Part 1 (View Groups and Layouts).
Presented By: Muhammad Tariq Software Engineer Android Training course.
WaveMaker Visual AJAX Studio 4.0 Training Basics: Building Your First Application Designer Basics.
User Interface Android Club Agenda Button OnClickListener OnLongClickListener ToggleButton Checkbox RatingBar AutoCompleteTextView.
Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside.
Video Games list lab 6  At the end of this lab you will be expected to know:  What Views, View Groups, Layouts, and Widgets are and how they relate to.
MOBILE COMPUTING D10K-7D02 MC05: Android UI Design Dr. Setiawan Hadi, M.Sc.CS. Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran.
© 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.
Designing user interfaces using: Simple views 1. Views Basic views – TextView – EditText – Button – ImageButton – CheckBox – ToggleButton – RadioButton.
CS378 - Mobile Computing User Interface Basics. User Interface Elements View – Control – ViewGroup Layout Widget (Compound Control) Many pre built Views.
Internet & World Wide Web How to Program, 5/e Copyright © Pearson, Inc All Rights Reserved.
Building User Interfaces Basic Applications
© 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.
COMP 365 Android Development.  Developing for Android  XML for user interface formatting and other scripting  Java for programming.
Events. Slide 2©SoftMoore Consulting Events Events are generated when a user interacts with the view objects of an application. Examples –button clicked–
Mobile Programming Lecture 4 Resources, Selection, Activities, Intents.
Http :// developer. android. com / guide / topics / fundamentals. html.
CS371m - Mobile Computing User Interface Basics. UI Programming with Widgets Widget is an element in a Graphical User Interface (GUI) – not to be confused.
ANDROID LAYOUTS AND WIDGETS. Slide 2 Introduction Parts of the Android screen Sizing widgets and fonts Layouts and their characteristics Buttons, checkboxes.
Android – Event Handling
Android Widgets 1 7 August 2018
Creation of an Android App By Keith Lynn
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
Android SDK & App Development
CS323 Android Getting Started
Building User Interfaces Basic Applications
Android Developer Fundamentals V2
User Interface Screen Elements
User Interface Screen Elements
Presentation transcript:

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

Luca Bedogni, Marco Di Felice - Programming with Android – Resources 2 Outline Event Management: Event Handlers Widget: CheckedTextView Widget: ImageView Widget: Button and CompoundButton Widget: TextView and EditText What is a Widget? Event Management: Event Listeners

Luca Bedogni, Marco Di Felice - Programming with Android – Resources 3 Android: Where are we now … Android Applications’ anatomy:  Activities  Application Components (screens)  Intents  Communication between components  Layouts  Placement of the elements on the screen …  Views  … Elements to be placed! Widget  Pre-defined, common-used View objects …

Luca Bedogni, Marco Di Felice - Programming with Android – Resources 4 Android: Views objects Views  basic building blocks for user interface components  Rectangular area of the screen  Responsible for drawing  Responsible for event handling  GoogleMap  WebView  Widgets  topic of the day  …  User-defined Views EXAMPLEs of VIEWS objects:

Luca Bedogni, Marco Di Felice - Programming with Android – Resources 5 Android: Views objects ViewGroup  Container of other views, base class for layouts LINEAR LAYOUT TABLE LAYOUT

Luca Bedogni, Marco Di Felice - Programming with Android – Resources 6 Android: Views objects Widget  Pre-defined interactive UI components (android.view.widgets) TextView EditText Button ToggleButton Spinner DataPicker

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni  Widgets can be created in the XML layout files Widgets: Java and XML code < TextView android:width=“100dp” android:height=“100dp” android:layout_width="match_parent" android:layout_height="wrap_content” android:visibility="visible” android:enabled="true” android:scrollbars="vertical” …. />

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni  Widgets can be created in Java  Widgets can be created in XML and accessed in Java Widgets: Java and XML code < TextView /> public TextView text; text=(TextView)findViewById(R.id.name1); JAVA XML CAST REQUIRED public TextView text; text=new TextView();

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni  Each Widget can generate events, that can be captured by Listeners that define the appropriate actions to be performed in response to each event. Widgets: Java and XML code ONCLICK event Java code that manages the onClick event … Java code that manages the onClick event …

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni  Each Widget can have a focus and a visibility, based on the user’s interaction.  The user can force a focus to a specific component through the requestFocus() method.  The user can modify the visibility of a specific component through the setVisibility(int) method. Widgets: Java and XML code public TextView text; text=(TextView) findViewById(R.id.name1); text.setVisibility(true) text.requestFocus();

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni  Widgets are organized on a hierarchy of classes … Widgets: Hierarchy of the classes … View TextView EditView ImageView Button CheckedTextView AutoCompleteTextView CompoundButton CheckBox RadioButton ToggleButton

Luca Bedogni, Marco Di Felice - Programming with Android – Resources 12  XML tags:  Could be filled with strings or HTML markups  Not directly editable by users  Usually used to display static informations Widgets: TextView <TextView android:layout_width="wrap_content" android:layout_height="wrap_content” />

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni  Methods to place some texts inside a TextView …  public void setText(CharSequence text)  public CharSequence getText()  public void setSingleLine(boolean singleLine)  public void setHorizontallyScrolling(boolean enable)  public void setLines(int lines)  public void setEllipsize(TextUtils.TruncateAt where)  public void setHints(CharSequence hints) Widgets: TextView methods  TextUtils.TruncateAt.END  TextUtils.TruncateAt.MARQUEE  TextUtils.TruncateAt.MIDDLE  TextUtils.TruncateAt.START

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni  Simple strings could be linkified automatically.  How? Pick a normal string, and use Linkify.addLinks() to define the kind of links to be created.  Could manage: Web addresses, s, phone numbers, Maps Widgets: Linkify elements TextView textView=(TextView) findViewById(R.id.output); Linkify.addLinks(textView, Linkify.WEB_URLS | Linkify.WEB_ADDRESSES | Linkify.PHONE_NUMBERS ); Linkify.addLinks(textView, Linkify.ALL);  It is possible to define custom Linkify objects...

Luca Bedogni, Marco Di Felice - Programming with Android – Resources 15  XML tags:  Similar to a TextView, but editable by the users  An appropriate keyboard will be displayed Widgets: EditText <EditText android:inputType= “textCapSentences” | “textCapWords” | “textAutoCorrect” | “textPassword” | “textMultiLane” | “textNoSuggestions” />

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni  XML tags:  Used to make easier the input by the users …  As soon as the user starts typing, hints are displayed  A list of hints is given through an Adapter Widgets: AutocompleteTextView String[] tips=getResources().getStringArray(R.array.nani_array); ArrayAdapter adapter=new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1lines, tips); AutoCompleteTextView acTextView=(AutoCompleteTextView) findViewById(R.id.inputText); acTextView.setAdapter(adapter);

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni Widgets: AutocompleteTextView

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni  CompoundButton: Button + state (checked/unchecked) Widgets: Button  XML tags:  Superclass of a TextView, but not directly editable by users  Can generate events related to click, long click, drag, etc <Button /> <item android:color="#ff819191" android:state_pressed="true”> res/color/blue.xml

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni Widgets: Button and CompoundButton checkBox CompoundButton XML tags: <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CheckBox" android:checked="true” />

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni Widgets: Button and CompoundButton checkBox CompoundButton  public boolean isChecked(): Returns true if the button is checked, false otherwise.  public boolean setChecked(bool) Listener: onCheckedChangeListener

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni Widgets: Button and CompoundButton radioButton CompoundButton XML tags: <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ButtonRadio" android:checked="true" />

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni Widgets: Button and CompoundButton radioButton CompoundButton  Define multiple (mutual- exclusive) options through a tag.  Only one button can be checked within the same RadioGroup. Listener: OnCheckedChangeListener

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni Widgets: Button and CompoundButton <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical”> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1" android:checked="true” /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2” />

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni Widgets: Button and CompoundButton toggleButton CompoundButton XML tags: <ToggleButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="Button ON" android:textOff="Button OFF" android:checked="false” />

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni Widgets: Button and CompoundButton toggleButton CompoundButton  It can assume only 2 states: checked/unchecked  Different labels for the states with: android:textOn and android:textOff XML attributes. Listener: OnCheckedChangeListener

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni Widgets: Spinners Spinner component XML tags: <Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" Option 1 Option 2 Option 3 Option 4 res/values.xml

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni Widgets: Spinners Spinner component XML tags:  Provides a quick way to select values from a specific set.  The spinner value-set can be defined in XML (through the entries tag) or through the SpinnerAdapter in Java Listener: OnItemSelectedListener

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni Widgets: Button and CompoundButton DataPicker component XML tags: <DatePicker android:layout_width="wrap_content" android:layout_height="wrap_content” android:endYear="1990" android:startYear="2014" android:maxDate="10/10/2014” />

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni Widgets: ImageView ImageView component XML tags: <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" Source: android.jpg in drawable/

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni  ImageView: subclass of View object.  Some methods to manipulate an image:  void setScaleType (enum scaleType)  void setAlpha (double alpha)  void setColorFilter (ColorFilter color) Widgets: ImageView CENTER, CENTER_CROP, CENTER_INSIDE, FIT_CENTER, FIT_END, FIT_START, FIT_XY, MATRIX

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni  Checkable version of a TextView  Usable with a ListView Adapter  Multiple or single selection of items (CHOICE_MODE_SINGLE, CHOICE_MODE_MULTIPLE)  Methods:  void setChoiceMode(int choiceMode)  long[] getCheckItemIds()  int getCheckedItemPosition() Widgets: CheckedTextView

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni Views/Widgets are interactive components …  … Upon certain action, an appropriate event will be fired  Events generated by the user’s interaction: click, long click, focus, items selected, items checked,drag, etc PROBLEM: How to handle these events? Views and Events 1. Directly from XML 2. Through Event Listeners (general, recommended) 3. Through Event Handlers (general)

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni  For a limited set of components, it is possible to manage the events through callbacks, directly indicated in the XML. Views and Events <Button android:onClick=”doSomething” /> public void doSomething(View w) { // Code to manage the click event } XML Layout File Java class

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni Views/Widgets are interactive components …  … Upon certain action, an appropriate event will be fired  Events generated by the user’s interaction: click, long click, focus, items selected, items checked,drag, etc PROBLEM: How to handle these events? Views and Events 1. Directly from XML 2. Through Event Listeners (general, recommended) 3. Through Event Handlers (general)

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni  Each View contains a collection of nested interfaces (listeners).  Each listener handles a single type of events…  Each listener contains a single callback method …  The callback is invoked in occurrence of the event. Views and Events Button interface OnClickListener public abstract void onClick(View v) {}

Luca Bedogni, Marco Di Felice - Programming with Android – Resources 36 Button btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() public void onClick(View view) { // Event management } }); To handle OnClick events through the ActionListener: 1. Implement the nested interface in the current Activity 2. Implement the callback method (onClick) 3.Associate the ActionListener to the Button through the View.setOnClickEventListener() method Views and Events: ActionListener public class ExampleActivity extends Activity implements OnClickListener { … Button button=(Button)findViewById(R.id.buttonNext); button.setOnClickListener(this); … public void onClick(View v) { } }

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni Button btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() public void onClick(View view) { // Event management } }); To handle OnClick events through the ActionListener: 1.Create an anonymous OnClickListener object 2.Implement the callback method (onClick) for the anonymous object 3.Associate the ActionListener to the Button through the View.setOnClickEventListener() method Views and Events: ActionListener Button btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() public void onClick(View view) { // Event management } });

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni Some ActionListeners:  interface OnClickListener abstract method: onClick()  interface OnLongClickListener abstract method: onLongClick()  interface OnFocusChangeListener abstract method: onFocusChange()  interface OnKeyListener abstract method: onKey() Views and Events: ActionListener

Luca Bedogni, Marco Di Felice - Programming with Android – Resources 39 Some ActionListeners:  interface OnCheckedChangeListener abstract method: onCheckedChanged()  interface OnItemSelectedListener abstract method: onItemSelected()  interface OnTouchListener abstract method: onTouch()  interface OnCreateContextMenuListener abstract method: onCreateContextMenu() Views and Events: ActionListener

Luca Bedogni, Marco Di Felice – Events and Widgets (c) Luca Bedogni  Possible to fire an event directly from the Java code (without user’s interaction) … useful for debugging purpose.  Tipically in the form performXXX()  The corresponding listener (if set) will be invoked… Views and Events: ActionListener … Button button=(Button)findViewById(R.id.buttonNext); button.performClick(); … // Callback method public void onClick(View v) { …. } // Callback method public void onClick(View v) { …. }

Luca Bedogni, Marco Di Felice – Events and Widgets (c) Luca Bedogni Views/Widgets are interactive components …  … Upon certain action, an appropriate event will be fired  Events generated by the user’s interaction: click, long click, focus, items selected, items checked,drag, etc PROBLEM: How to handle these events? Views and Events 1. Directly from XML 2. Through Event Listeners (general, recommended) 3. Through Event Handlers (general)

Luca Bedogni, Marco Di Felice - Programming with Android – Resources (c) Luca Bedogni Event Handlers  Some views have callback methods to handle specific events When a Button is touched  onTouchEvent() called Views and Events PROBLEM: to intercept an event, you must extend the View class and override the callback method … not very practical!  In practice: Events Handlers are used for custom (user- defined) components …  … Events Listeners are used for common View/Widget components …