VIEW SYSTEM Roberto Beraldi View Users interact with an - - PowerPoint PPT Presentation

view system
SMART_READER_LITE
LIVE PREVIEW

VIEW SYSTEM Roberto Beraldi View Users interact with an - - PowerPoint PPT Presentation

VIEW SYSTEM Roberto Beraldi View Users interact with an application mostly through a Graphical User Interface (GUI) (Other means include: voice, sounds, etc.) GUI is a graphical drawing of one or more graphical objects Wiew s are


slide-1
SLIDE 1

VIEW SYSTEM

Roberto Beraldi

slide-2
SLIDE 2
slide-3
SLIDE 3

View

  • Users interact with an application mostly through a

Graphical User Interface (GUI)

  • (Other means include: voice, sounds, etc.)
  • GUI is a graphical drawing of one or more graphical
  • bjects
  • Wiews are the basic building block to be drawn
  • Each view has a rectangular shape and knows how to

draw itself. In addition, it responds to external events generated by the user, e.g. push a button

  • There are a lot of predefined views in the form of widgets
slide-4
SLIDE 4

View

  • Android Studio has a powerful tool to draw views on

different virtual devices and to help designing GUI

  • (Predefined) Views can be defined through an XML file or

programmatically

  • Each view has an id (android:id=+id/name)
  • This way the view can be retrieved through the method
  • findViewById(R.id.name)
  • The returned view should be casted to the specific view
slide-5
SLIDE 5

View group

  • A ViewGroup is a special invisible view that contains other

views (called children)

  • Example of view group
  • Layouts
  • Contains other views organized in different way
  • AdapterView
  • Used to display Views generated by an Adapter that takes data from

some data source, e.g., an array

  • For example ListView and ListAdapter
  • RadioGroup
slide-6
SLIDE 6

Basic widgets: label

slide-7
SLIDE 7

Basic widgets: button

A Button widget allows the simulation of a clicking action on a GUI Demo

slide-8
SLIDE 8

Basic widget: images

  • ImageView and ImageButton are two Android widgets that

allow embedding of images in your applications.

  • Both are image-based widgets analogue to TextView and

Button, respectively.

  • Each widget takes an android:src or android:background

attribute (in an XML layout) to specify what picture to use.

  • Pictures are usually reference a drawable resource.
  • ImageButton, is a subclass of ImageView. It adds the standard

Button behavior for responding to click events.

slide-9
SLIDE 9

Images

slide-10
SLIDE 10

Basic widgets: EditText

slide-11
SLIDE 11

Basic widgets: EditText

  • In addition to the standard TextView properties, EditTex

thas many others features such as:

  • android:autoText, (true/false) provides automatic spelling

assistance

  • android:capitalize, (words/sentences) automatic capitalization
  • android:digits, to configure the field to accept only certain digits
  • android:singleLine, is the field for single-line / multiple-line input
  • android:password, (true/false) controls field’s visibility
  • android:numeric, (integer, decimal, signed) controls numeric

format

  • android:phonenumber, (true/false) Formatting phone numbers
slide-12
SLIDE 12

Basic widgets: EditText

slide-13
SLIDE 13

AutoCompletTextView

slide-14
SLIDE 14

Common Operation on a view

  • 1.Set properties: for example setting the text of a TextView.

Properties that are known at build time can be set in the XML layout files.

  • 2.Set focus: The framework will handled moving focus in

response to user input. To force focus to a specific view, call requestFocus().

  • 3.Set up listeners: Views allow clients to set listeners that will

be notified when something interesting happens to the view. For example, a Button exposes a listener to notify clients when the button is clicked.

  • 4.Set visibility: You can hide or show views using

setVisibility(int).

slide-15
SLIDE 15

Example (Simple Tip)

slide-16
SLIDE 16

Some example of layouts

slide-17
SLIDE 17

Displaying a layout

  • Views are organized as a tree
  • The root element contains a set of view, some of which can be view

containers

  • Displaying a view requires two passages
  • Collecting the dimension of the child
  • Drawing itself….(each view draws itself on a Canvas, i.e., it

implements the onDraw(Canvas c) method)

  • To force a view to draw itself one has to call the invalidate() method
slide-18
SLIDE 18

What is an XML layout?

slide-19
SLIDE 19

What is an XML layout

  • Each XML file contains a tree of elements specifying a

layout of widgets and containers that make up one View.

  • The attributes of the XML elements are properties,

describing how a widget should look or how a container should behave.

  • Example:
  • If a Button element has an attribute value of
  • android:textStyle= "bold"
  • that means that the text appearing on the face of the button should

be rendered in a boldface font style.

slide-20
SLIDE 20

What is an XML layout?

  • The root element needs to declare the Android XML namespace

xmlns:android=http://schemas.android.com/apk/res/android

  • All other elements will be children of the root and will inherit that

namespace declaration.

  • Suppose we add a Button in the layout..
  • Because we want to reference this button from our Java code, we need

to give it an identifier via the android:id attribute.

  • The remaining attributes are properties of this Button instance:
  • android:text indicates the initial text to be displayed on the button face (in this case,

an empty string)

  • android:layout_width and android:layout_height tell Android to have the button's

width and height fill the "parent“ container, in this case the entire screen.

slide-21
SLIDE 21

Layouts

  • Frame, Linear, Relative, Table
  • FrameLayout
  • FrameLayout is the simplest type of layout object. It's basically a

blank space on your screen that you can later fill with a single

  • bject —for example, a picture that you'll swap in and out.
  • All child elements of the FrameLayout are pinned to the top left

corner of the screen; you cannot specify a different location for a child view.

  • Subsequent child views will simply be drawn over previous ones,

partially or totally obscuring them (unless the newer object is transparent).

slide-22
SLIDE 22

Layouts

  • Generally, complex UI designs result from the

combination of simpler nested boxes that show their inner pieces using a horizontal or vertical orientation

slide-23
SLIDE 23

Linear layout

LinearLayout aligns all children in a single direction — vertically or horizontally depending on the android:orientation attribute. All children are stacked one after the other, so a

  • vertical list will only have one child per row, no matter how

wide they are, and a

  • horizontal list will only be one row high (the height of the

tallest child, plus padding). A LinearLayout respects margins between children and the gravity(right, center, or left alignment) of each child.

slide-24
SLIDE 24

Linear Layout

  • LinearLayout is a box model –widgets or child containers

are lined up in a column or row, one after the next.

  • To configure a LinearLayout, you have five main areas of

control besides the container's contents:

  • orientation,
  • fill model,
  • weight,
  • gravity,
  • padding ,
  • margin
slide-25
SLIDE 25

Linear layout (fill model)

slide-26
SLIDE 26

Linear layout (fill model)

slide-27
SLIDE 27

Linear layout (fill model)

slide-28
SLIDE 28

Linear layout (weight)

slide-29
SLIDE 29

Linear layout (gravity)

slide-30
SLIDE 30

Linear layout (padding)

slide-31
SLIDE 31

Linear layout (padding)

slide-32
SLIDE 32

Linear layout (padding)

slide-33
SLIDE 33

Table layouts

1.TableLayout positions its children into rows and columns. 2.TableLayout containers do not display border lines. 3.The table will have as many columns as the row with the most cells. 4.A TableRow object defines a single row in the table.

  • 5. A row has zero or more cells, each cell is defined by any

kind of other View. 6.A cell may also be a ViewGroup object

slide-34
SLIDE 34

Table layout (example)

slide-35
SLIDE 35

Relative layout

1.RelativeLayoutlets child views specify their position relative to the parent view or to each other(specified by ID). 2.You can align two elements by right border, or make one below another, centered in the screen, centered left, and so on. 3.Elements are rendered in the order given, so if the first element is centered in the screen, other elements aligning themselves to that element will be aligned relative to screen center. 4.Also, because of this ordering, if using XML to specify this layout, the element that you will reference (in order to position

  • ther view objects) must be listed in the XML file before you refer

to it from the other views via its reference ID.

slide-36
SLIDE 36

Relative layout

slide-37
SLIDE 37

Relative layout

slide-38
SLIDE 38

Relative layout

slide-39
SLIDE 39

Relative layout

slide-40
SLIDE 40

Other view groups

  • Other two important ViewGroup are
  • Selection widgets
  • Radio Group
  • Checkbox
  • TimePicker
  • DatePicker
  • AdapterView
  • Define a container where a set of data has to be displayed
  • Each view of the data is created by an Adapter
  • There are predefined adapter
  • Custom adapters
  • Implements methods like getView(…)
slide-41
SLIDE 41

example: checkbox

slide-42
SLIDE 42

example: checkbox

slide-43
SLIDE 43

Example of checkbox

slide-44
SLIDE 44

Example of checkbox

slide-45
SLIDE 45

example: radio buttons

slide-46
SLIDE 46

Example: radio buttons

slide-47
SLIDE 47

Example

slide-48
SLIDE 48

Example

slide-49
SLIDE 49

Example

slide-50
SLIDE 50

FlagQuizGame (demo) *

(*): Credits: Deitel and Deitel, «Android for Programmers: An App-Driven Approach»

Table laoyout 3,6,9 Buttons inserted programmatically ImageView Options

slide-51
SLIDE 51

FlagQuizGame (data structure)

  • fileNameList:
  • List containing the total number of File Name
  • File name is Europe-Italy.png
  • quizCountryList
  • 10 names of country to guess taken at random from the previous

list

  • regionMap
  • <Name,Boolean> HashMap saying if a region is to be included or

not

  • totalNumberOfGuess (int)
  • totalCorrectAnswers (int)
slide-52
SLIDE 52

FlagQuizGame (Main Methods)

  • ResetQuiz
  • Reset the counters
  • Pick 10 names at random from enabled regions
  • loadNextFlag
  • Load the first (position 0) flag from fileNameList
  • Shuffle fileNameList
  • Put the correct answer to the end of such a list
  • Prepare the botton in the table
  • Replace a button at random with the correct answer
  • submitGuess
  • Check if the answer is correct
  • 2 Menu listeners
  • To set up enabled regions
  • To set the difficulty level
slide-53
SLIDE 53

AdapterView and Adapter

  • AdapterView is a special container of views generated by

an Adapter

  • There are a set of predefined adapter
  • A programmer can define its own adapter
slide-54
SLIDE 54

ListView and ListActivity (demo)

slide-55
SLIDE 55

GridView (demo)

slide-56
SLIDE 56

2D Graphic: extending a View

  • User can define its own View to draw specific graphical
  • bjects on a Canvas
  • Canvas are used to draw a Bitmap
  • This is achieved extending the View class
  • The system provides the canvas when it calls onDraw(Canvas c) method

implemented by the class that extends View

  • This is good for infrequent updates (no additional thread is

required)

  • To force draw the app can call invalidate() (on the UI-thread)
  • If another thread is created, then the View’s postInvalidate() method

should be called

slide-57
SLIDE 57

MotionEvents

  • Created when fingers touch the screen or move
  • Delivered to the appropriate listener
  • In case of complex movements they generate a gesture
  • A motion event contains
  • Action Code (state change that occurred)
  • UP,MOVE,DOWN
  • Action values
  • Position, time, etc-
slide-58
SLIDE 58

Multitouch

  • The screen is sensible to multiple touches at the same

time

  • Each finger is called a pointer and it receives a unique ID

as long as it is active

  • Touch events from different fingers are grouped together

inside a single MotionEvents object

  • Each pointer gets an index within the event (may change
  • ver time) which is not the same of the pointer ID
  • It is used as index to data structure to get specific data
slide-59
SLIDE 59

Some Action Code

  • ACTION_DOWN
  • The first finger touches the screen
  • ACTION_POINTER_DOWN
  • Another finger touches the screen
  • ACTION_POINTER_UP
  • One of the finger doesn’t touch the screen anymore
  • ACTION_MOVE
  • Some finger moves
  • ACTION_UP
  • The last finger leaves the screen
slide-60
SLIDE 60

Some method

  • getActionMasked()
  • The the action associate to the event
  • getAction()
  • The action code AND the pointer
  • getActionIndex()
  • The index of the finger the caused the action
  • getPointerCount()
  • Number of pointers associated to the event
  • getX(..),getY(..)
slide-61
SLIDE 61

Example (MotionEvent)

slide-62
SLIDE 62

Example (SimpleDraw)

slide-63
SLIDE 63

2D Graphic: SurfaceView and animation

  • The other option is to extend SurfaceView
  • Used for fast movement
  • Application gets a reference to a Surface, which is a low-level

drawing area

  • In addition, programmer should provide a secondary thread inside

which drawing operations are performed

  • The class should implement the SurfaceHolder.Callback

Interface

  • Defines lifecycle methods called when the surface changes
  • To setup the view getHolder() method must be called. It returns a

SurfaceHolder

  • In addition, callbacks methods should be registered using the

addCallback() SurfaceHolder’s method

slide-64
SLIDE 64

Surface

  • The lifecycle of a Surface is determined by the following 3

methods (called from the GUI thread):

  • surfaceCreated(…)
  • surfaceChanged(…)
  • surfaceDestroyed(..)
  • To draw on the surface its required to:
  • Acquire a lock on the underlying canvas object
  • SurfaceHolder.lockCanvas()
  • Draw
  • Canvas.drawBitmap()
  • Unlock the canvas
  • SurfaceHolder.unlockCanvasAndPost()
slide-65
SLIDE 65

Example (SurfaceView)

slide-66
SLIDE 66

Animation: Property animation

  • Programmatically or XML file
  • Creates an animation by modifying an object’s property

values over a set period with an Animator

  • ObjectAnimator:
  • set the value of a property of an object
  • Uses an interpolator to determine the amount of animation at a

given time

  • Using XML file the android:PropertyName attribute corresponds to

a setName method implemented in the object

slide-67
SLIDE 67

Property animation (demo)

slide-68
SLIDE 68

Animation: View animation

  • Tween animation
  • Creates an animation by performing a series of transformations on

a single Image with an Animation

  • Resizing, rotation, translation,alpha
  • Frame by frame animation
  • Shows a sequence of images in order with an AnimationDrawable
slide-69
SLIDE 69

TweenAnimation (demo)