Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Programming with Android: Layouts, Widgets and Events Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna."— Presentation transcript:

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

2 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 2 Outline Event Management: Event Listeners Widget: examples Widget: definition ViewGroup: examples ViewGroup: definition Components of an Activity Event Management: Event Handlers

3 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 3 Android Applications Design  Developing an Android Application means using in a proper way the Android basic components … Activity Layout Views Fragment Intent Service Broadcast Receiver Broadcast Receiver Content Providers Content Providers

4 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 4 Android: Views and Layouts Components of the User Interface (UI) of an Activity ViewGroup View  View container  Responsible for placing other Views on the display  Every layout must extend a ViewGroup  Basic component of the UI  Can handle/produce events  Every new UI component must extend a View

5 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 5 Android: ViewGroups ViewGroup  define the placement of the Views.  Defined in Java code (within the Activity file)  Defined in XML (layout file)  Use XML tags to place a ViewGroup  Place a View within a ViewGroup using these XML attributes android:layout_width android:layout_height } match_parent|wrap_content

6 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 6 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation=”vertical" > <Button android:id="@+id/button1" android:layout_width=” match_parent " android:layout_height="wrap_content" android:text="@string/buttonString1" /> <Button android:id="@+id/button2" android:layout_width=" wrap_content " android:layout_height="wrap_content" android:text="@string/buttonString2" /> ACTIVITY_MAIN.XML Android: ViewGroups

7 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 7 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation=”vertical" > <Button android:id="@+id/button1" android:layout_width=” match_parent " android:layout_height="wrap_content" android:text="@string/buttonString1" /> <Button android:id="@+id/button2" android:layout_width=" wrap_content " android:layout_height=” match_parent " android:text="@string/buttonString2" /> ACTIVITY_MAIN.XML Android: ViewGroups

8 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 8  A Layout can be declared within another Layout List of most common Layouts provided by Android NameXML TagDescription LinearLayout arrange Views by aligning them on a single row or column RelativeLayout arrange Views through relative positions TableLayout arrange Views into rows and columns FrameLayout arrange a single View within a Layout AbsoluteLayout arrange Views through absolute positions Android: ViewGroups

9 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 9  A Layout can be declared within another Layout List of most common Layouts provided by Android NameXML TagDescription LinearLayout arrange Views by aligning them on a single row or column RelativeLayout arrange Views through relative positions TableLayout arrange Views into rows and columns FrameLayout arrange a single View within a Layout AbsoluteLayout arrange Views through absolute positions Android: ViewGroups

10 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 10 LinearLayout  view group that aligns all children in a single direction, vertically or horizontally.  Orientation can be declared through XML tag android:orientation= HORIZONTAL|VERTICAL  Orientation can also be declared in Java through setOrientation(int orientation)  Views has also other two attributes: gravity weigth How much space is assigned to the View Align the View with its parent Android: ViewGroups

11 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 11 ORIENTATION VERTICAL ORIENTATION VERTICAL ORIENTATION HORIZONTAL ORIENTATION HORIZONTAL Android: ViewGroups

12 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 12 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- Also horizontal  <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/buttonString1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/buttonString2" /> Android: ViewGroups

13 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 13  For each View in a LinearLayout, we can set the android:weight XML property (integer value) Importance of a View, how much it can expand <Button …android:layout_width=match_parent android:weight=1/> <Button … android:layout_width=match_parent android:weight=1/> Android: ViewGroups

14 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 14  For each View in a LinearLayout, we can set the android:weight XML property (integer value) Importance of a View, how much it can expand <Button …android:layout_width=match_parent android:weight=1/> <Button … android:layout_width=match_parent android:weight=2/> Android: ViewGroups

15 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 15  For each View in a LinearLayout, we can set the layout_gravity XML property Align a View with its parent (LinearLayout) <Button … android:layout_width=match_parent android:weigth=1/> <Button … android:layout_width=match_parent android:layout_gravity=“center_vertical” android:weight=2/> Android: ViewGroups

16 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 16  A Layout can be declared within another Layout List of most common Layouts provided by Android NameXML TagDescription LinearLayout arrange Views by aligning them on a single row or column RelativeLayout arrange Views through relative positions TableLayout arrange Views into rows and columns FrameLayout arrange a single View within a Layout AbsoluteLayout arrange Views through absolute positions Android: ViewGroups

17 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 17 RelativeLayout  View group that displays all Views based on relative positions. RELATIVE= COMPARED to the PARENT LAYOUT android:alignParentBottom=“true|false” android:alignParentTop=“true|false” android:alignParentLeft=“true|false” android:alignParentRight=“true|false” android:alignParentStart=“true|false” android:alignParentEnd=“true|false” … Android: ViewGroups

18 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 18 RelativeLayout  View group that displays all Views based on relative positions. RELATIVE= COMPARED to SIBLING VIEWs android:toLeftOf= ID android:toRightOf= ID android:toStartOf= ID android:toEndOf= ID … XML Identifier of the View used as reference point XML Identifier of the View used as reference point Android: ViewGroups

19 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 19 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <EditText android:id="@+id/username" android:text="username" android:inputType="text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_toRightOf="@+id/usernameLabel" > <TextView android:id="@+id/usernameLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/username" android:text="Username" /> CONTINUE ACTIVITY_MAIN.XML Android: ViewGroups

20 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 20 <EditText android:id="@+id/password" android:text="password" android:inputType="textPassword" android:layout_below="@+id/username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/username" android:layout_alignParentRight="true" android:layout_toRightOf="@+id/usernameLabel" > <TextView android:id="@+id/passwordLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/password" android:text="Password" /> ACTIVITY_MAIN.XML Android: ViewGroups

21 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 21  A Layout can be declared within another Layout List of most common Layouts provided by Android NameXML TagDescription LinearLayout arrange Views by aligning them on a single row or column RelativeLayout arrange Views through relative positions TableLayout arrange Views into rows and columns FrameLayout arrange a single View within a Layout AbsoluteLayout arrange Views through absolute positions Android: ViewGroups

22 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 22 TableLayout  View group that arranges all Views into rows and columns (as an HTML table). 1.Consists of a list of TableRaw objects, each defyining a raw of the Table. 2.The width of a column is defined by the row with the widest cell in that column. 3.Cells can be empty, or can span multiple columns (like in HTML). 4.Border lines of the cells are not displayed. PROPERTIES Android: ViewGroups

23 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 23 TableLayout  View group that arranges all Views into rows and columns (as an HTML table). android:layout_column android:layout_span android:collapsecolumn android:shrinkColumns android:stretchColumns ADDITIONAL XML attributes } Apply only to width layout_width is always WRAP_CONTENT layout_height is WRAP_CONTENT (default) Android: ViewGroups

24 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 24 <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android=schemas.android.com/apk/res/android android:id="@+id/tableLayout”> <Button android:id="@+id/button1“ android:layout_width="wrap_content“ android:layout_height="wrap_content“ android:text="Button" /> <Button android:id="@+id/button2“ android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button" /> ACTIVITY_MAIN.XML CONTINUE Android: ViewGroups

25 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/secondRow"> <Button android:layout_column="1" android:layout_span="2" android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button"> 25 ACTIVITY_MAIN.XML Android: ViewGroups

26 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 26  A Layout can be declared within another Layout List of most common Layouts provided by Android NameXML TagDescription LinearLayout arrange Views by aligning them on a single row or column RelativeLayout arrange Views through relative positions TableLayout arrange Views into rows and columns FrameLayout arrange a single View within a Layout AbsoluteLayout arrange Views through absolute positions Android: ViewGroups

27 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 27 FrameLayout  Block out an area on the screen to display a single item (i.e. a single View).  It should be used to display a single View within the Layout  Multiple views can be controlled through android:layout_gravity AbsoluteLayout  Arrange Views on the screen by specifying absolute x-y positions of each View.  Deprecated, since it is dependant of the screen resolution Android: ViewGroups

28 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 28 Android: Views and Layouts Components of the User Interface (UI) of an Activity ViewGroup View  View container.  Responsible for placing other Views on the display  Every layout must extend a ViewGroup  Basic component of the UI  Can handle/produce events  Every new UI component must extend a View

29 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 29 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:

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

31 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 31 Widgets can be defined in the XML layout files Widgets: Java and XML code < TextView android:id="@+id/textLabel” android:layout_width="match_parent" android:layout_height="wrap_content” android:visibility="visible” android:enabled="true” android:scrollbars="vertical” android:text=“Hello World” /> PROPERTIES, defined through android: … attributes

32 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 32 Widgets can be defined in XML and accessed from Java Widgets: Java and XML code < TextView android:id="@+id/name1” /> public TextView text; text=(TextView)findViewById(R.id.name1); JAVA XML CAST REQUIRED public TextView text; text=new TextView();

33 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 33  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();

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

35 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 35  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:text=”@string/textWelcome" android:id="@+id/textLabel" android:layout_width="wrap_content" android:layout_height="wrap_content” />

36 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 36  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

37 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 37  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, Emails, 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...

38 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 38  XML tags:  Similar to a TextView, but editable by the users  An appropriate keyboard will be displayed Widgets: EditText <EditText android:text=”@string/textDefault" android:id="@+id/editText” android:inputType= “textCapSentences” | “textCapWords” | “textAutoCorrect” | “textPassword” | “textMultiLane” />

39 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 39  CompoundButton: Button + state (checked/unchecked) Widgets: Button  XML tags:  Subclass of a TextView, but not directly editable by users  Can generate events related to click, long click, etc <Button android:text="@string/textButton" android:id="@+id/idButton" android:background="@color/blue” /> <item android:color="#ff819191” android:state_pressed="true”> res/color/blue.xml

40 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 40 Widgets: Button and CompoundButton checkBox CompoundButton XML tags: <checkBox android:layout_width="wrap_content” android:layout_height="wrap_content” android:id="@+id/buttonCheck” android:text="CheckBox” android:checked="true” />

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

42 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 42 Widgets: Button and CompoundButton radioButton CompoundButton XML tags: <RadioButton android:layout_width="wrap_content” android:layout_height="wrap_content” android:id="@+id/buttonRadio” android:text="ButtonRadio” android:checked="true” />

43 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 43 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

44 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 44 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:id="@+id/buttonRadio1" android:text="Option 1" android:checked="true” /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonRadio2" android:text="Option 2” />

45 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 45 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

46 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 46 Widgets: Button and CompoundButton toggleButton CompoundButton XML tags: <ToggleButton android:layout_width="wrap_content” android:layout_height="wrap_content” android:id="@+id/toggleButtonId” android:textOn="Button ON” android:textOff="Button OFF” android:checked="false” />

47 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 47 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

48 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 48 Widgets: Spinners Spinner component XML tags: <Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/spinnerId" android:entries="@array/stringOptions"> /> Option 1 Option 2 Option 3 Option 4 res/values.xml

49 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 49 Widgets: Button and CompoundButton DataPicker component XML tags: <DatePicker android:layout_width="wrap_content” android:layout_height="wrap_content” android:id=“@+id/datePickerId” android:endYear="1990” android:startYear="2014” android:maxDate="10/10/2014” />

50 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 50  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

51 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 51 Widgets: ImageView ImageView component XML tags: <ImageView android:layout_width="wrap_content” android:layout_height="wrap_content” android:id="@+id/imageId” android:src="@drawable/android” /> Source: android.jpg in drawable/

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

53 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 53  For a limited set of components, it is possible to manage the events through callbacks indicated in the XML layout. Views and Events <Button android:text="@string/textButton” android:id="@+id/idButton” android:onClick=”doSomething” /> public void doSomething(View w) { // Code to manage the click event } XML Layout File Java code

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

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

56 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 56 Views and Events: ActionListener public class ExampleActivity extends Activity implements OnClickListener { … } To handle events through the ActionListener mechanism: 1.Make the Activity implement the Listener Interface corrisponding to the Event generated by the View 1.Make the Activity implement the Listener Interface corrisponding to the Event generated by the View (Click Event in this example)

57 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 57 Views and Events: ActionListener public void onClick(View w) { … // Place here the code to manage the event } To handle events through the ActionListener mechanism: 2. Implement the abstract method of the Listener  this will be the callback method when the Event is fired 2. Implement the abstract method of the Listener  this will be the callback method when the Event is fired (Click Event in this example)

58 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events 58 Views and Events: ActionListener Button mybutton=(Button) findViewById(R.id.button1); mybutton.setOnClickListener(this); To handle events through the ActionListener mechanism: 3. Associate the ActionListener to the View through the method setEVENTListener(this) 3. Associate the ActionListener to the View through the method setEVENTListener(this) (Click Event in this example)

59 Luca Bedogni, Marco Di Felice - Programming with Android – ViewGroups, Views and Events (c) Luca Bedogni 2012 59  interface OnClickListener abstract method: onClick()  interface OnLongClickListener abstract method: onLongClick()  interface OnFocusChangeListener abstract method: onFocusChange()  interface OnKeyListener abstract method: onKey()  interface OnCheckedChangeListener abstract method: onCheckedChanged() Views and Events: ActionListener  interface OnItemSelectedListener abstract method: onItemSelected()  interface OnCheckedChangeListener abstract method: onCheckedChanged()  interface OnItemSelectedListener abstract method: onItemSelected()  interface OnTouchListener abstract method: onTouch()  interface OnCreateContextMenuListener abstract method: onCreateContextMenu() LIST OF ACTIONLISTENER INTERFACES

60 Luca Bedogni, Marco Di Felice – Events and Widgets (c) Luca Bedogni 2012 60  Can be done through performEVENT() events  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) { …. } It is also possible to fire an event directly from the Java code (without user’s interaction) … useful for debugging!

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

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


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

Similar presentations


Ads by Google