Lab01 Android
Lab01 Android
Lab01 Android
In what follows, we define the steps to define the behaviour of this interface.
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
}
});
This attribute indicates that a method called convert is defined in the activity, and
represents the behaviour on the click of the button.
2. Create the attribute bConvert and associate if the XML element b_convert as shown in
the first method.
3. Define its behaviour. For an input element, the main functionalities are:
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.
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.
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:
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) ;
ACTION_DIAL tel:123 Displays the telephone dialer with the number 123
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:
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: