SlideShare une entreprise Scribd logo
1  sur  31
ANDROID: LES LAYOUTS XML
MOHAMED BOURAOUI
BOURAWI.MOHAMED@GMAIL.COM
LES INTERFACES GRAPHIQUES SIMPLES
Les interfaces graphiques simples
EXEMPLE
ImageButton
TextView
CheckBox
RadioButton
ToggleButton
RatingBar
LES CONTENEURS
 LinearLayout: dispose les éléments de gauche à droite ou du haut vers le
bas
 RelativeLayout: les éléments enfants les uns par rapport aux autres
 TableLayout: disposition matricielle
 FrameLayout: disposition en haut à gauche en empilant les éléments
(utilisés conjointement avec les fragments)
 Webview :affiche le contenu d'une page web
CONTENEURS : ATTRIBUTS PRINCIPAUX
 Orientation
 Sens de placement des vues dans un conteneur
 android:orientation = vertical | horizontal
 Taille
 Surface prise par la vue
 android:layout_width = ??px | fill_parent | wrap_content
 android:layout_height = ??px | fill_parent | wrap_content
 Gravité
 Alignement d'une vue dans son conteneur
 android:layout_gravity = left | center_horizontal | top | bottom | right
Les unités dp et sp sont indépendants de la résolution de
l’écran, Il est recommandé d’utiliser dp pour les objets et sp
pour les polices du texte.
LINEARLAYOUT: EXEMPLE
RELATIVELAYOUT: EXEMPLE
RELATIVELAYOUT: ATTRIBUTS
 Placement par rapport au conteneur
 android:layout_alignParentBottom="b" (où b vaut true ou false)
 android:layout_alignParentLeft="b" (où b vaut true ou false)
 android:layout_alignParentRight="b" (où b vaut true ou false)
 android:layout_alignParentTop="b" (où b vaut true ou false)
 android:layout_centerHorizontal="b" (où b vaut true ou false)
 android:layout_centerInParent="b" (où b vaut true ou false)
 android:layout_centerVertical="b" (où b vaut true ou false)
 Placement par rapport aux autres éléments
 android:layout_above="@+id/ident"/
 android:layout_below="@+id/ident"/
 android:layout_toLeftOf="@+id/ident"/
 android:layout_toRightOf="@+id/ident"/
 android:layout_alignLeft="@+id/ident"/
 android:layout_alignRight="@+id/ident"/
 android:layout_alignTop="@+id/ident"/
 android:layout_alignBottom="@+id/ident"/
TABLELAYOUT : DISPOSITION MATRICIELLE
<TableLayout>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button" />
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
/>
</TableRow>
</TableLayout>
SCROLLVIEW
 Ce layout permet de défiler le contenu par une barre de défilement vertical si le
contenu dépasse sa longueur. Pour appliquer ce layout, on met à son intérieur un
autre type de layout (LinearLayout par exemple) dans laquelle on met un
contenu.
<?xml version ="1.0" ?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lineSpacingMultiplier="2"
android:text="@string/texteAndroid"
android:textColor="@android:color/black" />
</LinearLayout>
</ScrollView>
HORIZONTALSCROLLVIEW
 Ce layout nous permettra de défiler le contenu mais cette fois-ci d’une
manière horizontale. Exemple:
HORIZONTALSCROLLVIEW (2)
<?xml version ="1.0"?>
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/white">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="72dp"
android:layout_weight="0.07"
android:src="@drawable/contact" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="72dp"
android:layout_weight="0.07"
android:src="@drawable/support" />
</LinearLayout>
</HorizontalScrollView>
LES LABELS DE TEXTE
<TextView
android:id="@+id/le_texte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_gravity="center"/>
LinearLayout gabarit = new LinearLayout(this);
gabarit.setGravity(Gravity.CENTER); // centrer les éléments graphiques
gabarit.setOrientation(LinearLayout.VERTICAL); // empiler vers le bas !
TextView texte = new TextView(this);
texte.setText(« Créer l’interface dans le code");
gabarit.addView(texte);
setContentView(gabarit);
Déclaration dans le layout
Créer « dynamiquement »
LES ZONES DE TEXTE
<EditText android:text=""
android:id="@+id/EditText01"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</EditText>
EditText edit = new EditText(this);
edit.setText("Edit me");
gabarit.addView(edit);
Déclaration dans le layout
Créer « dynamiquement »
LES ZONES DE TEXTE : ÉVÈNEMENTS
Intercepter les évènements de saisie
edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// do something here
}
);
LES IMAGES
<ImageView
android:id="@+id/logoISET"
android:src="@drawable/iset"
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
></ImageView>
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.iset);
gabarit.addView(image);
Déclaration dans le layout
Créer « dynamiquement »
LES BOUTONS
<Button android:text="Go !"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
Button b = (Button)findViewById(R.id.Button01);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), « C’est un
clic!",Toast.LENGTH_LONG).show();
}
});
Déclaration dans le layout
Intercepter l’évènement de clic
GESTION DES ÉVÈNEMENTS : LES « LISTENERS »
 La gestion des évènements est assurée par des interfaces Java, dont le scénario
est:
1. Associer à l’objet sur lequel on
veut gérer un ou plusieurs
évènements l’interface adéquate,
et on redéfinit les méthodes de
cette dernière avec le code Java
qui sera exécuté le moment ou
l’évènement se produit.
2. l’interface reste à l’écoute des
éventuels évènements.
3. un certain évènement compatible
se produit.
4. L’interface exécutera alors le code
contenu dans la méthode
adéquate.
RÉCAPITULATIF DES ÉVÈNEMENTS
 Evénements généraux:
Tous les éléments d’interface (conteneurs et widgets)possèdent les
méthodes suivantes :
RÉCAPITULATIF DES ÉVÈNEMENTS
 Evénements spécifiques:
Pour EditText et TextView
LES LISTES
 L’importance d’une liste vient du besoin de dérouler un nombre important
d’élèments. Pour ce faire, il suffit de créer un layout linéaire et d’y
implanter une ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout …>
<ListView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_weight="1" />
</LinearLayout>
LES LISTES (2)
 Etant donné qu'une liste peut contenir des éléments graphiques divers et
variés, les éléments de la liste doivent être insérés dans un ListAdapter.
 Exemple : Une liste de chaines de caractères:
1. Création du layout d’un élément de la liste montexte.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:text="TextView"
android:id="@+id/montexte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
</TextView>
LES LISTES (2)
2. Layout de la liste
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/maliste">
</ListView>
</LinearLayout>
LES LISTES (4)
ListView list = (ListView)findViewById(R.id.maliste);
3. Instancier la ListeView
4. Ajouter dynamiquement un ArrayAdapter à la ListeView list
ArrayAdapter<String> tableau = new ArrayAdapter<String>(
list.getContext(), R.layout.maligne);
for (int i=0; i<40; i++) {
tableau.add("Ligne n° " + i);
}
list.setAdapter(tableau);
LES LISTES (4)
 Résultat:
LES LISTES : EXEMPLE PLUS COMPLEXE
 Lorsque les listes contiennent un layout plus complexe qu'un texte, il faut
utiliser un autre constructeur de ArrayAdapter:
ArrayAdapter (Context context, int resource, int textViewResourceId)
 Ressource : L’identificateur du layout à appliquer à chaque ligne.
 textViewRessourceId: L’identificateur de la zone de texte inclue dans ce layout
LES LISTES : EXEMPLE PLUS COMPLEXE (1)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout…>
<TextView …>
</TextView>
<LinearLayout
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:gravity="end"
android:layout_gravity="center">
<ImageView android:id="@+id/monImage"
android:layout_height="wrap_content"
android:contentDescription="demo"
android:layout_width="wrap_content"
android:src="@android:drawable/ic_menu_compass">
</ImageView>
</LinearLayout>
</LinearLayout>
 1. Création du layout d’un
élément de la liste
montexte.xml
LES LISTES : EXEMPLE PLUS COMPLEXE (2)
 2. Layout de la liste
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:layout_height="fill_parent"
android:id="@+id/maliste"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
LES LISTES : EXEMPLE PLUS COMPLEXE (3)
ListView list = (ListView)findViewById(R.id.maliste);
3. Instancier la ListeView
4. Ajouter dynamiquement un ArrayAdapter à la ListeView list
ArrayAdapter<String> tableau = new ArrayAdapter<String>(
list.getContext(), R.layout.maligne, R.id.monTexte);
for (int i=0; i<40; i++) {
tableau.add("Ligne n° " + i);
}
list.setAdapter(tableau);
LES LISTES : EXEMPLE PLUS COMPLEXE (4)
 Résultat:
INCLURE LES LAYOUTS
 Les interfaces peuvent inclure d’autres interfaces pour favoriser la
réutilisabilité des layouts. Pour l’inclusion, on utilise le mot clé include
 Exemple
<RelativeLayout>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/champ_txt«
android:text="Ecrire ici 30 caractères" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/txt"/>
<include android:id="@+id/include01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
layout="@layout/included"
></include>
</RelativeLayout>
Included.xml
principale.xml principale.xml

Contenu connexe

En vedette

Aula 02 - Android. Intent, Intent Filters
Aula 02 - Android. Intent, Intent FiltersAula 02 - Android. Intent, Intent Filters
Aula 02 - Android. Intent, Intent FiltersArthur Emanuel
 
[Android] Intent and Activity
[Android] Intent and Activity[Android] Intent and Activity
[Android] Intent and ActivityNikmesoft Ltd
 
Android Data Binding in action using MVVM pattern - droidconUK
Android Data Binding in action using MVVM pattern - droidconUKAndroid Data Binding in action using MVVM pattern - droidconUK
Android Data Binding in action using MVVM pattern - droidconUKFabio Collini
 
Android intents, notification and broadcast recievers
Android intents, notification and broadcast recieversAndroid intents, notification and broadcast recievers
Android intents, notification and broadcast recieversUtkarsh Mankad
 
Android notification
Android notificationAndroid notification
Android notificationKrazy Koder
 
Android: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast ReceiversAndroid: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast ReceiversCodeAndroid
 
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAndroid User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAhsanul Karim
 
School attendance management system (sams)
School attendance management system (sams)School attendance management system (sams)
School attendance management system (sams)gajananp2008
 
15.project attendence managemnt system
15.project attendence managemnt system15.project attendence managemnt system
15.project attendence managemnt systemHaseeb Nasir
 
Android Development: The Basics
Android Development: The BasicsAndroid Development: The Basics
Android Development: The BasicsMike Desjardins
 
Smart Attendance Management System Using Android WIFI Technology
Smart Attendance Management System Using Android WIFI TechnologySmart Attendance Management System Using Android WIFI Technology
Smart Attendance Management System Using Android WIFI TechnologySukanta Biswas
 
Basic of Android App Development
Basic of Android App DevelopmentBasic of Android App Development
Basic of Android App DevelopmentAbhijeet Gupta
 
Data Binding in Action using MVVM pattern
Data Binding in Action using MVVM patternData Binding in Action using MVVM pattern
Data Binding in Action using MVVM patternFabio Collini
 

En vedette (16)

Aula 02 - Android. Intent, Intent Filters
Aula 02 - Android. Intent, Intent FiltersAula 02 - Android. Intent, Intent Filters
Aula 02 - Android. Intent, Intent Filters
 
[Android] Intent and Activity
[Android] Intent and Activity[Android] Intent and Activity
[Android] Intent and Activity
 
Android - Android Intent Types
Android - Android Intent TypesAndroid - Android Intent Types
Android - Android Intent Types
 
Android Data Binding in action using MVVM pattern - droidconUK
Android Data Binding in action using MVVM pattern - droidconUKAndroid Data Binding in action using MVVM pattern - droidconUK
Android Data Binding in action using MVVM pattern - droidconUK
 
Android intents, notification and broadcast recievers
Android intents, notification and broadcast recieversAndroid intents, notification and broadcast recievers
Android intents, notification and broadcast recievers
 
Android notification
Android notificationAndroid notification
Android notification
 
Android: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast ReceiversAndroid: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast Receivers
 
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAndroid User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
 
School attendance management system (sams)
School attendance management system (sams)School attendance management system (sams)
School attendance management system (sams)
 
15.project attendence managemnt system
15.project attendence managemnt system15.project attendence managemnt system
15.project attendence managemnt system
 
Android Development: The Basics
Android Development: The BasicsAndroid Development: The Basics
Android Development: The Basics
 
Student Attendance System
Student Attendance SystemStudent Attendance System
Student Attendance System
 
Smart Attendance Management System Using Android WIFI Technology
Smart Attendance Management System Using Android WIFI TechnologySmart Attendance Management System Using Android WIFI Technology
Smart Attendance Management System Using Android WIFI Technology
 
Android Basic Components
Android Basic ComponentsAndroid Basic Components
Android Basic Components
 
Basic of Android App Development
Basic of Android App DevelopmentBasic of Android App Development
Basic of Android App Development
 
Data Binding in Action using MVVM pattern
Data Binding in Action using MVVM patternData Binding in Action using MVVM pattern
Data Binding in Action using MVVM pattern
 

Similaire à Les Layouts XML

Les interface graphiques sous android
Les interface graphiques sous androidLes interface graphiques sous android
Les interface graphiques sous androidHoussem Lahiani
 
Interace Utilisateur.pdf
Interace Utilisateur.pdfInterace Utilisateur.pdf
Interace Utilisateur.pdfRihabBENLAMINE
 
interface graphique mobile.pdf
interface graphique mobile.pdfinterface graphique mobile.pdf
interface graphique mobile.pdfYasmineChihab1
 
Création d’applications et découverte d’Android
Création d’applications et découverte d’AndroidCréation d’applications et découverte d’Android
Création d’applications et découverte d’AndroidENSAM Casablanca
 
Mise en place de l'ActionBarCompat dans vos projets Android.
Mise en place de l'ActionBarCompat dans vos projets Android.Mise en place de l'ActionBarCompat dans vos projets Android.
Mise en place de l'ActionBarCompat dans vos projets Android.Mathias Seguy
 
Android-Tp2: liste et adaptateurs
Android-Tp2: liste et adaptateursAndroid-Tp2: liste et adaptateurs
Android-Tp2: liste et adaptateursLilia Sfaxi
 
chapitre-7-listviews.pdf
chapitre-7-listviews.pdfchapitre-7-listviews.pdf
chapitre-7-listviews.pdfolfaharrabi2
 
ASP.NET Futures
ASP.NET FuturesASP.NET Futures
ASP.NET Futuresloicbar
 
Asp.Net Futures
Asp.Net FuturesAsp.Net Futures
Asp.Net Futuresloicbar
 
Linq et Entity framework
Linq et Entity frameworkLinq et Entity framework
Linq et Entity frameworkDNG Consulting
 
Présentation de Neten'Board par NETENCY
Présentation de Neten'Board par NETENCYPrésentation de Neten'Board par NETENCY
Présentation de Neten'Board par NETENCYJ-Marc Beaudoin
 

Similaire à Les Layouts XML (20)

Gestion des ui
Gestion des uiGestion des ui
Gestion des ui
 
TP_2.pdf
TP_2.pdfTP_2.pdf
TP_2.pdf
 
Les interface graphiques sous android
Les interface graphiques sous androidLes interface graphiques sous android
Les interface graphiques sous android
 
react-fr.pdf
react-fr.pdfreact-fr.pdf
react-fr.pdf
 
2-android.pdf
2-android.pdf2-android.pdf
2-android.pdf
 
Interace Utilisateur.pdf
Interace Utilisateur.pdfInterace Utilisateur.pdf
Interace Utilisateur.pdf
 
interface graphique mobile.pdf
interface graphique mobile.pdfinterface graphique mobile.pdf
interface graphique mobile.pdf
 
Création d’applications et découverte d’Android
Création d’applications et découverte d’AndroidCréation d’applications et découverte d’Android
Création d’applications et découverte d’Android
 
Mise en place de l'ActionBarCompat dans vos projets Android.
Mise en place de l'ActionBarCompat dans vos projets Android.Mise en place de l'ActionBarCompat dans vos projets Android.
Mise en place de l'ActionBarCompat dans vos projets Android.
 
Android2017 cours2
Android2017 cours2Android2017 cours2
Android2017 cours2
 
Présentation WPF
Présentation  WPFPrésentation  WPF
Présentation WPF
 
Android-Tp2: liste et adaptateurs
Android-Tp2: liste et adaptateursAndroid-Tp2: liste et adaptateurs
Android-Tp2: liste et adaptateurs
 
Association 2 0
Association 2 0Association 2 0
Association 2 0
 
chapitre-7-listviews.pdf
chapitre-7-listviews.pdfchapitre-7-listviews.pdf
chapitre-7-listviews.pdf
 
ASP.NET Futures
ASP.NET FuturesASP.NET Futures
ASP.NET Futures
 
Asp.Net Futures
Asp.Net FuturesAsp.Net Futures
Asp.Net Futures
 
test
testtest
test
 
Tutorial android
Tutorial androidTutorial android
Tutorial android
 
Linq et Entity framework
Linq et Entity frameworkLinq et Entity framework
Linq et Entity framework
 
Présentation de Neten'Board par NETENCY
Présentation de Neten'Board par NETENCYPrésentation de Neten'Board par NETENCY
Présentation de Neten'Board par NETENCY
 

Les Layouts XML

  • 1. ANDROID: LES LAYOUTS XML MOHAMED BOURAOUI BOURAWI.MOHAMED@GMAIL.COM
  • 2. LES INTERFACES GRAPHIQUES SIMPLES Les interfaces graphiques simples
  • 4. LES CONTENEURS  LinearLayout: dispose les éléments de gauche à droite ou du haut vers le bas  RelativeLayout: les éléments enfants les uns par rapport aux autres  TableLayout: disposition matricielle  FrameLayout: disposition en haut à gauche en empilant les éléments (utilisés conjointement avec les fragments)  Webview :affiche le contenu d'une page web
  • 5. CONTENEURS : ATTRIBUTS PRINCIPAUX  Orientation  Sens de placement des vues dans un conteneur  android:orientation = vertical | horizontal  Taille  Surface prise par la vue  android:layout_width = ??px | fill_parent | wrap_content  android:layout_height = ??px | fill_parent | wrap_content  Gravité  Alignement d'une vue dans son conteneur  android:layout_gravity = left | center_horizontal | top | bottom | right Les unités dp et sp sont indépendants de la résolution de l’écran, Il est recommandé d’utiliser dp pour les objets et sp pour les polices du texte.
  • 8. RELATIVELAYOUT: ATTRIBUTS  Placement par rapport au conteneur  android:layout_alignParentBottom="b" (où b vaut true ou false)  android:layout_alignParentLeft="b" (où b vaut true ou false)  android:layout_alignParentRight="b" (où b vaut true ou false)  android:layout_alignParentTop="b" (où b vaut true ou false)  android:layout_centerHorizontal="b" (où b vaut true ou false)  android:layout_centerInParent="b" (où b vaut true ou false)  android:layout_centerVertical="b" (où b vaut true ou false)  Placement par rapport aux autres éléments  android:layout_above="@+id/ident"/  android:layout_below="@+id/ident"/  android:layout_toLeftOf="@+id/ident"/  android:layout_toRightOf="@+id/ident"/  android:layout_alignLeft="@+id/ident"/  android:layout_alignRight="@+id/ident"/  android:layout_alignTop="@+id/ident"/  android:layout_alignBottom="@+id/ident"/
  • 9. TABLELAYOUT : DISPOSITION MATRICIELLE <TableLayout> <TableRow> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button" /> </TableRow> <TableRow> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button2" /> </TableRow> </TableLayout>
  • 10. SCROLLVIEW  Ce layout permet de défiler le contenu par une barre de défilement vertical si le contenu dépasse sa longueur. Pour appliquer ce layout, on met à son intérieur un autre type de layout (LinearLayout par exemple) dans laquelle on met un contenu. <?xml version ="1.0" ?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/white"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:lineSpacingMultiplier="2" android:text="@string/texteAndroid" android:textColor="@android:color/black" /> </LinearLayout> </ScrollView>
  • 11. HORIZONTALSCROLLVIEW  Ce layout nous permettra de défiler le contenu mais cette fois-ci d’une manière horizontale. Exemple:
  • 12. HORIZONTALSCROLLVIEW (2) <?xml version ="1.0"?> <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:color/white"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:layout_width="wrap_content" android:layout_height="72dp" android:layout_weight="0.07" android:src="@drawable/contact" /> <ImageView android:layout_width="wrap_content" android:layout_height="72dp" android:layout_weight="0.07" android:src="@drawable/support" /> </LinearLayout> </HorizontalScrollView>
  • 13. LES LABELS DE TEXTE <TextView android:id="@+id/le_texte" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello" android:layout_gravity="center"/> LinearLayout gabarit = new LinearLayout(this); gabarit.setGravity(Gravity.CENTER); // centrer les éléments graphiques gabarit.setOrientation(LinearLayout.VERTICAL); // empiler vers le bas ! TextView texte = new TextView(this); texte.setText(« Créer l’interface dans le code"); gabarit.addView(texte); setContentView(gabarit); Déclaration dans le layout Créer « dynamiquement »
  • 14. LES ZONES DE TEXTE <EditText android:text="" android:id="@+id/EditText01" android:layout_width="match_parent" android:layout_height="wrap_content"> </EditText> EditText edit = new EditText(this); edit.setText("Edit me"); gabarit.addView(edit); Déclaration dans le layout Créer « dynamiquement »
  • 15. LES ZONES DE TEXTE : ÉVÈNEMENTS Intercepter les évènements de saisie edit.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // do something here } );
  • 16. LES IMAGES <ImageView android:id="@+id/logoISET" android:src="@drawable/iset" android:layout_width="100px" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" ></ImageView> ImageView image = new ImageView(this); image.setImageResource(R.drawable.iset); gabarit.addView(image); Déclaration dans le layout Créer « dynamiquement »
  • 17. LES BOUTONS <Button android:text="Go !" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> Button b = (Button)findViewById(R.id.Button01); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(v.getContext(), « C’est un clic!",Toast.LENGTH_LONG).show(); } }); Déclaration dans le layout Intercepter l’évènement de clic
  • 18. GESTION DES ÉVÈNEMENTS : LES « LISTENERS »  La gestion des évènements est assurée par des interfaces Java, dont le scénario est: 1. Associer à l’objet sur lequel on veut gérer un ou plusieurs évènements l’interface adéquate, et on redéfinit les méthodes de cette dernière avec le code Java qui sera exécuté le moment ou l’évènement se produit. 2. l’interface reste à l’écoute des éventuels évènements. 3. un certain évènement compatible se produit. 4. L’interface exécutera alors le code contenu dans la méthode adéquate.
  • 19. RÉCAPITULATIF DES ÉVÈNEMENTS  Evénements généraux: Tous les éléments d’interface (conteneurs et widgets)possèdent les méthodes suivantes :
  • 20. RÉCAPITULATIF DES ÉVÈNEMENTS  Evénements spécifiques: Pour EditText et TextView
  • 21. LES LISTES  L’importance d’une liste vient du besoin de dérouler un nombre important d’élèments. Pour ce faire, il suffit de créer un layout linéaire et d’y implanter une ListView: <?xml version="1.0" encoding="utf-8"?> <LinearLayout …> <ListView android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/listView" android:layout_weight="1" /> </LinearLayout>
  • 22. LES LISTES (2)  Etant donné qu'une liste peut contenir des éléments graphiques divers et variés, les éléments de la liste doivent être insérés dans un ListAdapter.  Exemple : Une liste de chaines de caractères: 1. Création du layout d’un élément de la liste montexte.xml <?xml version="1.0" encoding="utf-8"?> <TextView android:text="TextView" android:id="@+id/montexte" android:layout_width="wrap_content" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android"> </TextView>
  • 23. LES LISTES (2) 2. Layout de la liste <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/maliste"> </ListView> </LinearLayout>
  • 24. LES LISTES (4) ListView list = (ListView)findViewById(R.id.maliste); 3. Instancier la ListeView 4. Ajouter dynamiquement un ArrayAdapter à la ListeView list ArrayAdapter<String> tableau = new ArrayAdapter<String>( list.getContext(), R.layout.maligne); for (int i=0; i<40; i++) { tableau.add("Ligne n° " + i); } list.setAdapter(tableau);
  • 25. LES LISTES (4)  Résultat:
  • 26. LES LISTES : EXEMPLE PLUS COMPLEXE  Lorsque les listes contiennent un layout plus complexe qu'un texte, il faut utiliser un autre constructeur de ArrayAdapter: ArrayAdapter (Context context, int resource, int textViewResourceId)  Ressource : L’identificateur du layout à appliquer à chaque ligne.  textViewRessourceId: L’identificateur de la zone de texte inclue dans ce layout
  • 27. LES LISTES : EXEMPLE PLUS COMPLEXE (1) <?xml version="1.0" encoding="utf-8"?> <LinearLayout…> <TextView …> </TextView> <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="fill_parent" android:gravity="end" android:layout_gravity="center"> <ImageView android:id="@+id/monImage" android:layout_height="wrap_content" android:contentDescription="demo" android:layout_width="wrap_content" android:src="@android:drawable/ic_menu_compass"> </ImageView> </LinearLayout> </LinearLayout>  1. Création du layout d’un élément de la liste montexte.xml
  • 28. LES LISTES : EXEMPLE PLUS COMPLEXE (2)  2. Layout de la liste <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:layout_height="fill_parent" android:id="@+id/maliste" android:layout_width="fill_parent"> </ListView> </LinearLayout>
  • 29. LES LISTES : EXEMPLE PLUS COMPLEXE (3) ListView list = (ListView)findViewById(R.id.maliste); 3. Instancier la ListeView 4. Ajouter dynamiquement un ArrayAdapter à la ListeView list ArrayAdapter<String> tableau = new ArrayAdapter<String>( list.getContext(), R.layout.maligne, R.id.monTexte); for (int i=0; i<40; i++) { tableau.add("Ligne n° " + i); } list.setAdapter(tableau);
  • 30. LES LISTES : EXEMPLE PLUS COMPLEXE (4)  Résultat:
  • 31. INCLURE LES LAYOUTS  Les interfaces peuvent inclure d’autres interfaces pour favoriser la réutilisabilité des layouts. Pour l’inclusion, on utilise le mot clé include  Exemple <RelativeLayout> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/champ_txt« android:text="Ecrire ici 30 caractères" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/txt"/> <include android:id="@+id/include01" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" layout="@layout/included" ></include> </RelativeLayout> Included.xml principale.xml principale.xml