SlideShare uma empresa Scribd logo
1 de 10
Baixar para ler offline
CS425 - Web and Mobile Software Engineering
Dr. Lilia Sfaxi
17 avril 2017
Basic Graphical Elements and
Intents
Android Lab 01
BASICS AND INTENTS DR. LILIA SFAXI !1
MedTech
I. Views and ViewGroups
The class View represents the main class for creating a graphical element in Android. It is the
main class for all widgets, used to create interactive graphical components (buttons, text views,
text inputs,…).
A view occupies a rectangular space on the screen, responsible for the management of the
events initiated by the user.
A child class ViewGroup is defined, representing the main class for all layouts, invisible
containers for Views or ViewGroups, and defining their placement on the scree.
Each element in a graphical interface is either a View or a ViewGroup. The dispositions of
these elements in the screen are defined with Layouts. A layout is a ViewGroup that groups a
set of widgets or other layouts, enabling thus to define their dispositions in the screen. We can
site for example, FrameLayout, LinearLayout, RelativeLayout,…
II. Exercise 1: Basic Graphical Components
1. Objective
This first part of the lab aims to create a simple conversion application, having an interface
looking like the one below.
Activity 1: Start by creating the graphical interface. Launch
the emulator and check that the result is correct.
In what follows, we define the steps to define the behaviour of this interface.
BASICS AND INTENTS DR. LILIA SFAXI !2
2. Behaviour of the Button
Having a button defined in the XML layout, which identifier is b_convert. To manage this
button, three ways are available:
• Method 1: Rewriting the Click Listener method
In Java, a listener is an object enabling the programmer to react to actions of the user (mouse
click, keyboard, etc.). In our case, we need a listener for a click on the button, called
onClickListener. Listener is an interface, providing methods that must be implemented by the
programmer for each element. For example, the onClickListener contains a method onClick,
that we must implement to represent the behaviour of the button.
1. Create an attribute in your activity of type Button:
private Button bConvert;
2. In the method onCreate, initialise the new attribute bConvert by associating the
graphical button defined in the XML layout:
this.bConvert = (Button) this.findViewById(R.id.b_convert) ;
3. Use the following code to define a behaviour for the click on the button bConvert. It
can be done in the method onCreate, to be enabled at the creation of the activity.
this.bConvert.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

//behaviour of the button

}

});
• Method 2: Associating a method to the button
An easier manner to define the behaviour of the button, is to associate a special method for
the click on the button. The problem is that this solution works only for the click, but is not
provided for other events, like a long click, for example.
1. In the XML layout file, as an attribute of the button element, add:
android:onClick="convert"
This attribute indicates that a method called convert is defined in the activity, and
represents the behaviour on the click of the button.
2. In the code of the activity, add the following method:
public void convert(View v){
// behaviour of the button
}
The method convert, has a particular signature: it must be public, return void, and take as a
parameter a View object. It has the same signature as the onClick of the first method. The
BASICS AND INTENTS DR. LILIA SFAXI !3
View object passed as a parameter represents the graphical object that was clicked, hence the
button. If you use this method, it is useless to create a Button attribute in the activity, you will
not need it.
• Method 3: Implementing the OnClickListener Interface
It is possible to use inheritance to implement the onClick method, without calling
setOnClickListener each time. This method can be useful if you want to group the
behaviours of a set of elements in the same class.
1. Your activity must implement the OnClickListener interface.
public class MoneyConverter extends Activity implements
OnClickListener {…}
2. Create the attribute bConvert and associate if the XML element b_convert as shown in
the first method.
3. Define the current activity as the listener to this button.
bConvert.setOnClickListener(this);
4. Override the onClick method directly in your activity:
public void onClick(View v) {
if (v.getId()==R.id.b_convert){
//behaviour of the button
}
}
Be careful, this onClick method will be common to all the graphical elements that use the
current activity as an OnClickListener. To distinguish which element was clicked, test the id
of the view (as shown in the code above).
3. Behaviour of an EditText
An EditText is a graphic object that enables the application to receive input from the user.
1. Create an attribute in your activity :
private EditText eEntry;
2. In the onCreate method, initialise the attribute:
this.eEntry = (EditText) this.findViewById(R.id.e_entry) ;
3. Define its behaviour. For an input element, the main functionalities are:
• Reading the content:
String s = eEntry.getText().toString();
• Modifying the content:
eEntry.setText("New Text »);
BASICS AND INTENTS DR. LILIA SFAXI !4
4. Behaviour of a TextView
A TextView is a graphical objet that displays an immutable string. It is used by the application
in the same manner as the EditText.
5. Behaviour of a Radio Button
A radio button is a box with two states: checked or unchecked. Radio buttons are in general
used in a RadioGroup, where only one button can be checked.
1. Create an attribute RadioButton in your activity and associate it to the appropriate
XML element.
2. To test the state of you button, call the method isChecked().
if (rDinarEuro.isChecked() ){
// code
}
3. To dynamically change the state of a radio button, use setChecked(state):
radio1.setChecked(true);
radio2.setChecked(false);
There is no need to create a Java object for the RadioGroup. If radio1 and radio2 of the
previous example are in the same RadioGroup, it is unnecessary to change both their states: if
you check one of them, the other will be automatically unchecked.
Activity 2: As shown above, define the behaviour of the elements
of your interface, using the method of your choice.
III. Exercise 2: Intents
1. Objective
The objective of this part is to change the behaviour of the previous application to obtain the
following result:
BASICS AND INTENTS DR. LILIA SFAXI !5
2. Intents: Definition
An Android application can contain a lot of activities. An activity uses the method
setContentView to be associated to a graphical layout. Basically, activities are independent
from one another, but they can collaborate to exchange data and actions.
Typically, one of the activities is defined as a launcher activity, the one that is displayed at the
bootstrapping of the application.
All the activities interact in an asynchronous mode. To switch from an activity to another, the
first creates an intent. It is a message that can be used to trigger an action from another
component of the application. It can invoke activities, broadcast receivers or services. The
methods used to do so are:
• startActivity(intent) : starts an activity
• sendBroadcast(intent) : sends an intent to all the concerned Broadcast receivers
• startService(intent) ou bindService(intent, …) : launches a service
An intent contains informations used by the Android system:
• The name of the component to start
• The action to realise: ACTION-VIEW, ACTION_SEND…
• The data: a URI referencing the data on which the action will be performed
• The category: additional information about the type of the components that handle the
intent: CATEGORY-BROWSABLE, CATEGORY-LAUNCHER…
• The extras: key-value pairs containing information to be sent to the target component
• The flags: Metadata class for this intent, showing for example how to launch an activity
and what to do once it is launched.
There are mainly two types of intents: Explicit and Implicit intents
3. Explicit Intents
Explicit intents specify the component to start by name (complete name of the class). They
enable starting a component of your own application, as the name is known. For example:
start another activity as a response to an action of the user.
The main arguments of an explicit intent are:
• The context starting the intent. In general, it is the activity from which it was launched.
• The target component.
It is typically called like the following:
Intent myActivityIntent =
new Intent (StartClass.this, EndClass.class) ;
startActivity (myActivityIntent) ;
BASICS AND INTENTS DR. LILIA SFAXI !6
The data shared between activities can be sent as extras. An extra is a key-value element, sent
to the target component. It is created as follows:
myActivityIntent.putExtra("clef","valeur");
Of course, all extras must be attached to the intent before calling startActivity.
It is also possible to start an activity with a result, by establishing a bi-directional link between
activities. To receive a result from another activity, call startActivityForResult instead of
startActivity.
Destination activity must of course be designed to send back a result once the operation
performed, and this is done using an intent. The main activity will receive it and will hand it
using the predefined method onActivityResult.
This is an example of an intent with a result:
Activity1:
Activity2:
BASICS AND INTENTS DR. LILIA SFAXI !7
The result displayed by this code is the following:
Activity 3: Start by creating the interface of the second
activity. Implement then the code of the back button, that enables
to come back to the previous activity to display the result in a
toast.
4. Implicit Intents
Implicit intents don’t call specific components, but declare an action to realise. They enable a
component of an application to call another component, even though it is in another
application.
Example: show a position in a map.
The main arguments of an implicit intent are :
• The action to realise, which can be predefined (ACTION-VIEW, ACTION_EDIT…)
or created by the user.
• The data: the main data we are sending, like the phone number to call.
It is typically called as follows:
Intent myActivityIntent = new Intent (<action>, <donnee>) ;
startActivity(myActivityIntent) ;
An implicit intent works as follows:
1. The activity A creates an intent with an action and sends it as a parameter of
startActivity.
2. The Android system looks for all the applications to find an intent filter corresponding
to this intent.
3. When a correspondance is found, the system starts the activity B, by invoking its
onCreate method and sending the intent to it.
BASICS AND INTENTS DR. LILIA SFAXI !8
Implicit intents use the notion of intent filter to find the target activity to start. An intent filter
is an expression in the manifest file of an application, which specifies the type of intent that
the component wants to receive. It enables to other activities to launch directly your activity
by using a certain intent.
If you don’t declare intent filters in your activity, it can’t be started unless you use an explicit
intent. It is nonetheless recommended not to declare intent filters for services, for security
reasons.
This is an example of an intent filter:
These are some examples of commonly used actions :
Most of the actions need permissions to add in the manifest file. For example, to authorise
your activity to start a call, add the following line in the manifest:
Action Data Description
ACTION_DIAL tel:123 Displays the telephone dialer with the number 123
ACTION_VIEW http://www.google.com Displays the Google page in a browser
ACTION_EDIT content://contacts/people/2 Edits the information on the person of your
contacts book with the id 2
ACTION_VIEW content://contacts/people/2 Displays an activity that displays the information
of the contact with id 2
ACTION_VIEW content://contacts/people Displays the contact list. The selection of a
contact can display the details in a new intent
BASICS AND INTENTS DR. LILIA SFAXI !9
<uses-permission android:name="android.permission.CALL_PHONE">
</uses-permission>
An example of an implicit intent enabling to send a text message:
The resolveActivity method helps avoiding a crash of the application if the called activity does
not exist. It is a kind of exception handling.
The result will be as follows:
Activity 4: Implement the code of the call button, that enables
to start a phone call to a predefined number.
BASICS AND INTENTS DR. LILIA SFAXI !10

Mais conteúdo relacionado

Mais procurados

A comprehensive guide on developing responsive and common react filter component
A comprehensive guide on developing responsive and common react filter componentA comprehensive guide on developing responsive and common react filter component
A comprehensive guide on developing responsive and common react filter componentKaty Slemon
 
Django tutorial
Django tutorialDjango tutorial
Django tutorialKsd Che
 
Android App Development - 04 Views and layouts
Android App Development - 04 Views and layoutsAndroid App Development - 04 Views and layouts
Android App Development - 04 Views and layoutsDiego Grancini
 
Chapt 04 user interaction
Chapt 04 user interactionChapt 04 user interaction
Chapt 04 user interactionEdi Faizal
 
Using intents in android
Using intents in androidUsing intents in android
Using intents in androidOum Saokosal
 
Events and Listeners in Android
Events and Listeners in AndroidEvents and Listeners in Android
Events and Listeners in Androidma-polimi
 
Android activity
Android activityAndroid activity
Android activityKrazy Koder
 
[Individual presentation] android fragment
[Individual presentation] android fragment[Individual presentation] android fragment
[Individual presentation] android fragmentGabriele Vecchia
 
WordPress plugin #2
WordPress plugin #2WordPress plugin #2
WordPress plugin #2giwoolee
 
What's New in Android
What's New in AndroidWhat's New in Android
What's New in AndroidRobert Cooper
 
Create an android app for database creation using.pptx
Create an android app for database creation using.pptxCreate an android app for database creation using.pptx
Create an android app for database creation using.pptxvishal choudhary
 
ProTips DroidCon Paris 2013
ProTips DroidCon Paris 2013ProTips DroidCon Paris 2013
ProTips DroidCon Paris 2013Mathias Seguy
 

Mais procurados (20)

Eclipse Tricks
Eclipse TricksEclipse Tricks
Eclipse Tricks
 
A comprehensive guide on developing responsive and common react filter component
A comprehensive guide on developing responsive and common react filter componentA comprehensive guide on developing responsive and common react filter component
A comprehensive guide on developing responsive and common react filter component
 
Unit i informatica en ingles
Unit i informatica en inglesUnit i informatica en ingles
Unit i informatica en ingles
 
Android sql examples
Android sql examplesAndroid sql examples
Android sql examples
 
Django tutorial
Django tutorialDjango tutorial
Django tutorial
 
Android App Development - 04 Views and layouts
Android App Development - 04 Views and layoutsAndroid App Development - 04 Views and layouts
Android App Development - 04 Views and layouts
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Chapt 04 user interaction
Chapt 04 user interactionChapt 04 user interaction
Chapt 04 user interaction
 
Android 3
Android 3Android 3
Android 3
 
Using intents in android
Using intents in androidUsing intents in android
Using intents in android
 
Events and Listeners in Android
Events and Listeners in AndroidEvents and Listeners in Android
Events and Listeners in Android
 
Android activity
Android activityAndroid activity
Android activity
 
[Individual presentation] android fragment
[Individual presentation] android fragment[Individual presentation] android fragment
[Individual presentation] android fragment
 
WordPress plugin #2
WordPress plugin #2WordPress plugin #2
WordPress plugin #2
 
My first slide
My first slideMy first slide
My first slide
 
Day 6
Day 6Day 6
Day 6
 
What's New in Android
What's New in AndroidWhat's New in Android
What's New in Android
 
Create an android app for database creation using.pptx
Create an android app for database creation using.pptxCreate an android app for database creation using.pptx
Create an android app for database creation using.pptx
 
ProTips DroidCon Paris 2013
ProTips DroidCon Paris 2013ProTips DroidCon Paris 2013
ProTips DroidCon Paris 2013
 
Java awt
Java awtJava awt
Java awt
 

Semelhante a Lab1-android

Android Bootcamp Tanzania:intents
Android Bootcamp Tanzania:intentsAndroid Bootcamp Tanzania:intents
Android Bootcamp Tanzania:intentsDenis Minja
 
How to Detect a Click Outside a React Component.pptx
How to Detect a Click Outside a React Component.pptxHow to Detect a Click Outside a React Component.pptx
How to Detect a Click Outside a React Component.pptxBOSC Tech Labs
 
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptxMugiiiReee
 
android_mod_3.useful for bca students for their last sem
android_mod_3.useful for bca students for their last semandroid_mod_3.useful for bca students for their last sem
android_mod_3.useful for bca students for their last semaswinbiju1652
 
Android App Development - 02 Activity and intent
Android App Development - 02 Activity and intentAndroid App Development - 02 Activity and intent
Android App Development - 02 Activity and intentDiego Grancini
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intentPERKYTORIALS
 
android activity
android activityandroid activity
android activityDeepa Rani
 
Create an other activity lesson 3
Create an other activity lesson 3Create an other activity lesson 3
Create an other activity lesson 3Kalluri Vinay Reddy
 
Intent, Service and BroadcastReciver (2).ppt
Intent, Service and BroadcastReciver (2).pptIntent, Service and BroadcastReciver (2).ppt
Intent, Service and BroadcastReciver (2).pptBirukMarkos
 
Unit 5 Activity and Activity Life Cycle.pptx
Unit 5 Activity and Activity Life Cycle.pptxUnit 5 Activity and Activity Life Cycle.pptx
Unit 5 Activity and Activity Life Cycle.pptxShantanuDharekar
 
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxxMAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx34ShreyaChauhan
 
Data Transfer between activities and Database
Data Transfer between activities and Database Data Transfer between activities and Database
Data Transfer between activities and Database faiz324545
 

Semelhante a Lab1-android (20)

Android Bootcamp Tanzania:intents
Android Bootcamp Tanzania:intentsAndroid Bootcamp Tanzania:intents
Android Bootcamp Tanzania:intents
 
Activity & Shared Preference
Activity & Shared PreferenceActivity & Shared Preference
Activity & Shared Preference
 
How to Detect a Click Outside a React Component.pptx
How to Detect a Click Outside a React Component.pptxHow to Detect a Click Outside a React Component.pptx
How to Detect a Click Outside a React Component.pptx
 
Android intents in android application-chapter7
Android intents in android application-chapter7Android intents in android application-chapter7
Android intents in android application-chapter7
 
unit3.pptx
unit3.pptxunit3.pptx
unit3.pptx
 
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
 
Android App development III
Android App development IIIAndroid App development III
Android App development III
 
android_mod_3.useful for bca students for their last sem
android_mod_3.useful for bca students for their last semandroid_mod_3.useful for bca students for their last sem
android_mod_3.useful for bca students for their last sem
 
Android App Development - 02 Activity and intent
Android App Development - 02 Activity and intentAndroid App Development - 02 Activity and intent
Android App Development - 02 Activity and intent
 
Android development session 2 - intent and activity
Android development   session 2 - intent and activityAndroid development   session 2 - intent and activity
Android development session 2 - intent and activity
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intent
 
android activity
android activityandroid activity
android activity
 
Create an other activity lesson 3
Create an other activity lesson 3Create an other activity lesson 3
Create an other activity lesson 3
 
Level 1 &amp; 2
Level 1 &amp; 2Level 1 &amp; 2
Level 1 &amp; 2
 
Intent, Service and BroadcastReciver (2).ppt
Intent, Service and BroadcastReciver (2).pptIntent, Service and BroadcastReciver (2).ppt
Intent, Service and BroadcastReciver (2).ppt
 
Unit 5 Activity and Activity Life Cycle.pptx
Unit 5 Activity and Activity Life Cycle.pptxUnit 5 Activity and Activity Life Cycle.pptx
Unit 5 Activity and Activity Life Cycle.pptx
 
iOS Development (Part 2)
iOS Development (Part 2)iOS Development (Part 2)
iOS Development (Part 2)
 
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxxMAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx
 
Android action bar and notifications-chapter16
Android action bar and notifications-chapter16Android action bar and notifications-chapter16
Android action bar and notifications-chapter16
 
Data Transfer between activities and Database
Data Transfer between activities and Database Data Transfer between activities and Database
Data Transfer between activities and Database
 

Mais de Lilia Sfaxi

chp1-Intro à l'urbanisation des SI.pdf
chp1-Intro à l'urbanisation des SI.pdfchp1-Intro à l'urbanisation des SI.pdf
chp1-Intro à l'urbanisation des SI.pdfLilia Sfaxi
 
Plan d'études_INSAT_2022_2023.pdf
Plan d'études_INSAT_2022_2023.pdfPlan d'études_INSAT_2022_2023.pdf
Plan d'études_INSAT_2022_2023.pdfLilia Sfaxi
 
Lab1-DB-Cassandra
Lab1-DB-CassandraLab1-DB-Cassandra
Lab1-DB-CassandraLilia Sfaxi
 
TP2-UML-Correction
TP2-UML-CorrectionTP2-UML-Correction
TP2-UML-CorrectionLilia Sfaxi
 
TP1-UML-Correction
TP1-UML-CorrectionTP1-UML-Correction
TP1-UML-CorrectionLilia Sfaxi
 
TP0-UML-Correction
TP0-UML-CorrectionTP0-UML-Correction
TP0-UML-CorrectionLilia Sfaxi
 
TD4-UML-Correction
TD4-UML-CorrectionTD4-UML-Correction
TD4-UML-CorrectionLilia Sfaxi
 
TD3-UML-Séquences
TD3-UML-SéquencesTD3-UML-Séquences
TD3-UML-SéquencesLilia Sfaxi
 
TD3-UML-Correction
TD3-UML-CorrectionTD3-UML-Correction
TD3-UML-CorrectionLilia Sfaxi
 
TD2 - UML - Correction
TD2 - UML - CorrectionTD2 - UML - Correction
TD2 - UML - CorrectionLilia Sfaxi
 
TD1-UML-correction
TD1-UML-correctionTD1-UML-correction
TD1-UML-correctionLilia Sfaxi
 
Android - Tp1 - installation et démarrage
Android - Tp1 -   installation et démarrageAndroid - Tp1 -   installation et démarrage
Android - Tp1 - installation et démarrageLilia Sfaxi
 
Android - Tp2 - Elements graphiques
Android - Tp2 - Elements graphiques Android - Tp2 - Elements graphiques
Android - Tp2 - Elements graphiques Lilia Sfaxi
 
Android - Tp3 - intents
Android - Tp3 -  intentsAndroid - Tp3 -  intents
Android - Tp3 - intentsLilia Sfaxi
 
Android - TPBonus - web services
Android - TPBonus - web servicesAndroid - TPBonus - web services
Android - TPBonus - web servicesLilia Sfaxi
 
Android - Tp4 - graphiques avancés
Android - Tp4 - graphiques avancésAndroid - Tp4 - graphiques avancés
Android - Tp4 - graphiques avancésLilia Sfaxi
 

Mais de Lilia Sfaxi (20)

chp1-Intro à l'urbanisation des SI.pdf
chp1-Intro à l'urbanisation des SI.pdfchp1-Intro à l'urbanisation des SI.pdf
chp1-Intro à l'urbanisation des SI.pdf
 
Plan d'études_INSAT_2022_2023.pdf
Plan d'études_INSAT_2022_2023.pdfPlan d'études_INSAT_2022_2023.pdf
Plan d'études_INSAT_2022_2023.pdf
 
Lab3-DB_Neo4j
Lab3-DB_Neo4jLab3-DB_Neo4j
Lab3-DB_Neo4j
 
Lab2-DB-Mongodb
Lab2-DB-MongodbLab2-DB-Mongodb
Lab2-DB-Mongodb
 
Lab1-DB-Cassandra
Lab1-DB-CassandraLab1-DB-Cassandra
Lab1-DB-Cassandra
 
TP2-UML-Correction
TP2-UML-CorrectionTP2-UML-Correction
TP2-UML-Correction
 
TP1-UML-Correction
TP1-UML-CorrectionTP1-UML-Correction
TP1-UML-Correction
 
TP0-UML-Correction
TP0-UML-CorrectionTP0-UML-Correction
TP0-UML-Correction
 
TD4-UML
TD4-UMLTD4-UML
TD4-UML
 
TD4-UML-Correction
TD4-UML-CorrectionTD4-UML-Correction
TD4-UML-Correction
 
TD3-UML-Séquences
TD3-UML-SéquencesTD3-UML-Séquences
TD3-UML-Séquences
 
TD3-UML-Correction
TD3-UML-CorrectionTD3-UML-Correction
TD3-UML-Correction
 
TD2 - UML - Correction
TD2 - UML - CorrectionTD2 - UML - Correction
TD2 - UML - Correction
 
TD1 - UML - DCU
TD1 - UML - DCUTD1 - UML - DCU
TD1 - UML - DCU
 
TD1-UML-correction
TD1-UML-correctionTD1-UML-correction
TD1-UML-correction
 
Android - Tp1 - installation et démarrage
Android - Tp1 -   installation et démarrageAndroid - Tp1 -   installation et démarrage
Android - Tp1 - installation et démarrage
 
Android - Tp2 - Elements graphiques
Android - Tp2 - Elements graphiques Android - Tp2 - Elements graphiques
Android - Tp2 - Elements graphiques
 
Android - Tp3 - intents
Android - Tp3 -  intentsAndroid - Tp3 -  intents
Android - Tp3 - intents
 
Android - TPBonus - web services
Android - TPBonus - web servicesAndroid - TPBonus - web services
Android - TPBonus - web services
 
Android - Tp4 - graphiques avancés
Android - Tp4 - graphiques avancésAndroid - Tp4 - graphiques avancés
Android - Tp4 - graphiques avancés
 

Último

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 

Último (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

Lab1-android

  • 1. CS425 - Web and Mobile Software Engineering Dr. Lilia Sfaxi 17 avril 2017 Basic Graphical Elements and Intents Android Lab 01 BASICS AND INTENTS DR. LILIA SFAXI !1 MedTech
  • 2. I. Views and ViewGroups The class View represents the main class for creating a graphical element in Android. It is the main class for all widgets, used to create interactive graphical components (buttons, text views, text inputs,…). A view occupies a rectangular space on the screen, responsible for the management of the events initiated by the user. A child class ViewGroup is defined, representing the main class for all layouts, invisible containers for Views or ViewGroups, and defining their placement on the scree. Each element in a graphical interface is either a View or a ViewGroup. The dispositions of these elements in the screen are defined with Layouts. A layout is a ViewGroup that groups a set of widgets or other layouts, enabling thus to define their dispositions in the screen. We can site for example, FrameLayout, LinearLayout, RelativeLayout,… II. Exercise 1: Basic Graphical Components 1. Objective This first part of the lab aims to create a simple conversion application, having an interface looking like the one below. Activity 1: Start by creating the graphical interface. Launch the emulator and check that the result is correct. In what follows, we define the steps to define the behaviour of this interface. BASICS AND INTENTS DR. LILIA SFAXI !2
  • 3. 2. Behaviour of the Button Having a button defined in the XML layout, which identifier is b_convert. To manage this button, three ways are available: • Method 1: Rewriting the Click Listener method In Java, a listener is an object enabling the programmer to react to actions of the user (mouse click, keyboard, etc.). In our case, we need a listener for a click on the button, called onClickListener. Listener is an interface, providing methods that must be implemented by the programmer for each element. For example, the onClickListener contains a method onClick, that we must implement to represent the behaviour of the button. 1. Create an attribute in your activity of type Button: private Button bConvert; 2. In the method onCreate, initialise the new attribute bConvert by associating the graphical button defined in the XML layout: this.bConvert = (Button) this.findViewById(R.id.b_convert) ; 3. Use the following code to define a behaviour for the click on the button bConvert. It can be done in the method onCreate, to be enabled at the creation of the activity. this.bConvert.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
 //behaviour of the button
 }
 }); • Method 2: Associating a method to the button An easier manner to define the behaviour of the button, is to associate a special method for the click on the button. The problem is that this solution works only for the click, but is not provided for other events, like a long click, for example. 1. In the XML layout file, as an attribute of the button element, add: android:onClick="convert" This attribute indicates that a method called convert is defined in the activity, and represents the behaviour on the click of the button. 2. In the code of the activity, add the following method: public void convert(View v){ // behaviour of the button } The method convert, has a particular signature: it must be public, return void, and take as a parameter a View object. It has the same signature as the onClick of the first method. The BASICS AND INTENTS DR. LILIA SFAXI !3
  • 4. View object passed as a parameter represents the graphical object that was clicked, hence the button. If you use this method, it is useless to create a Button attribute in the activity, you will not need it. • Method 3: Implementing the OnClickListener Interface It is possible to use inheritance to implement the onClick method, without calling setOnClickListener each time. This method can be useful if you want to group the behaviours of a set of elements in the same class. 1. Your activity must implement the OnClickListener interface. public class MoneyConverter extends Activity implements OnClickListener {…} 2. Create the attribute bConvert and associate if the XML element b_convert as shown in the first method. 3. Define the current activity as the listener to this button. bConvert.setOnClickListener(this); 4. Override the onClick method directly in your activity: public void onClick(View v) { if (v.getId()==R.id.b_convert){ //behaviour of the button } } Be careful, this onClick method will be common to all the graphical elements that use the current activity as an OnClickListener. To distinguish which element was clicked, test the id of the view (as shown in the code above). 3. Behaviour of an EditText An EditText is a graphic object that enables the application to receive input from the user. 1. Create an attribute in your activity : private EditText eEntry; 2. In the onCreate method, initialise the attribute: this.eEntry = (EditText) this.findViewById(R.id.e_entry) ; 3. Define its behaviour. For an input element, the main functionalities are: • Reading the content: String s = eEntry.getText().toString(); • Modifying the content: eEntry.setText("New Text »); BASICS AND INTENTS DR. LILIA SFAXI !4
  • 5. 4. Behaviour of a TextView A TextView is a graphical objet that displays an immutable string. It is used by the application in the same manner as the EditText. 5. Behaviour of a Radio Button A radio button is a box with two states: checked or unchecked. Radio buttons are in general used in a RadioGroup, where only one button can be checked. 1. Create an attribute RadioButton in your activity and associate it to the appropriate XML element. 2. To test the state of you button, call the method isChecked(). if (rDinarEuro.isChecked() ){ // code } 3. To dynamically change the state of a radio button, use setChecked(state): radio1.setChecked(true); radio2.setChecked(false); There is no need to create a Java object for the RadioGroup. If radio1 and radio2 of the previous example are in the same RadioGroup, it is unnecessary to change both their states: if you check one of them, the other will be automatically unchecked. Activity 2: As shown above, define the behaviour of the elements of your interface, using the method of your choice. III. Exercise 2: Intents 1. Objective The objective of this part is to change the behaviour of the previous application to obtain the following result: BASICS AND INTENTS DR. LILIA SFAXI !5
  • 6. 2. Intents: Definition An Android application can contain a lot of activities. An activity uses the method setContentView to be associated to a graphical layout. Basically, activities are independent from one another, but they can collaborate to exchange data and actions. Typically, one of the activities is defined as a launcher activity, the one that is displayed at the bootstrapping of the application. All the activities interact in an asynchronous mode. To switch from an activity to another, the first creates an intent. It is a message that can be used to trigger an action from another component of the application. It can invoke activities, broadcast receivers or services. The methods used to do so are: • startActivity(intent) : starts an activity • sendBroadcast(intent) : sends an intent to all the concerned Broadcast receivers • startService(intent) ou bindService(intent, …) : launches a service An intent contains informations used by the Android system: • The name of the component to start • The action to realise: ACTION-VIEW, ACTION_SEND… • The data: a URI referencing the data on which the action will be performed • The category: additional information about the type of the components that handle the intent: CATEGORY-BROWSABLE, CATEGORY-LAUNCHER… • The extras: key-value pairs containing information to be sent to the target component • The flags: Metadata class for this intent, showing for example how to launch an activity and what to do once it is launched. There are mainly two types of intents: Explicit and Implicit intents 3. Explicit Intents Explicit intents specify the component to start by name (complete name of the class). They enable starting a component of your own application, as the name is known. For example: start another activity as a response to an action of the user. The main arguments of an explicit intent are: • The context starting the intent. In general, it is the activity from which it was launched. • The target component. It is typically called like the following: Intent myActivityIntent = new Intent (StartClass.this, EndClass.class) ; startActivity (myActivityIntent) ; BASICS AND INTENTS DR. LILIA SFAXI !6
  • 7. The data shared between activities can be sent as extras. An extra is a key-value element, sent to the target component. It is created as follows: myActivityIntent.putExtra("clef","valeur"); Of course, all extras must be attached to the intent before calling startActivity. It is also possible to start an activity with a result, by establishing a bi-directional link between activities. To receive a result from another activity, call startActivityForResult instead of startActivity. Destination activity must of course be designed to send back a result once the operation performed, and this is done using an intent. The main activity will receive it and will hand it using the predefined method onActivityResult. This is an example of an intent with a result: Activity1: Activity2: BASICS AND INTENTS DR. LILIA SFAXI !7
  • 8. The result displayed by this code is the following: Activity 3: Start by creating the interface of the second activity. Implement then the code of the back button, that enables to come back to the previous activity to display the result in a toast. 4. Implicit Intents Implicit intents don’t call specific components, but declare an action to realise. They enable a component of an application to call another component, even though it is in another application. Example: show a position in a map. The main arguments of an implicit intent are : • The action to realise, which can be predefined (ACTION-VIEW, ACTION_EDIT…) or created by the user. • The data: the main data we are sending, like the phone number to call. It is typically called as follows: Intent myActivityIntent = new Intent (<action>, <donnee>) ; startActivity(myActivityIntent) ; An implicit intent works as follows: 1. The activity A creates an intent with an action and sends it as a parameter of startActivity. 2. The Android system looks for all the applications to find an intent filter corresponding to this intent. 3. When a correspondance is found, the system starts the activity B, by invoking its onCreate method and sending the intent to it. BASICS AND INTENTS DR. LILIA SFAXI !8
  • 9. Implicit intents use the notion of intent filter to find the target activity to start. An intent filter is an expression in the manifest file of an application, which specifies the type of intent that the component wants to receive. It enables to other activities to launch directly your activity by using a certain intent. If you don’t declare intent filters in your activity, it can’t be started unless you use an explicit intent. It is nonetheless recommended not to declare intent filters for services, for security reasons. This is an example of an intent filter: These are some examples of commonly used actions : Most of the actions need permissions to add in the manifest file. For example, to authorise your activity to start a call, add the following line in the manifest: Action Data Description ACTION_DIAL tel:123 Displays the telephone dialer with the number 123 ACTION_VIEW http://www.google.com Displays the Google page in a browser ACTION_EDIT content://contacts/people/2 Edits the information on the person of your contacts book with the id 2 ACTION_VIEW content://contacts/people/2 Displays an activity that displays the information of the contact with id 2 ACTION_VIEW content://contacts/people Displays the contact list. The selection of a contact can display the details in a new intent BASICS AND INTENTS DR. LILIA SFAXI !9
  • 10. <uses-permission android:name="android.permission.CALL_PHONE"> </uses-permission> An example of an implicit intent enabling to send a text message: The resolveActivity method helps avoiding a crash of the application if the called activity does not exist. It is a kind of exception handling. The result will be as follows: Activity 4: Implement the code of the call button, that enables to start a phone call to a predefined number. BASICS AND INTENTS DR. LILIA SFAXI !10