Vous êtes sur la page 1sur 18

Cours Dveloppement Mobile et Embarqu

3LT

CHAPITRE 2
NOTIONS DE BASE DE LA
PROGRAMMATION SOUS ANDROID :
ETUDE VIA UNE APPLICATION
M. Slim BEN ABDELBARI, ISITCom
BASIQUE

APPLICATION 1 : LES BASIQUES,


HELLO WORLD

Objectif de lapplication : afficher le trs fameux et


tant difficile message hello world via la
programmation sous ANDROID !!
IDE utilis : ANDROID STUDIO.
Pr requis : Notions intermdiaires en JAVA, XML.

APPLICATION 1 : LES BASIQUES,


HELLO WORLD

Etape 1 : Crer un
nouveau projet sous
AS.

Nom de lapp. sur Play Store

Package final
gnr par
lapplication (cf.
notation JAVA)

APPLICATION 1 : LES BASIQUES,


HELLO WORLD
Version minimale
de lAPI Android
pour faire
fonctionner
lapplication !

Versions API
Android ??

APPLICATION 1 : LES BASIQUES,


HELLO WORLD

API rfrencie
prcdemment !!

APPLICATION 1 : LES BASIQUES,


HELLO WORLD
Volet dajout dune
activit

Une activit ?
On y reviendra
au moment
opportun

APPLICATION 1 : LES BASIQUES,


HELLO WORLD

Nom de lactivit

Q : mais au fait, quelle


est la diffrence entre
Activity et Layout ?

Nom du Layout

APPLICATION 1 : LES BASIQUES,


HELLO WORLD

Pour faire simple..

APPLICATION 1 : LES BASIQUES,


HELLO WORLD
MainActivity . JAVA

Activity_Main . XML

On fait les
prsentations ?

APPLICATION 1 : LES BASIQUES,


HELLO WORLD
package com.example.lt3.hellowoldapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

MainActivity . JAVA

APPLICATION 1 : LES BASIQUES,


HELLO WORLD
package com.example.lt3.hellowoldapp;
Package final regroupant les mthodes et variables de lapplication (cf.
dfinit au dbut au niveau de AS.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
Classes importes automatiquement par AS.
public class MainActivity extends AppCompatActivity
La classe MainActivity est une extension de la classe de base AppCompatActivity
(gestion des ActionBar entre autres) !
N.B : Pour la gestion des activits, on aurait galement pu faire appel la classe
.Activity (issue de import android.app.Activity)

APPLICATION 1 : LES BASIQUES,


HELLO WORLD
setContentView(R.layout.activity_main);
Demande ANDROID
dafficher le layout
dont le nom est donn
dans la liste des
paramtres

R.JAVA est une


classe (ayant des
sous classes
emboites tels que
layout, string, etc)
gnre durant le
processus de
construction
permettant de crer
des rfrences aux
ressources utilises
dans lapplication.

Nom du layout qui


sera affich lors de
la cration de
lactivit.

setContentView(activity_main);

APPLICATION 1 : LES BASIQUES,


HELLO WORLD
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
Spec. Du
android:layout_width="match_parent"
android:layout_height="match_parent"
layout en
android:paddingBottom="@dimen/activity_vertical_margin"
gnral
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.lt3.hellowoldapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>

Activity_Main . XML

Spec. Llment TextView

APPLICATION 1 : LES BASIQUES,


HELLO WORLD

Vue Design , Activity_Main . XML

APPLICATION 1 : LES BASIQUES,


HELLO WORLD
android:text="Hello World!"
Spcifie la proprit
texte de llment
TextView hardcoding

Hardcoding !!

Daccord, maispenser que lapplication


dveloppe sera dploye sous diffrentes
languescomment faire ??

APPLICATION 1 : LES BASIQUES,


HELLO WORLD
<resources>
<string
name="app_name">HelloWoldApp</string>
</resources>

Indique un
fichier
ressources

Les ressources
du fichier
XML sont de
type string

Syntaxe gnrale
<string
name="string_name">string_value</string>

APPLICATION 1 : LES BASIQUES,


HELLO WORLD

Par la suite, on peut remplacer toutes les occurrences de


string_value par
"@string/string_name"
<resources>
<string name="app_name">HelloWoldApp</string>
<string name="hello_world">Hello world!</string>
</resources>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

Pour basculer en Fr, ajouter un nouveau dossier de ressources Values-fr

APPLICATION 1 : LES BASIQUES,


HELLO WORLD

Au revoir HELLO WORLD !!


Que doit-on retenir ?

Vous aimerez peut-être aussi