a guided tour in android world - portail...

81
6-1-2016 © For internal use © For internal use A guided tour in Android World Practical Work Session 2

Upload: others

Post on 28-May-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

6-1-2016

© For internal use © For internal use

A guided tour in Android World

Practical Work Session 2

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Who are we ?

2

▶ Maxime PERON

▶ François Julien Ritaine

R&D Engineer Mobile expert

[email protected]

▶ Ibrahim GHARBI

Mobile software

architect [email protected]

Mes Transferts

L’Appli Mon Coffre

Our job explained in 7 minutes (koreus.com)

▶ François FACON

SDCO Engineer

Web & Mobile [email protected]

Practical Work - Session 2

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

▶ Go to http://www.socrative.com

– Then click on « STUDENT LOGIN »

▶ Wait until I start the Quizz, then enter your « first name » & « last name » like this:

FirstName LastName

▶ Complete the Quizz…

3

Quizz time About you…

From A to H From I to Z

T6SXFNK4 CWVS25TY

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline 4

…What we will do together

Objectives

1. Dive into The Android World

2. Take a look at an application

(main structure, components, GUI, tools…)

3. Work in a team and make

great apps !

4. Share our experiences,

tools, to be more efficient

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Planning P2

5

D1

26/11

D2

03/12

D3

10/12

D4

17/12

D5

07/01

D6

14/01

P1

21/01

P2

28/01

- A bit of History… - Deep in android - Inside an Android application

Hello World

Twitter App

Step1

QUIZZ D1 QUIZZ D2 QUIZZ D3 QUIZZ D4 QUIZZ D5 QUIZZ D6

Twitter App

Step2

- ListView

- Custom Cell

- Custom

- Adapter

- ViewHolder

- ImageView

- Bitmap

- LayoutInflater

Twitter App

Step3

Twitter App

Step4

Twitter App

Step5

- Layout

- Activity

- Intent

- Buttons

- Listeners

- Toast

- Preferences

- Menu

- Thread

- AsyncTask

- Fragment

- ListView

- Adapter

- SQLite

- SQLiteDatabase

- ContentValues

- Cursor

- ContentProvider

- ContentResolver

- URI

- Authority

- CursorAdapter

- ContentObserver

- Service

- PendingIntent

- AlarmManager

- BroadcastReceiver

- Notification

I.Gharbi F.Facon

Project part 1

Project part 2

I.Gharbi F.Facon M.Peron

Project teams Project presentation Practical Work – Session 2

I.Gharbi F.Facon

I.Gharbi F.Facon

I.Gharbi F.Facon

I.Gharbi F.Facon

FJ.Ritaine

I.Gharbi F.Facon

FJ.Ritaine

I.Gharbi F.Facon

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Ressources

6

Download this presentation here http://awl.li/ISENSession2

Download the app sources here

http://awl.li/ISENBaseApp

Practical Work – Session 2

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline 7

Last Session Fundamental - Session 1

Android Studio

1.3+

Gradle

2.4

Android Gradle Plugin

1.1.0

Build tools

21.1.2

Minimum SDK

14

Targeted SDK

21

SupportV4 version

21.0.+

AppCompat version

21.0.3

Source code available @ http://awl.li/ISENTP2

- A bit of History…

- Deep in android - Inside an Android application

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Agenda

▶ Rewind : Inside an android application

– Activity

– Layout

▶ PW1 – Part 1 : Add a Login Activity

▶ Activity workflow & basic user interaction

– Context

– Intents

– Buttons and listeners

– Toast

– Preferences

– Menu

▶ PW1 – Part 2 : Navigate & share data between activity

8

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

An Android app from the inside

9

Activity (1/9) : Principles

▶ An Activity is an application component that provides a screen (such as dial the phone, take a photo, send an email, or view a map)

▶ Each activity is given a window in which to draw its user interface.

▶ Because state of your application changes, activity has a lifecycle

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

An Android app from the inside

10

Activity (2/9) : Lifecycle

▶ Many event can occur when your app is running

▶ SDK use callback to properly handle lifecycle

▶ Three essential states :

– Resumed

• Running, in foreground

– Paused

• Running, but another activity is on top

• Other activity doesn’t cover the entire screen

– Stopped

• In background, another activity is on top

• Other activity covers the entire screen

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

An Android app from the inside

11

Activity (3/9) : Lifecycle in your code

▶ An Activity is a java object with different types.

from

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

An Android app from the inside

12

Activity (4/9) : Layout & Views

▶ An activity provide a user interface with a hierarchy of views packaged in a single layout file located on the Layout Folder

▶ The language to describe a layout is XML

▶ ViewGroup : used to layout multiple views

from

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

An Android app from the inside

13

Activity (5/9) : Layout & Views

▶ You can edit your layout in plain XML or with the layout editor where you can drag and drop views

▶ It’s also possible to create a layout and views with code only.

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

An Android app from the inside

14

▶ Android provides a number of ready-made views that you can use to customize and organize your layout.

Activity (6/9) : Layout & Views

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

An Android app from the inside

15

Activity (7/9) : Layout & Views

▶ The layout is provided to the activity programmatically by “setContentView” methods :

HOW EVERYTHING IS DRAWN (INOVEX.DE)

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

An Android app from the inside

16

Activity (8/9) : the manifest declaration

▶ You must declare your activity in the manifest file in order for it to be accessible to the system.

▶ Lots of activity properties could be set inside the manifest (portrait/landscape mode, theme …)

MANIFEST PROPERTIES FOR AN ACTIVITY

(ANDROID DEVELOPPER)

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

An Android app from the inside

17

▶ “An Intent is a messaging object you can use to request an action from another app component ” (ex: activities)

▶ Intent filter : it specifies the type of intents accepted by the activity based on the intent's action, data, and category. The system will deliver an implicit intent to your app component only if the intent can pass through one of your intent filters.

▶ “Launcher” is the default one when you create an application

INTENT FILTERS (ANDROID DEVELOPPER)

Activity (9/9) : the manifest declaration

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

An Android app from the inside

18

▶ Aligns all children in a single direction

▶ Attributes :

– android:orientation

set child views Vertically or horizontally

– android:layout_weight

assigns an "importance" value to a view

Layouts in detail (1/4) : The LinearLayout

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

▶ Displays child views in relative positions :

– Relative to the parent relativelayout

– Relative to other childs (on the left, on the bottom…)

▶ The XML order of the views count along with the properties

An Android app from the inside

19

Layouts in detail (2/4) : The RelativeLayout

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Agenda

▶ Rewind : Inside an android application

– Activity

– Layout

▶ PW1 – Part 1 : Add a Login Activity

▶ Activity workflow & basic user interaction

– Context

– Intents

– Buttons and listeners

– Toast

– Preferences

– Menu

▶ PW1 – Part 2 : Navigate & share data between Activity

22

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Legend

23

Coding time !

Be smart !

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Overview

24

WLTwitterLoginActivity

Layout XML

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

PW1 – Part 1 : Add a Login Activity

25

1/6 import the WLTwitterStep0 project

▶ Twitter application !!!

▶ First we need to authenticate with Twitter backend

▶ So we need a login Activity

▶ Unzip WLTwitterStep0.zip and import the project

Download the app here http://awl.li/ISENBaseApp

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

PW1 – Part 1 : Add a Login Activity

26

2/6 add a new RelativeLayout

▶ First create the new XML layout file

– Right click on res/layout and new>Android XML file

▶ Resource type : layout, name it, and select RelativeLayout as Root Element

▶ Remember the different layouts

– LinearLayout : layout children in a linear way (horizontal or vertical)

– AbsoluteLayout : layout children with absolute positions

– RelativeLayout : layout children relative to their parent or themselves

– GridLayout : layout children using a grid

– FrameLayout : layout children in a frame

– …

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

PW1 – Part 1 : Add a Login Activity

27

3/6 Add a new imageView

▶ Now add the bottom right image

▶ Add an ImageView (either WYSIWYG, or type xml)

▶ put this ImageView at bottom right

– android:layout_alignParentBottom="true"

– android:layout_alignParentRight="true"

▶ Wrap_content as width and height, to take the same size as the picture

▶ Set the source of the image in this ImageView

– android:src="@drawable/twitter"

▶ Set the content description for the accessibility

– android:contentDescription="@string/app_name"

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

PW1 – Part 1 : Add a Login Activity

28

4/6 add another RelativeLayout

▶ Now add the center panel

▶ Add another RelativeLayout inside our root RelativeLayout

▶ Wrap_content match_parent as width and set android:layout_marginTop="100dp"

▶ Add some margins

– android:layout_marginLeft="30dp"

– android:layout_marginRight="30dp"

▶ Add a background color

– #FF44BBE3

– Declare it in a new resource file in res/values/color.xml

– android:background="@drawable/blue_round_corners_background"

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

PW1 – Part 1 : Add a Login Activity

29

5/6 add input and button

▶ Inside that center panel, we need two EditText and a Button

▶ Add the first EditText to enter login, and the second one to enter password

– android:inputType="text"

– android:inputType="textPassword’’

▶ You can add a hint to inform the user

– android:hint="@string/loginHint’’

▶ Add ids to uniquely identify those components

– android:id="@+id/loginEditText"

– android:id="@+id/passwordEditText"

▶ Layout the password EditText to be below

▶ Add the Button at the bottom right of the panel android:background="@drawable/white_blue_round_corners_selector’’

android:id="@+id/loginButton’’

▶ Wrap_content as height for the center panel.

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

PW1 – Part 1 : Add a Login Activity

30

6/6 Create and register the login activity

▶ We have the layout, now display it within an Activity

▶ Create a new class in the package, name it WLTwitterLoginActivity

▶ Make this class extends Activity

– Override the onCreate method to load our XML layout

– setContentView(R.layout.activity_login);

▶ Now declare this Activity in Manifest

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Let’s create your login activity

Twitter project

31

Login activity

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Agenda

▶ Rewind : Inside an android application

– Activity

– Layout

▶ PW1 – Part 1 : Add a Login Activity

▶ Activity workflow & basic user interaction

– Context

– Intents

– Buttons and listeners

– Toast

– Preferences

– Menu

▶ PW1 – Part 2 : Navigate & share data between Activity

32

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

33

UI Thread

▶ UI Thread / Main Thread

– When started, an app has one thread inside process

– We can only modify GUI in this thread

– Avoid blocking/long operation (network, FS, database, compute)

• Use another threads

• NetworkOnMainThreadException since API 11

▶ Keep application as smooth as possible

▶ Use standard component

▶ Give feedback to the user (GUI, sound, vibrate, …)

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

from

Activity workflow & basic user interaction

35

Context

▶ It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

36

Context : activity context and application context

Application instance is tied to the lifecycle of the application, while the Activity instance is tied to the lifecycle of an Activity. Thus, they have access to different information about the application environment.

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

37

Basics, once more

Activity

BroadCastReceiver

Intent ContentProvider

Service

start

listen

send

listen

bind

Call with URI result

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

38

Intent/Intent Filter

• An Intent is a messaging object you can use to request an action from another app component.

• Three fundamental use-cases: • To start an activity • To start a service • To deliver a broadcast

INTENTS AND INTENT FILTERS (ANDROID DEVELOPPER)

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

39

Intent/Intent Filter/Intent Types

There are two types of intents:

• Explicit intents specify the component to start by name

• Implicit intents declare a general action to perform

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

40

Intent/Intent Filter/Intent Types/Implicit intent

1. Activity A creates an Intent with an action description and passes it to startActivity().

2. The Android System searches all apps for an intent filter that matches the intent. When a match is found,

3. the system starts the matching activity (Activity B) by invoking its onCreate() method and passing it the Intent.

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

41

Intent/Intent Filter/Building an Intent

An Intent object carries information that the Android system uses to determine which component to start, plus information that the recipient component uses in order to properly perform the action. The primary information contained in an Intent is the following:

• Component name • Action • Data • Category • Extras • Flags

INTENT (ANDROID DEVELOPPER)

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

42

Intent/Intent Filter/Building an Intent/Component name

Component name

• Name of the component to start. • Important, makes the differences between implicit/explicit

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

43

Intent/Intent Filter/Building an Intent/Action

Action • Specifies the generic action to perform (such as view or pick).

• Some common actions for starting an activity: • ACTION_VIEW use this when you have something to show to

the user • ACTION_SEND use this when you have some data that the user

can share through another app • …

• If you define your own actions, be sure to include your app's package

name as a prefix. For example:

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

44

Intent/Intent Filter/Building an Intent/Data

Data • The URI that references the data to be acted on and/or the MIME type

of that data. • For example, an activity that's able to display images probably

won't be able to play an audio file, even though the URI formats could be similar. So specifying the MIME type of your data helps the Android system find the best component to receive your intent.

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

45

Intent/Intent Filter/Building an Intent/Category

Category • A string containing additional information about the kind of component

that should handle the intent. • Here are some common categories:

• CATEGORY_BROWSABLE The target activity allows itself to be started by a web browser to display data referenced by a link—such as an image or an e-mail message.

• CATEGORY_LAUNCHER The activity is the initial activity of a task and is listed in the system's application launcher.

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

46

Intent/Intent Filter/Building an Intent/Extras

Extras • Key-value pairs that carry additional information required to accomplish

the requested action. • For example, when creating an intent to send an email with

ACTION_SEND, you can specify the "to" recipient with the EXTRA_EMAIL key, and specify the "subject" with the EXTRA_SUBJECT key.

• The Intent class specifies many EXTRA_* constants for standardized

data types. If you need to declare your own extra keys (for intents that your app receives), be sure to include your app's package name as a prefix. For example:

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

48

Intent/Intent Filter/Building an Intent/Example explicit Intent

• An explicit intent is one that you use to launch a specific app component

• To create an explicit intent, define the component name for the Intent object

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

49

Intent/Intent Filter/Building an Intent/Example implicit intent

• An implicit intent specifies an action that can invoke any app on the device able to perform the action.

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

50

Intent/Intent Filter/more later on

• Still lots of things to learn on the Intents, but we’ll see that later on if time doesn’t flies by

• Forcing an app chooser • Receiving an Implicit Intent • Using a Pending Intent • More on Intent Resolution, and so on…

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Agenda

▶ Rewind : Inside an android application

– Activity

– Layout

▶ PW1 – Part 1 : Add a Login Activity

▶ Activity workflow & basic user interaction

– Context

– Intents

– Buttons and listeners

– Toast

– Preferences

– Menu

▶ PW1 – Part 2 : Navigate & share data between Activity

51

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

52

Buttons and listeners

A button consists of text or an icon (or both text and an icon) that communicates what action occurs when the user touches it.

You can create the button in your layout in three ways:

- With text, using the Button class

- With an icon, using the ImageButton class

- With text and an icon, using the Button class with the android:drawableLeft attribute

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

53

Buttons and listeners/Responding to Click Events

• When the user clicks a button, the Button object receives an on-click event. • The value for this attribute must be the name of the method you

want to call in response to a click event.

Within the Activity that hosts this layout, the following method handles the click event.

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

54

Buttons and listeners/Using an onClickListener

• You can also declare the click event handler programmatically rather than in an XML layout. • To declare the event handler programmatically, create an

View.OnClickListener object and assign it to the button by calling setOnClickListener(View.OnClickListener).

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

55

Buttons and listeners/Styling your button/Borderless

• Borderless buttons resemble basic buttons except that they have no borders or background but still change appearance during different states, such as when clicked.

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

56

Buttons and listeners/Styling your button/Custom Background

• To create a state list drawable for your button background: • Create three bitmaps for the button background that represent

the default, pressed, and focused button states. • Place the bitmaps into the res/drawable/ directory of your project. • Be sure each bitmap is named properly to reflect the button state

that they each represent, such as button_default.9.png, button_pressed.9.png, and button_focused.9.png.

• Create a new XML file in the res/drawable/ directory (name it

something like button_custom.xml). Insert the following XML:

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

57

Buttons and listeners/Styling your button/Custom Background

• Then simply apply the drawable XML file as the button background

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Agenda

▶ Rewind : Inside an android application

– Activity

– Layout

▶ PW1 – Part 1 : Add a Login Activity

▶ Activity workflow & basic user interaction

– Intents

– Buttons and listeners

– Toast

– Preferences

– Menu

▶ PW1 – Part 2 : Navigate & share data between Activity

58

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

59

Toast

• A toast provides simple feedback about an operation in a small popup. • It only fills the amount of space required for the message • The current activity remains visible and interactive.

TOAST (ANDROID DEVELOPPER)

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

60

Toast/The Basics

• Instantiate a Toast object with one of the makeText() methods. • Display the Toast with the show() method.

• You can also chain your methods and avoid holding on to the Toast object, like this:

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Agenda

▶ Rewind : Inside an android application

– Activity

– Layout

▶ PW1 – Part 1 : Add a Login Activity

▶ Activity workflow & basic user interaction

– Context

– Intents

– Buttons and listeners

– Toast

– Preferences

– Menu

▶ PW1 – Part 2 : Navigate & share data between Activity

62

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

63

Preferences/Saving Key-Value Sets

If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences APIs.

• A SharedPreferences object points to a XML file containing key-value pairs

• It provides simple methods to read and write them.

• Each SharedPreferences file is managed by the framework and can be private or shared.

SHAREDPREFERENCES (ANDROID DEVELOPPER)

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

64

Preferences/Get a Handle to a SharedPreferences

• You can create a new shared preference file or access an existing one by calling one of two methods: • getSharedPreferences() • getPreferences()

• When naming your shared preference files, you should use a name that's uniquely identifiable to your app, such as "com.example.myapp.PREFERENCE_FILE_KEY“

• Alternatively, if you need just one shared preference file for your activity, you can use the getPreferences() method:

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

66

Preferences/Read from SharedPreferences

• To retrieve values from a shared preferences file, call methods such as getInt() and getString(), providing the key for the value you want, and optionally a default value to return if the key isn't present. For example:

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Agenda

▶ Rewind : Inside an android application

– Activity

– Layout

▶ PW1 – Part 1 : Add a Login Activity

▶ Activity workflow & basic user interaction

– Context

– Intents

– Buttons and listeners

– Toast

– Preferences

– Menu

▶ PW1 – Part 2 : Navigate & share data between Activity

67

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

68

Menus

• Menus are a common user interface component in many types of applications.

• Three fundamental types of menus or action presentations on all versions of Android • Options menu and action bar • Context menu and contextual action mode • Popup menu

• We’ll only see Options menu and action bar

MENU (ANDROID DEVELOPPER)

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

69

Menus/Defining in XML

• For all menu types, Android provides a standard XML format to define menu items.

• To define the menu, create an XML file inside your project's res/menu/ directory and build the menu with the following elements: • <menu> Defines a Menu, which is a container for menu items. • <item> Creates a MenuItem, which represents a single item in a

menu. • <group> An optional, invisible container for <item> elements.

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

70

Menus/Defining in XML/item

• The <item> element supports several attributes you can use to define an item's appearance and behavior. The items in the above menu include the following attributes • android:id A resource ID that's unique to the item, which allows

the application to recognize the item when the user selects it. • android:icon A reference to a drawable to use as the item's icon. • android:title A reference to a string to use as the item's title.

• android:showAsAction Specifies when and how this item should appear as an action item in the action bar.

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

71

Menus/Defining in XML/submenu

• You can add a submenu to an item in any menu (except a submenu) by adding a <menu> element as the child of an <item>. Submenus are

useful when your application has a lot of functions that can be organized into topics, like items in a PC application's menu bar (File, Edit, View, etc.). For example:

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

72

Menus/Options Menus

• The options menu is where you should include actions and other options that are relevant to the current activity context, such as "Search," "Compose email," and "Settings."

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

73

Menus/Options Menus/Handling Click Events

• When the user selects an item from the options menu, the system calls your activity's onOptionsItemSelected() method.

• This method passes the MenuItem selected. • You can identify the item by calling getItemId(), which returns the

unique ID for the menu item. • You can match this ID against known menu items to perform the

appropriate action. For example:

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

74

Menus/Options Menus

• You can declare items for the options menu from your Activity subclass.

• To specify the options menu for an activity, override onCreateOptionsMenu(). In this method, you can inflate your menu resource (defined in XML) into the Menu provided in the callback. For example:

• You can also add menu items using add() and retrieve items with findItem() to revise their properties with MenuItem APIs.

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Activity workflow & basic user interaction

75

Menus/Options Menus/Changing Menu Items at runtime

• After the system calls onCreateOptionsMenu(), it retains an instance of the Menu you populate and will not call onCreateOptionsMenu() again unless the menu is invalidated for some reason.

• However, you should use onCreateOptionsMenu() only to create the initial menu state and not to make changes during the activity lifecycle.

• If you want to modify the options menu based on events that occur during the activity lifecycle, you can do so in the onPrepareOptionsMenu() method. This method passes you the Menu object as it currently exists so you can modify it, such as add, remove, or disable items.

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Agenda

▶ Rewind : Inside an android application

– Activity

– Layout

▶ PW1 – Part 1 : Add a Login Activity

▶ Activity workflow & basic user interaction

– Context

– Intents

– Buttons and listeners

– Toast

– Preferences

– Menu

▶ PW1 – Part 2 : Navigate & share data between Activity

76

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Overview

77

WLTwitterLoginActivity

Layout XML

WLTwitterActivity

Layout XML

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

PW1–P2:Navigate & share data between Activity

78

1/5 Check Inputs

▶ Check if the user entered his login/password

▶ Add an onClickListener to the button

– Retrieve the button with findViewById using its id

– findViewById(R.id.loginButton).setOnClickListener(this);

▶ Let the Activity implement the OnClickListener interface

– And override the onClick(View view) method

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

PW1–P2:Navigate & share data between Activity

79

2/5 Toast or send intent

▶ Now retrieve the two EditText and check if they’re empty

– TextUtils.isEmpty(editText.getText())

▶ If they’re empty, display a Toast to the user

– Toast.makeText(this, “Error”, Toast.LENGTH_LONG).show();

▶ If everything is ok, launch the home activity, to do that create an Intent

– Intent intent = new Intent(this, WLTwitterActivity.class);

– startActivity(intent);

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

PW1–P2:Navigate & share data between Activity

80

3/5 pass data to another activity

▶ Pass the login as parameter to the second Activity

– Add a Bundle to the Intent

– Bundle extras = new Bundle();

– extras.putString(key, value);

– Intent.putExtras(extras);

▶ Retrieve the login in the second Activity and set as ActionBar subtitle

– String login = getIntent().getExtras().getString(key);

– getActionBar().setSubtitle(login);

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

PW1–P2:Navigate & share data between Activity

81

4/5 Persist data in SharedPreferences

▶ Store the login and password in the Preferences

– Key/value pair storage in application, with defined r/w/x

▶ How to store in Preferences

– context.getSharedPreferences(“fileName”, Context.MODE_PRIVATE);

– prefs.edit().putString(key,value).commit();

▶ In onCreate

– prefs.getString(key, defaultValue);

▶ In our LoginActivity, when created, check if we already have login/password in Preferences

– If we do, directly launch the second Activity

– Still pass the login as parameter

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

PW1–P2:Navigate & share data between Activity

82

5/5 update Menu

▶ Define a Menu in the second Activity to logout

– Logout will erase the login and password in Preferences

– Logout will also go back to the Login Screen

▶ res/menu/wltwitter.xml

– Change the title and id

▶ In WLTwitterActivity

– onOptionsItemSelected() to handle click on menu item

– finish() to close the current Activity and go back to the previous

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

Create your login activity interactions ( listeners, input checks, intents)

My First Application

83

Start an android project

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline 84

– XML Layout

• RelativeLayout

• ImageView

• EditText

• Button

– String resources

– Activity

– Listener

– Toast

– Intent

– Preferences

– Menu

Summary Practical Work - Session 2

Android Studio

1.3+

Gradle

2.4

Android Gradle Plugin

1.1.0

Build tools

21.1.2

Minimum SDK

14

Targeted SDK

21

SupportV4 version

21.0.+

AppCompat version

21.0.3

| 1/6/2016 | Maxime Peron, Bruno Verachten, Ibrahim Gharbi, François Facon © Worldline

That’s all for Today

▶ quartet programming next week (4 people)

▶ Send your project before 08/12 Midnight

▶ Precise each member of your group on the email

[email protected]

▶ Be ready for 2nd Quizz…

85

Download this presentation here http://awl.li/ISENSession2