Les Toast sont des vues qui permettent d’afficher des informations courtes à l’utilisateur. Par exemple lorsque l’utilisateur est en train d’écrire un SMS et passe à une autre application, l’application SMS lui notifie l’enregistrement ou non à l’aide d’un Toast.
Création du Toast
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.buttonToast);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),”Bonjour le monde Toast !”, Toast.LENGTH_LONG).show();
} });
}
Ce code permet de voir un Toast. La méthode statique est makeText qui renvoie un Toast. Pour l’affichage il suffit d’appeler la méthode show() de l’instance créée.
Figure 33 Exemple de Toast
Conclusion
Cette activité présente les intents qui permettent de lancer une autre activité dans la même application soit de manière implicite ou explicite.
Évaluation
Exercice 1 Création d’un Intent implicite
Le but de cet exercice est de créer un Intent Implicit qui va permettre de lancer le navigateur.
1. Créez un projet sous Android Studio Avec les fichiers suivants :
activity_main.xml
android:text=”Tapez l'URL que vous souhaitez ouvrir”
android:id=”@+id/textView”
<Button
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.
id.buttonOuvrir);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView url = (TextView) findViewById(R.
});
}
}
strings.xml
<resources>
<string name=”app_name”>Implicit Web Intent</string>
<string name=”action_settings”>Settings</string>
</resources>
2. Essayer de donner l’Uri tel:+221123456 sur le TextView de l’URL 3. Que pouvez-vous conclure du résultat ?
Exercice 2 : Passage d’une fenêtre à l’autre avec les Intents Explicit
Le but de cet exercice est de créer une application qui contient 2 activités. La première activité est sensé lancer la seconde activités.
Figure 34 Interface de l’application à developpemer
Créez un projet à partir du code suivant :
<Button
android:text=”Bienvenue en Afrique”
android:id=”@+id/textViewWelcome”
android:layout_alignParentTop=”true”
android:layout_alignParentStart=”true” />
</RelativeLayout>
AndroidManifest.xml
<?xml version=”1.0” encoding=”utf-8”?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
<string name=”app_name”>Demo Intent</string>
<string name=”title_activity_main”>MainActivity</string>
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.buttonSuivant);
EditText nom = (EditText) findViewById(R.id.editTextNom);
EditText email = (EditText) findViewById(R.
id.editTextEmail);
final Intent i = new Intent(MainActivity.this, WelcomeAfricaActivity.
class);
i.putExtra(“Nom”, nom.getText().toString());
i.putExtra(“Email”, email.getText().toString());
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { startActivity(i);
} });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true;
public class WelcomeAfricaActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_africa);
Intent i = this.getIntent();
if(i.getExtras() != null)
{
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_welcome_africa, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true;
}
return super.onOptionsItemSelected(item);
} }
Exercice 3 : Création de Toast
Créez un projet Android pour tester les Toast à partir des fichiers suivants :
activity_main.xml
android:text=”Afficher un Toast”
android:id=”@+id/buttonToast”
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.buttonToast);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),”Bonjour le monde Toast !”, Toast.LENGTH_LONG).show();
} });
} }
strings.xml
<resources>
<string name=”app_name”>Demo Toast</string>
<string name=”action_settings”>Settings</string>
</resources>
Exercice 4 Création d’une application appelable par des Intent Implicite
Le but de cet exercice est de créer une application qui se présente comme un Navigateur en utilisant les filtres des Intents.
Figure 35 Application qui se présente sur la liste des propositions d’Android lorsqu’un navigateur est demandé en Intent Implicite
Le code est le suivant :
activity_main.xml
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/
android”
xmlns:tools=”http://schemas.android.com/tools” android:layout_
width=”match_parent”
android:layout_height=”match_parent” android:paddingLeft=”@dimen/
activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:paddingBottom=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity”>
<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Chargez UVA.ORG”
android:id=”@+id/button”
android:layout_centerVertical=”true”
android:layout_centerHorizontal=”true” />
</RelativeLayout>
Activity_navigateur_africa.xml
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/
android”
xmlns:tools=”http://schemas.android.com/tools” android:layout_
width=”match_parent”
android:layout_height=”match_parent” android:paddingLeft=”@dimen/
activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:paddingBottom=”@dimen/activity_vertical_margin”
tools:context=”org.uva.navigateurafricain.NavigateurAfrica”>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”URL”
android:id=”@+id/textViewURL”
android:layout_alignParentTop=”true”
android:layout_alignParentStart=”true” />
<WebView
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:id=”@+id/webView”
android:layout_below=”@+id/textViewURL”
android:layout_alignParentStart=”true” />
</RelativeLayout>
AndroidManifest.xml
<?xml version=”1.0” encoding=”utf-8”?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”org.uva.navigateurafricain” >
<uses-permission android:name=”android.permission.INTERNET” />
<application
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
<action android:name=”android.intent.action.VIEW” />
<category android:name=”android.intent.category.DEFAULT” />
<data android:scheme=”http” />
</intent-filter>
</activity>
</application>
</manifest>
MainAcitivity.java
package org.uva.navigateurafricain;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import java.net.URL;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(“http://www.uva.org”));
startActivity(intent);
} });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true;
public class NavigateurAfrica extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigateur_africa);
Intent intent = getIntent();
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl(url.toString());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_navigateur_africa, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true;
}
return super.onOptionsItemSelected(item);
} }
Résumé de l’unité
Cette unité a traité la structure de base d’un projet Android avec les principaux fichiers dans Android Studio. Ensuite, les différents widgets sont présentés. Enfin, Les activités et les Intents sont traités pour la programmaiton du comportement de l’application.