I3350 - Chapter 7-8 - en
I3350 - Chapter 7-8 - en
Chapter 7
The menus
171
1. Introduction
Android supports 3 types of menu
• Option menu
• Context menu
• Popup menu
1. Introduction
The menus are declared
• in an XML file located in res/menu
Create a file for each type of menu
Example: option.xml or context.xml
173
1. Introduction
The main attributes of the tag item
• title: the item's title
• icon: a reference to a drawable to use as the item's icon
• orderInCategory: re-order the menu items
– by default, the order in the XML file
– to change the order, define its value (begin by 0)
• enable: to enable or disable items
– by default the item is enable
– inactive items appear in the menu but can’t be selected
• visible: to show or hide the items
174
1. Introduction
Example of a menu declaration: res/menu/option.xml
175
1. Introduction
Example of a menu declaration: res/menu/context.xml
176
1. Introduction
In Java:
177
1. Introduction
It is necessary to transform the XML file in the instance of Menu
• use the method getMenuInflater
Menu menu = new Menu();
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuInflater provides
– the menu items of the resource (ex: R.menu.option)
– to the object Menu or ContextMenu passed as a parameter
178
179
We can use
• the switch() function with the identifier of menu item.getItemId()
• to execute an action on the selected item
This method
• Returns true if the action is done, false otherwise
180
case R.id.reset:
// do something
return(true);
case R.id.item1:
// do something
return(true);
case R.id.item2:
// do something
return(true);
}
return(super.onOptionsItemSelected(item));
}
181
or
public class MenuContextuelActivity extends ListActivity {
private static final String[] items={“item1”, “item2”, … }
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
….
registerForContextMenu(getListView());
}
} 183
184
} 185
Chapter 8
Activities - Intent
187
1. Introduction
An application = set of activities
An activity = a screen
188
1. Introduction
Example:
When a call arrives
• it is placed at the top of the stack
• and it is it that is displayed in place of the application, which
becomes in second place
Principle
• we can only have one visible application on the terminal
• and what is visible is the graphical interface of the activity that
is at the top of the stack
189
1. Introduction
Android has a particular system:
• Anytime an application can leave place for another application
– that has a higher priority
• If an application uses too much system resources, then it will
prevent the system from working properly
– Android will stop it without warning
• An activity will exist in several states during his life
– for example an active state when used
– and a pause state when we receive a call
190
1. Introduction
In the AndroidManifest.xml file
• The activity is declared by the Activity element child of the application
element
<activity
android:name=".HelloActivity"
android:label="@string/app_name">
</activity>
191
1. Introduction
Example of AndroidManifest.xml file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.antislashn.android.activity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" > activity called first
<activity
android:label="@string/app_name"
android:name=".AppelactivityActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> activity can only be called by an explicit
<activity Intent
android:label="explicit"
android:name=".ExplicitActivity" >
</activity>
</application>
</manifest>
192
1. Introduction
An application contains several independent activities
One of these activities is launched automatically
• when the user launches the application Launcher
1. Introduction
An intent is a message sent to Android
• To tell him « I want you to do … something »
194
2. Explicit Intents
The explicit intents are used in the same application
Example:
• in the folder src, create a 2nd activity "Child1.java"
import android.app.Activity;
import android.os.Bundle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
195
}
2. Explicit Intents
This activity must be declared in the AndroidManifest.xml file
<manifest package="com.commonsware.android.intenttab"
xmlns:android="http://schemas.android.com/apk/res/android">
<application android:icon="@drawable/cw"
android:label="@string/app_name">
<activity android:label="@string/app_name"
android:name=".Principale">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
196
2. Explicit Intents
To call the activity "Child1" from the activity "Principale"
• we will create an instance of an object of type "Intent"
new Intent (this, Child1.class);
2. Explicit Intents
2 choices are possible:
198
2.1- startActivity()
In the Principale.java file
import android.app.Activity;
import android.content.Intent; //Package to indicate
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Principale extends Activity {
Button bouton_1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bouton_1=(Button)findViewById(R.id.bouton);
bouton_1.setOnClickListener(new OnClickListener(){
public void onClick (View view){
Intent intent=new Intent(Principale.this, Child1.class);
startActivity(intent);
}
});
}
} 199
2.1- startActivity()
Intent intent=new Intent(Principale.this, Child1.class);
creates an object "intent" of type "Intent"
• The 1st parameter corresponds to the current instance of the
"Principale" activity from where the "Principale.this"
• The 2nd parameter corresponds to the target of "intent" ie the
activity "Child1.class"
startActivity(intent);
sends the intent to its recipient with
• the single parameter the instance "intent“
200
2.1- startActivity()
In the Child1.java file
import android.app.Activity;
import android.os.Bundle;
public class Child1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
}
201
2.2- startActivityForResult()
To start the "Child1" activity from the "Principale" activity
• with notification of the end of the execution of "Child1"
We must use
• the method "startActivityForResult" instead of "StartActivity"
202
2.2- startActivityForResult()
The method "startActivityForResult" contians 2 parameters:
• intent and identifier
203
2.2- startActivityForResult()
This method is used to identify the activity "child1" that has just ended
2.2- startActivityForResult()
In the activity "child1", it is the method "setResult“
• which has as parameter the value of "resultCode“
• and which returns to the activity "parent" this value
205
2.2- startActivityForResult()
Example: we have 3 interfaces
• An interface main.xml for the activity Principale
• An interface main1.xml for the activity Child1
• An interface main2.xml for the activity Child2
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:text="Call activity Child1"
android:id="@+id/bouton1"
/>
<Button
android:text="Call activity Child2"
android:id="@+id/bouton2"
/>
</LinearLayout> 206
2.2- startActivityForResult()
main1.xml
</LinearLayout>
207
2.2- startActivityForResult()
main2.xml
</LinearLayout>
208
2.2- startActivityForResult()
Principale.java
public class Principale extends Activity { Button bouton_1; Button bouton_2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bouton_1=(Button)findViewById(R.id.bouton1);
bouton_2=(Button)findViewById(R.id.bouton2);
bouton_1.setOnClickListener(new OnClickListener(){
public void onClick (View view){
Intent intent=new Intent(Principale.this, Child1.class);
startActivityForResult(intent,1); } });
bouton_2.setOnClickListener(new OnClickListener(){
public void onClick (View view){
Intent intent=new Intent(Principale.this,Child2.class);
startActivityForResult(intent,2);
} });} 209
2.2- startActivityForResult()
Principale.java
public void onActivityResult (int requestCode, int resultCode, Intent data){
switch (requestCode){
case (1):
switch (resultCode){
case RESULT_OK:
Toast.makeText(this,"Child1 OK", Toast.LENGTH_SHORT).show();
return;
case RESULT_CANCELED:
Toast.makeText(this,"Child1 Canceled", Toast.LENGTH_SHORT).show();
return; }
case (2):
switch (resultCode){
case RESULT_OK:
Toast.makeText(this," Child2 OK", Toast.LENGTH_SHORT).show();
return;
case RESULT_CANCELED:
Toast.makeText(this,"Child2 Canceled", Toast.LENGTH_SHORT).show();
return; }
210
} } }
2.2- startActivityForResult()
Child1.java
public class Child1 extends Activity { Button B_OK; Button B_CA;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
B_OK=(Button)findViewById(R.id.boutonOK1);
B_CA=(Button)findViewById(R.id.boutonCANCEL1);
} } 211
2.2- startActivityForResult()
Child2.java
public class Child2 extends Activity { Button B_OK; Button B_CA;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
B_OK=(Button)findViewById(R.id.boutonOK2);
B_CA=(Button)findViewById(R.id.boutonCANCEL2);
}} 212
2.2- startActivityForResult()
We have 2 buttons:
• one button "Call Activity Child1"
• and one button "Call Activity Child2"
2.2- startActivityForResult()
If we click on "CANCEL"
• the "RESULT_CANCELED" ("resultCode") is sent to activity "Principale"
– using the method "setResult(RESULT_CANCELED)"
– "Child1" ends with the method "finish()"
2.3- putExtra()
The intents also allow to "transport" data
• using the method "putExtra()"
intent.putExtra (key,value)
215
2.3- putExtra()
To extract the value of the intent, it is necessary
to use an object of type "Bundle“
• Bundle objet_Bundle = this.getIntent().getExtras()
Example
• getString if the transported value is of type String
• getInt if the transported value is of type Integer
• etc. 216
2.3- putExtra()
Example: main.xml
2.3- putExtra()
main1.xml
2.3- putExtra()
Principale.java
public class Principale extends Activity {Button bouton; EditText texte_name;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bouton=(Button)findViewById(R.id.bouton);
bouton.setOnClickListener(new OnClickListener(){
public void onClick (View view){
texte_name=(EditText)findViewById(R.id.editT);
String name = texte_name.getText().toString();
Intent intent=new Intent(Principale.this,Child1.class);
intent.putExtra("myName", name);
startActivityForResult(intent,1); } }); }
public void onActivityResult (int requestCode, int resultCode, Intent data){
switch (resultCode){
case RESULT_OK:
Toast.makeText(this,"Child1 OK", Toast.LENGTH_SHORT).show();
return;
case RESULT_CANCELED:
Toast.makeText(this,"Child1 Canceled", Toast.LENGTH_SHORT).show();
219
return; } } }
2.3- putExtra()
Child1.java
public class Child1 extends Activity { Button B_OK; Button B_CA; TextView textV; private String name;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
Bundle infoName = this.getIntent().getExtras();
if(infoName != null){
name=infoName.getString("myName"); }
textV=(TextView)findViewById(R.id.textV);
textV.setText("Hello "+name+" Welcome to activity Child1");
B_OK=(Button)findViewById(R.id.boutonOK);
B_CA=(Button)findViewById(R.id.boutonCANCEL);
B_OK.setOnClickListener (new OnClickListener () {
public void onClick (View view) {
setResult(RESULT_OK);
finish(); } });
B_CA.setOnClickListener (new OnClickListener () {
public void onClick (View view) {
setResult(RESULT_CANCELED);
finish(); } }); }
220
}
2.3- putExtra()
The activity "Principale" contains
• a "EditText" to enter the user’s name
• and a button to launch the activity "Child1"
221
2.3- putExtra()
String name = texte_name.getText().toString();
• allows to retrieve the text entered by the user
intent.putExtra("myName", name);
• add an information to the intent
• key= "myName", value = name the variable of type String defined above
222
2.3- putExtra()
Bundle infoName = this.getIntent().getExtras();
• creates a "Bundle" object that extracts information from the intent
if(infoName != null)
• verifies that the object of type "Bundle" ("infoName") is not empty
name=infoName.getString("myName");
• the method "getString" (having as parameter the key "myName")
• allows to give a value to the variable of type String "name"
223
2.3- putExtra()
It is also possible to pass information
• from the activity "child1" to the activity "parent"
Just
• create an object of type Intent "empty" in the activity "child1"
• use the method "putExtra" to "fill" the Intent object with the information to be
transmitted
2.3- putExtra()
Example: Child1.java
public class Child1 extends Activity { Button B_OK; Button B_CA; EditText votre_age; private String age;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
B_OK=(Button)findViewById(R.id.boutonOK);
B_CA=(Button)findViewById(R.id.boutonCANCEL);
votre_age=(EditText)findViewById(R.id.editT);
B_OK.setOnClickListener (new OnClickListener () {
public void onClick (View view) {
age=votre_age.getText().toString();
Intent result= new Intent ();
result.putExtra("mykey",age);
setResult(RESULT_OK, result);
finish(); } });
B_CA.setOnClickListener (new OnClickListener () {
public void onClick (View view) {
setResult(RESULT_CANCELED);
finish(); } });
}
225
}
2.3- putExtra()
Principale.java
public class Principale extends Activity { Button bouton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bouton=(Button)findViewById(R.id.bouton);
bouton.setOnClickListener(new OnClickListener(){
public void onClick (View view){
Intent intent=new Intent(Principale.this,child1.class);
startActivityForResult(intent,1); } }); }
public void onActivityResult (int requestCode, int resultCode, Intent data){
switch (resultCode){
case RESULT_OK:
String age=data.getStringExtra("mykey");
Toast.makeText(this,"Vous avez "+age+" ans", Toast.LENGTH_SHORT).show();
return;
case RESULT_CANCELED:
Toast.makeText(this,"Vous ne voulez pas donner votre age",
Toast.LENGTH_SHORT).show();
return; } }
226
}
2.3- putExtra()
Intent result= new Intent ();
• creates an instance of type "Intent" (with empty parenthesis)
– no "target" activity and no "requestCode"
– This intent will only serve us to "transport" information
result.putExtra("mykey",age);
• allows to add a data of type "String" to the intent "result"
setResult(RESULT_OK, result);
• returns the data in the intent to the activity "Principale"
String age=data.getStringExtra("mykey");
• allows to retrieve the string corresponding to the key "mykey"
227
3. Intent implicites
These intents allow to launch other applications
For example
• if we click on a phone number and we want to call it
• it is the system that finds the application that can call it
228
3. Intent implicites
The Intent takes 2 parameters
• a constant
• and an object of type "Uri"
229
3. Intent implicites
The constant
The Android system offers several native actions
• each action is represented by a static constant of the class "Intent"
230
3. Intent implicites
L'uri
3. Intent implicites
To transform a character string
• into "uri" that can be used by an object of type "Intent"
we must use
• the method parse(String uri)
To create an instance
• of type "uri"
we must use
Uri uri = Uri.parse(String uri)
232
3. Intent implicites
Example with url as uri
main.xml
233
3. Intent implicites
Principale.java
234
3. Intent implicites
"uri" is an instance
• of the class "Uri"
235
3. Intent implicites
Example with a phone number as uri
main.xml
236
3. Intent implicites
Principale.java
3. Intent implicites
If we launch the application as is, it will not work!
238
3. Intent implicites
AndroidManifest.xml
<manifest package="com.commonsware.android.intenttab"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application android:icon="@drawable/cw"
android:label="@string/app_name">
<activity android:label="@string/app_name"
android:name=".Principale">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest> 239