<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>

<channel>
	<title>AndroidDevBlog</title>
	<atom:link href="http://android.cyrilmottier.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://android.cyrilmottier.com</link>
	<description>Blog francophone sur le développement Android</description>
	<pubDate>Sun, 05 Sep 2010 16:51:16 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>New GreenDroid release: library project &amp; ActionBarItem</title>
		<link>http://android.cyrilmottier.com/?p=299</link>
		<comments>http://android.cyrilmottier.com/?p=299#comments</comments>
		<pubDate>Sun, 05 Sep 2010 16:51:16 +0000</pubDate>
		<dc:creator>Cyril</dc:creator>
		
		<category><![CDATA[Non classé]]></category>

		<guid isPermaLink="false">http://android.cyrilmottier.com/?p=299</guid>
		<description><![CDATA[I&#8217;ve recently updated the GreenDroid library (available on GitHub). Here is a list of the main changes:

Improvements to the ActionBar
New &#8220;applying&#8221; system
Bugs fixed

Improvements to the ActionBar
With the first release of GD[Tab&#124;List]Activity it wasn&#8217;t possible to create &#8220;non regular&#8221; items. This release introduces ActionBarItem. An ActionBarItem is an object representing a single action button in the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently updated the GreenDroid library (available on <a href="http://github.com/cyrilmottier/GreenDroid">GitHub</a>). Here is a list of the main changes:</p>
<ul>
<li>Improvements to the <code>ActionBar</code></li>
<li>New &#8220;applying&#8221; system</li>
<li>Bugs fixed</li>
</ul>
<h2>Improvements to the ActionBar</h2>
<p>With the first release of <a href="http://android.cyrilmottier.com/?p=274">GD[Tab|List]Activity</a> it wasn&#8217;t possible to create &#8220;non regular&#8221; items. This release introduces <code>ActionBarItem</code>. An <code>ActionBarItem</code> is an object representing a single action button in the <code>ActionBar</code>. It may act differently depending on its purpose. For instance, a <code>LoaderActionBarItem</code> switches from a regular button to a circular <code>ProgressBar</code> once pressed. A <code>NormalActionBarItem</code> displays a simple <code>Drawable</code>.</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p299code3'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p2993"><td class="code" id="p299code3"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
 * This will add a &quot;refresh&quot; button that switches from button with two rounded arrows
 * to an indeterminate ProgressBar
 */</span>
addActionBarItem<span style="color: #009900;">&#40;</span>Type.<span style="color: #006633;">Refresh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>To prevent developers from designing icons, GreenDroid also includes several base items &#8230; (Refresh, Export, Locate, etc.). Do not hesitate to use it as it makes <code>ActionBar</code> development easier and increases consistency through Android applications.</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p299code4'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p2994"><td class="code" id="p299code4"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
 * Here is a sample that show how to create a NormalActionBarItem with custom Drawable
 * and content description.
 */</span>
ActionBarItem item <span style="color: #339933;">=</span> getActionBar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">newActionBarItem</span><span style="color: #009900;">&#40;</span>NormalActionBarItem.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
item.<span style="color: #006633;">setDrawable</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">ic_title_export</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setContentDescription</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">string</span>.<span style="color: #006633;">gd_export</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
addActionBarItem<span style="color: #009900;">&#40;</span>item<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><em><strong>Note:</strong> The <code>ActionBarItem</code> icons I&#8217;ve included to GreenDroid are those that are, to my mind, essential to Android applications. If you think some other icons are essential, fill free to contribute. Just make sure they are in accordance with Android guidelines (shape, colors, size, etc.).</em></p>
<h2>New &#8220;applying&#8221; system</h2>
<p>When I created GreenDroid it wasn&#8217;t possible to use projects as &#8220;libraries&#8221;. Fortunately, the latest release of ADT includes a brand new amazing feature: library projects. A complete description is given on the <a href="http://developer.android.com/intl/fr/guide/developing/eclipse-adt.html#libraryProject">Android Developers Website</a> so I won&#8217;t spend time describing it here.</p>
<p>Thanks to that feature, I&#8217;ve removed the previous Python script that were used to copy files from the GreenDroid library to your own projects. To apply GreenDroid to your projects, simply follow the process detailed <a href="http://developer.android.com/intl/fr/guide/developing/eclipse-adt.html#librarySetup">here</a>. The main advantage of this modification is GreenDroid is now more user-friendly and way easier to use. It is also fully compatible with Windows, Mac OS X or Linux.</p>
<p>I hope you&#8217;ll have a lot of fun with this new GreenDroid release. As usual, feel free to leave a comment here if you have a GreenDroid-remated question or if you simply want to thank me! Happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://android.cyrilmottier.com/?feed=rss2&amp;p=299</wfw:commentRss>
		</item>
		<item>
		<title>Sans transition : les news !!!</title>
		<link>http://android.cyrilmottier.com/?p=295</link>
		<comments>http://android.cyrilmottier.com/?p=295#comments</comments>
		<pubDate>Thu, 26 Aug 2010 20:42:11 +0000</pubDate>
		<dc:creator>Cyril</dc:creator>
		
		<category><![CDATA[Non classé]]></category>

		<guid isPermaLink="false">http://android.cyrilmottier.com/?p=295</guid>
		<description><![CDATA[Tous les développeurs friands de technique seront déçus par ce post. En effet, cette fois ci je souhaitais simplement parler de tout et de rien (en rapport avec Android bien sûr). N&#8217;allez, tout de même pas croire que ce post est sans intérêt &#8230; dites vous plutôt que c&#8217;est un état de l&#8217;art de mes [...]]]></description>
			<content:encoded><![CDATA[<p>Tous les développeurs friands de technique seront déçus par ce post. En effet, cette fois ci je souhaitais simplement parler de tout et de rien (en rapport avec Android bien sûr). N&#8217;allez, tout de même pas croire que ce post est sans intérêt &#8230; dites vous plutôt que c&#8217;est un état de l&#8217;art de mes différentes &#8220;actions&#8221; récentes sur Android (et également une excuse pour tous ceux qui pensent que je ne suis plus très actif en ce moment ^^)</p>
<h2>MetroMap</h2>
<p>Certains l&#8217;auront probablement remarqué &#8230; MetroMap n&#8217;est plus disponible sur l&#8217;Android Market ou même l&#8217;Appslib. Cette absence est <strike>entièrement</strike> voulue et est la conséquence directe d&#8217;une mise en demeure que j&#8217;ai reçue il y a maintenant environ 2 semaines.</p>
<p>Reçue via courriel et courrier postal recommandé, cette mise en demeure, envoyée par la RATP elle-même, me donnait deux semaines pour retirer mon application des différents <em>markets</em> Android. La raison de cette demande était simple : la RATP interdit totalement l&#8217;utilisation de leurs cartes, logos, etc. sans autorisation préalable. Dans mon cas, le problème venait de l&#8217;utilisation inautorisée de la carte du métro parisien.</p>
<p>Un peu enragé par la large base d&#8217;utilisateurs ainsi que par le temps passé à réaliser cette application, j&#8217;ai décidé de demander une autorisation qu&#8217;on m&#8217;a gentiment refusé en affirmant qu&#8217;aucune autorisation d&#8217;utilisation de fond de carte ou de logo n&#8217;était donnée pour les applications sur terminaux mobiles. J&#8217;ai même essayé de me battre <strike>jusqu&#8217;à la mort</strike> pour ma cause en affirmant que l&#8217;application MetroMap s&#8217;incrivait parfaitement en complément des applications Android RATP (ce que je pense réellement). La cause s&#8217;est malheureusement avéré veine &#8230;</p>
<p>Aujourd&#8217;hui je suis donc face à diverses options :</p>
<ul>
<li>Laisser tomber purement et simplement (et donc dire adieu à des heures de travail et m&#8217;excuser auprès d&#8217;utilisateurs fidèles) : ça n&#8217;est pas du tout mon genre !</li>
<li>Mettre le code source à disposition de tous pour que chacun puisse utiliser/recompiler MetroMap Paris comme il le souhaite : je ne trouve malheureusement pas ça très <em>user-friendly</em> (surtout pour Mme Michu qui a déjà du mal à utiliser l&#8217;application &#8220;Android Market&#8221;) et donc absolument pas viable.</li>
<li>Vendre le code au plus offrant : N&#8217;ayant toujours pas trouvé d&#8217;application &#8220;carte de métro&#8221; aussi fluide et facile d&#8217;utilisation &#8230; pourquoi pas ^^</li>
<li>Rebondir en utilisant un autre fond de carte du métro parisien : Cela me semble la piste la plus logique &#8230; il me faut simplement du temps pour reprendre le code et intégrer une carte dont la license m&#8217;autorise son utilisation. Cela peut, tout de même, engendrer certains problèmes. En effet, actuellement les possesseurs de MetroMap disposent encore et toujours du fond de carte RATP (je n&#8217;ai pas le pouvoir de forcer la suppression des applications déjà installées). Forcer une mise à jour impliquera un changement forcé du fond de carte et donc une perte probable d&#8217;utilisateurs qui se sentiront &#8220;brusqué&#8221; par ce changement. Pour contrer ce genre de problématique je pense qu&#8217;il n&#8217;y a qu&#8217;une seule et unique possibilité : améliorer MetroMap Paris (fond de carte sur la SD-card, rapatriement des fonds de carte à la première utilisation, amélioration des performances, améliorations de la qualité des images, etc.).</li>
</ul>
<h2>GreenDroid</h2>
<p>GreenDroid est un projet que j&#8217;ai débuté il y a maintenant 2 ou 3 mois. C&#8217;est une librairie dont la vocation principale est de simplifier le développement sur Android tout en uniformisant les interfaces graphiques (à mon goût pas assez consistantes et ergonomiques actuellement sur Android). Cette librairie contient ainsi diverses fonctionnalités pour gérer facilement des <code>ListView</code>s, créer des <code>SegmentedBar</code>s ou des <code>ActionBar</code>s.</p>
<p>Assez étonné par l&#8217;intérêt porté par les développeurs (que je remercie) sur GreenDroid (notamment depuis l&#8217;intégration de l&#8217;<code>ActionBar</code>), je continue à travailler sur ce projet qui me tient à coeur. L&#8217;avenir proche de GreenDroid s&#8217;oriente donc vers une modification du système &#8220;d&#8217;application&#8221; de la librairie aux projets l&#8217;utilisant ainsi qu&#8217;un ajout de plusieurs fonctionnalités (<code>ActionBarItem</code> prédéfinis, <code>ActionBarItem</code> gérant l&#8217;état &#8220;loading&#8221;, etc.)&#8217;.</p>
<p>Il est clair que ce genre de projet prend énormément de temps mais c&#8217;est une expérience vraiment enrichissante. GreenDroid est né d&#8217;une idée simple et évolue au grès de son utilisation dans divers projets. Si vous utilisez GreenDroid, n&#8217;hésitez donc pas à remonter vos différentes impressions ou ressentiments sur la philosophie sous-jacente, la facilité d&#8217;utilisation, etc. Si vous ne l&#8217;utilisez pas encore &#8230; je ne pourrais que vous conseiller de sauter le pas !</p>
<h2>Après un blog &#8230; un livre !?!</h2>
<p>Je suis resté extrêmement discret sur le point depuis le début et je pense qu&#8217;il devient de plus en plus acceptable de traiter de ce sujet maintenant. Lorsque j&#8217;ai créé ce blog, mon objectif était principalement de partager sur un système encore méconnu. Aujourd&#8217;hui Android est devenu un acteur incontournable du monde mobile et il est encore amené à évoluer énormément.</p>
<p>C&#8217;est donc face à cette certitude que j&#8217;ai décidé de pousser l&#8217;expérience du blog un peu plus loin &#8230; J&#8217;ai le plaisir de vous annoncer que j&#8217;ai entrepris de <strong>rédiger un livre sur le développement sur Android</strong>. Ce livre, co-écrit avec Ludovic Perrier (co-fondateur de la société Diotasoft et également co-vainqueur du concours SFR Jeunes Talents Développeur grâce à l&#8217;application Didunozat) sera disponible en français et, je l&#8217;espère, dans le courant d&#8217;octobre.</p>
<p>L&#8217;ouvrage abordera le développement sur la plateforme Android en suivant une démarche très pédagogique (truffées d&#8217;exemples réutilisables) et complète. Destiné aussi bien aux débutants (explication des concepts élémentaires) qu&#8217;aux expérimentés (optimisations diverses), le livre traite de sujets divers et inédits.</p>
<p>Je ne vais pas trop m&#8217;étendre sur ce sujet car je pense que la meilleure des options qui s&#8217;offrent à vous sera de télécharger la version électronique du livre ou tout simplement d&#8217;acheter la version papier. C&#8217;est aussi l&#8217;occasion pour vous de me remercier pour ce blog ou tout simplement de mon investissement vis à vis d&#8217;Android et de la communauté !. Je donnerais bien sûr plus d&#8217;infos sur ce super ouvrage dans les mois prochains.</p>
<p>Pour finir, j&#8217;aimerai profiter de ce post un peu particulier pour remercier tous les lecteurs de ce blog. En effet, écrire est un plaisir mais se savoir lu et encore plus plaisant et entrainant ! Merci à vous tous. La communauté Android et le système Android tout entier repose sur vos épaules et sur les applications que vous développez. Depuis mes débuts sur Android, j&#8217;ai eu l&#8217;occasion de voir des choses vraiment innovantes et j&#8217;espère sincèrement que ce n&#8217;était que le début !!!</p>
]]></content:encoded>
			<wfw:commentRss>http://android.cyrilmottier.com/?feed=rss2&amp;p=295</wfw:commentRss>
		</item>
		<item>
		<title>Maîtriser les ressources de type &#8220;string&#8221; et &#8220;plurals&#8221;</title>
		<link>http://android.cyrilmottier.com/?p=288</link>
		<comments>http://android.cyrilmottier.com/?p=288#comments</comments>
		<pubDate>Mon, 02 Aug 2010 06:59:52 +0000</pubDate>
		<dc:creator>Cyril</dc:creator>
		
		<category><![CDATA[Non classé]]></category>

		<guid isPermaLink="false">http://android.cyrilmottier.com/?p=288</guid>
		<description><![CDATA[Voici maintenant un petit moment que je rédige des articles en anglais. Ces derniers traitent d&#8217;un travail sur lequel je passe pas mal de temps et qui, je pense, est nécessaire pour essayer d&#8217;uniformiser les applications Android tout en facilitant la vie du développeur : ce projet c&#8217;est GreenDroid. Cette fois, j&#8217;ai envie de revenir [...]]]></description>
			<content:encoded><![CDATA[<p>Voici maintenant un petit moment que je rédige des articles en anglais. Ces derniers traitent d&#8217;un travail sur lequel je passe pas mal de temps et qui, je pense, est nécessaire pour essayer d&#8217;uniformiser les applications Android tout en facilitant la vie du développeur : ce projet c&#8217;est <a href="http://github.com/cyrilmottier/GreenDroid">GreenDroid</a>. Cette fois, j&#8217;ai envie de revenir aux origines ! C&#8217;est donc avec plaisir que j&#8217;écris ces lignes en français, pour les francophones aussi bien débutants qu&#8217;expérimentés.</p>
<p>Je ne vais rien vous apprendre en affirmant que le développement sous Android repose principalement sur 2 entités distinctes :</p>
<ul>
<li>Le code source (contenu dans le répertoire <code>src</code> sous Eclipse) écrit en Java et intégrant la logique sous-jacente d&#8217;une application</li>
<li>Les ressources (contenues dans le répertoire <code>res</code> dans une arborescence classique) qui ont des formes variées (XML, images, etc.). Les ressources n&#8217;ont pas de notion de logique mais représentent plus le contenu et la &#8220;forme&#8221; de l&#8217;application</li>
</ul>
<p>Il existe un très grand nombre de ressources disponibles sous Android. J&#8217;ai déjà largement traité de nombreuses d&#8217;entre-elles comme les <code>Drawables</code> (dans une présentation que j&#8217;ai donné aux <em>Android Developers Labs</em> de Paris - présentation disponible sur <a href="http://www.slideshare.net/cyrilmottier/tour-d-horizon-des-drawables">SlideShare</a> et code source téléchargeable sur <a href="http://github.com/cyrilmottier/DrawablePresentation">GitHub</a>) ou les layouts (dans presque tous les articles de ce blog : si vous n&#8217;avez jamais remarqué, honte à vous !). Il existe énormément d&#8217;autres types de ressources sur Android (couleurs, styles, thèmes, etc.) mais nous allons ici nous attarder sur une ressource rarement abordée mais pourtant essentielle : les chaines de caractères et le fichier <code>strings.xml</code>.</p>
<h2>Les chaines de caractères simples</h2>
<p>Lorsqu&#8217;on souhaite utiliser des chaines de caractères dans une application, il suffit d&#8217;inclure la chaine dans les ressources de type &#8220;string&#8221; comme montré ci-dessous :</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p288code15'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p28815"><td class="code" id="p288code15"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;application_name&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>My Application<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;application_description&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>This is my first Android application!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>    
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>La récupération d&#8217;une chaine de caractères s&#8217;effectue ensuite grâce à son nom (ou <em>name</em>). Le code ci-dessous est extrait d&#8217;une <code>Activity</code> (et donc d&#8217;un <code>Context</code>) et montre comment récupérer de façon efficace et rapide la valeur d&#8217;une chaine de caractères :</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p288code16'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p28816"><td class="code" id="p288code16"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> appName <span style="color: #339933;">=</span> getString<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">string</span>.<span style="color: #006633;">application_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Lorsque vous ne disposez que du <code>Context</code> (dans une <code>View</code> par exemple), il est nécessaire de d&#8217;abord récupérer les ressources :</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p288code17'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p28817"><td class="code" id="p288code17"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> appName <span style="color: #339933;">=</span> context.<span style="color: #006633;">getResources</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">string</span>.<span style="color: #006633;">application_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Les chaines de caractères formatées</h2>
<p>Il est parfois utile de créer des chaines un peu plus développées que de simple chaines fixes. Imaginons par exemple que nous souhaitions avoir une chaine de caractères décrivant un album de musique et contenant le nombre de chansons ainsi que le titre de l&#8217;album. Concaténer manuellement les différentes parties de la chaine de caractères est un travail long est inutile. En effet, Android gère parfaitement les chaines formatées et autorise donc une syntaxe comme montré ci-dessous. Pour une documentation complète sur les possibilités des chaines de caractère formatées, n&#8217;hésitez pas à vous reporter à la documentation de <code><a href="http://developer.android.com/intl/fr/reference/java/util/Formatter.html">Formatter</a></code></p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p288code18'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p28818"><td class="code" id="p288code18"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;album_description&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>%1$s contains %2$d songs<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Pour utiliser la chaine de caractères, il suffit alors d&#8217;utiliser le code suivant (dans le contexte d&#8217;une <code>Activity</code>):</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p288code19'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p28819"><td class="code" id="p288code19"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> albumDescription <span style="color: #339933;">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>getString<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">string</span>.<span style="color: #006633;">album_description</span><span style="color: #009900;">&#41;</span>, albumName, songCount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>C&#8217;est simple non ? Eh bien, comme à son habitude, le framework Android vous facilite encore plus la vie ! La méthode <code>getString(int)</code> a, en effet, été surchargée et accepte un nombre illimité de paramètres :</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p288code20'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p28820"><td class="code" id="p288code20"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> albumDescription <span style="color: #339933;">=</span> getString<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">string</span>.<span style="color: #006633;">album_description</span>, albumName, songCount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Le type <em>plurals</em></h2>
<p>Dans l&#8217;exemple précédent, il est possible de rencontrer un problème assez gênant. Lorsque qu&#8217;il n&#8217;y a qu&#8217;une seule chanson dans l&#8217;album, l&#8217;application va bêtement afficher &#8220;Banana contains 1 songs&#8221;. Dommage &#8230; mais cela peut être géré assez facilement grâce à Android et l&#8217;utilisation du type de ressource &#8220;plurals&#8221; !</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p288code21'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p28821"><td class="code" id="p288code21"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plurals</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;album_description&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">quantity</span>=<span style="color: #ff0000;">&quot;one&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>%1$s contains %2$d song<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">quantity</span>=<span style="color: #ff0000;">&quot;other&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>%1$s contains %2$d songs<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plurals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Grâce au fichier ressource ci dessous, nous venons de déclarer une chaine formatée qui se comporte différemment suivant que la quantité affichée a une valeur de un (<em>one</em>) ou plus (<em>other</em>). Dans votre code, rien de plus simple :</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p288code22'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p28822"><td class="code" id="p288code22"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> albumDescription <span style="color: #339933;">=</span> getQuantityString<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">plurals</span>.<span style="color: #006633;">album_description</span>, songCount, albumName, songCount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><em><strong>Note :</strong> Mon seul regret avec l&#8217;utilisation des ressources de type &#8220;plurals&#8221; et l&#8217;absence d&#8217;un item de quantité &#8220;zero&#8221;. Dans de tel cas, le framework utilise la valeur de <em>other</em> et affiche donc &#8220;0 songs&#8221;. Je ne comprends pas pourquoi cette valeur a été évincée du framework par Google. Un oubli (j&#8217;en doute), une raison particulière (surement mais laquelle ???) ? Vous devrez donc gérer ce cas à la main.</em></p>
<h2>L&#8217;internationalisation des chaines de caractères - i18n</h2>
<p>Regrouper l&#8217;intégralité des chaines de caractères dans les fichiers ressources permet de séparer efficacement le code source et le design des ressources textuelles. Ainsi, lorsque vous devez traduire ou faire traduire votre application, il vous suffit simplement de dupliquer le fichier strings.xml original et de modifier les valeurs de chaque chaines. L&#8217;externalisation de <strong>l&#8217;intégralité</strong> des chaines présentes dans votre application (layouts, code source, etc.) et donc vital à l&#8217;évolution de votre application. Le code suivant est donc totalement à proscrire de vos applications :</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p288code23'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p28823"><td class="code" id="p288code23"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>TextView
    android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span>
    android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span>
    android<span style="color: #339933;">:</span>text<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;This is a TextView&quot;</span> <span style="color: #339933;">/&gt;</span></pre></td></tr></table></div>

<p>Pour finir, il est également possible de faciliter le travail des traducteurs en utilisant le format XLIFF dans vos chaines de caractères. Cette spécification dont la description détaillée est disponible <a href="http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html">ici</a> permet de laisser le traducteur se concentrer sur le texte à traduire. Personnellement, je trouve cette spécification sympa mais je doute de son utilité dans une application à taille réduite comme c&#8217;est le cas pour beaucoup d&#8217;applications mobiles.</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p288code24'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p28824"><td class="code" id="p288code24"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plurals</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;found_contact&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">quantity</span>=<span style="color: #ff0000;">&quot;one&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Found 1 contact<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">quantity</span>=<span style="color: #ff0000;">&quot;other&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Found <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xliff:g</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;count&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>%1$d<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xliff:g<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> contacts<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plurals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://android.cyrilmottier.com/?feed=rss2&amp;p=288</wfw:commentRss>
		</item>
		<item>
		<title>Clean and user-friendly UIs with ActionBar</title>
		<link>http://android.cyrilmottier.com/?p=274</link>
		<comments>http://android.cyrilmottier.com/?p=274#comments</comments>
		<pubDate>Tue, 20 Jul 2010 20:32:41 +0000</pubDate>
		<dc:creator>Cyril</dc:creator>
		
		<category><![CDATA[Non classé]]></category>

		<guid isPermaLink="false">http://android.cyrilmottier.com/?p=274</guid>
		<description><![CDATA[During the past few weeks, a lot of amazing features have been integrated to GreenDroid. Some of those features includes item-based ListViews, XML-based ListViews SegmentedBar, etc. Today, I&#8217;d like to introduce you with a brand new addition to the GreenDroid library project: the ActionBar and all of its related classes.
Some of you may know, I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>During the past few weeks, a lot of amazing features have been integrated to <a href="http://android.cyrilmottier.com/?p=240">GreenDroid</a>. Some of those features includes item-based <code>ListView</code>s, XML-based <code>ListView</code>s <code>SegmentedBar</code>, etc. Today, I&#8217;d like to introduce you with a brand new addition to the GreenDroid library project: the <code>ActionBar</code> and all of its related classes.</p>
<p>Some of you may know, I&#8217;m strongly attached to make Android a great mobile platform. In order to that, I think it is compulsory to create user-friendly UIs. I really believe a great Android UI have to be be clear, need to focus on content and should be VERY easy to use &#8230; even if the user is my grandfather!</p>
<p>The concept of <code>ActionBar</code> has been introduced with the Twitter application for Android. More recently, we have seen this widget in the Google I/O 2010 schedule application. To sum up, an <code>ActionBar</code> can be considered as an advanced title bar that is capable of:</p>
<ul>
<li>containing several <code>ActionBar</code>items (similar to buttons)</li>
<li>being styled so that the general look&#8217;n'feel of the application is in accordance with the underlying brand/visual identity</li>
<li>containing a title to show the user a summary of the screen content</li>
<li>featuring several user interaction pattern.</li>
</ul>
<p><img src="http://android.cyrilmottier.com/medias/actionbar/action_bar_1.png" alt="" /></p>
<p>Google recently explained everything about the advantages of using ActionBar in your applications so I won&#8217;t spend a lot of time here talking about those key features. If you&#8217;re interested in knowing more about the <code>ActionBar</code> UI design pattern, watch the talk given at <a href="http://code.google.com/events/io/2010/sessions/android-ui-design-patterns.html">Google I/O 2010</a>.</p>
<p><em><strong>Note</strong> :As usual, snippets of code below are all extracted from the GDCatolog and GreenDroid projects. Those projects are available on GitHub at the following address: <a href="http://github.com/cyrilmottier/GreenDroid">http://github.com/cyrilmottier/GreenDroid</a></em></p>
<h2>The ActionBar and ActionBarHost classes</h2>
<p>As we have previously seen, the <code>ActionBar</code> class represents an enhanced title bar that can be included in your layouts. It supports a large set of features described below:</p>
<ul>
<li><code>greendroid:title</code>: This is the text that will be used as <code>ActionBar</code>&#8217;s title</li>
<li><code>greendroid:type</code>: An <code>ActionBar</code> can be of type <em>normal</em> (the default type) or of type <em>dashboard</em>. A <em>normal</em> <code>ActionBar</code> looks like the picture above. A <em>dashboard</em> <code>ActionBar</code> doesn&#8217;t have a &#8220;home&#8221; button and display an icon that describes the application :  the application drawable. Usually, the <em>dashboard</em> type is used only on the main screen of your application</li>
<li><code>greendroid:dividerDrawable</code>: This is the <code>Drawable</code> that is used to divide items. By default, the divider is a 1-pixel width translucent white <code>ColorDrawable</code></li>
</ul>
<p>Clickable items can be added to the <code>ActionBar</code> using the <code>addItem(Drawable)</code> or <code>addItem(int)</code> method. Clicks are handled via the <code>ActionBar.OnActionBarListener</code> interface and its <code>onActionBarItemClicked(int)</code> method.</p>
<p>The <code>ActionBarHost</code> is a simple wrapper containing a upper <code>ActionBar</code> (id <code>gd_action_bar</code>) and a lower <code>FrameLayout</code> content view (of id <code>gd_action_bar_content_view</code>).</p>
<h2>The GD[List/Tab]Activity</h2>
<p>Including <code>ActionBar</code> manually in your layout is a boring task and may be time consuming. To prevent you from doing the same actions over and over again, GreenDroid includes several useful <code>Activity</code>s:</p>
<ul>
<li>GDActivity: Hosts an <code>ActionBarHost</code></li>
<li>GDListActivity: Hosts an <code>ActionBarHost</code> and a <code>ListView</code>.</li>
<li>GDTabActivity: Hosts an <code>ActionBarHost</code> and a <code>TabHost</code> as shown below.</li>
</ul>
<p><img src="http://android.cyrilmottier.com/medias/actionbar/action_bar_2.png" alt="" /></p>
<h2>How to use the GD-classes</h2>
<p>As usual, the GD-classes has been designed focusing on GreenDroid&#8217;s development principles: improving easiness, fastness and efficiency!</p>
<h3>Applying GreenDroid to your project</h3>
<p>I won&#8217;t explain how to apply GreenDroid to your project. Indeed, I&#8217;ve discussed about that in a very long post that you can find <a>here</a>. Installation instructions are also available in the project&#8217;s <em>readme</em></p>
<h3>Make sure you application is a GDApplication</h3>
<p>GreenDroid uses information that must be available through the entire application. Some of those information are the application <code>Intent</code> (the Intent that is used when the user pressed the application drawable) or the home <code>Activity</code> class (the class of your main <code>Activity</code>). As a result you need to force your application to be a <code>GDApplication</code>. Simply add <code>android:name="greendroid.app.GDApplication"</code> to the application tag in your <code>AndroidManifest.xml</code>:</p>
<p>The default implementation of <code>GDApplication</code> always returns null values. Changing those values can be done by extending the <code>GDApplication</code>:</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p274code30'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p27430"><td class="code" id="p274code30"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.cyrilmottier.android.gdcatalog</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">greendroid.app.GDApplication</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CatalogApplication <span style="color: #000000; font-weight: bold;">extends</span> GDApplication <span style="color: #009900;">&#123;</span>
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">Class</span> getHomeActivityClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> CatalogActivity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>You can now change the class of your application:</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p274code31'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p27431"><td class="code" id="p274code31"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;application</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:icon</span>=<span style="color: #ff0000;">&quot;@drawable/greendroid_icon&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:label</span>=<span style="color: #ff0000;">&quot;@string/app_name&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:theme</span>=<span style="color: #ff0000;">&quot;@style/Theme.GDCatalog&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;.CatalogApplication&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></pre></td></tr></table></div>

<h3>Uses the GreenDroid&#8217;s base theme</h3>
<p>GreenDroid heavily relies on XML resources and especially on styles and themes. In order to use GreenDroid, make sure your application theme inherits from the GreenDroid base theme: <code>@style/Theme.GreenDroid</code></p>
<h3>Create your first GDActivity</h3>
<p>When using a <code>GDActivity</code> you don&#8217;t need to manually add an <code>ActionBar</code> to your layout. The <code>setActionBarContentView(...)</code> methods take your layout and insert it in an <code>ActionBarHost</code>.</p>
<p>Setting the title of the <code>GDActivity</code> can be done using several ways:</p>
<ul>
<li>Using one of the setTitle methods</li>
<li>Adding an extra <code>String</code> with the key <code>ActionBarActivity.GD_ACTION_BAR_TITLE</code> to the <code>Intent</code> that launches your <code>GDActivity</code></li>
<li>Setting a label to the Activity in your AndroidManifest.xml</li>
</ul>
<p>Your application is notified everytime the user clicked on an <code>ActionBar</code> item via the <code>onHandleActionBarItemClick(int)</code> method.</p>
<p>Let&#8217;s start with an example. The following code is a very simple layout composed of a single <code>TextView</code> that will be used as the content of our <code>GDActivity</code>:</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p274code32'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p27432"><td class="code" id="p274code32"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextView</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/text&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:gravity</span>=<span style="color: #ff0000;">&quot;center&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:textSize</span>=<span style="color: #ff0000;">&quot;14sp&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:textStyle</span>=<span style="color: #ff0000;">&quot;bold&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p>The <code>GDActivity</code> code is given below:</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p274code33'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p27433"><td class="code" id="p274code33"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.cyrilmottier.android.gdcatalog</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">greendroid.app.GDActivity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Intent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.TextView</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.Toast</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ActionBarActivity <span style="color: #000000; font-weight: bold;">extends</span> GDActivity <span style="color: #009900;">&#123;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        setActionBarContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">text</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Screen 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        getActionBar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">addItem</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">ic_title_export</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        getActionBar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">addItem</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">ic_title_search</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onHandleActionBarItemClick<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> position<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>position<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">:</span>
                Intent intent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, TabbedActionBarActivity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                startActivity<span style="color: #009900;">&#40;</span>intent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>
                Toast.<span style="color: #006633;">makeText</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #0000ff;">&quot;Fake feature. Click on the other item instead&quot;</span>, Toast.<span style="color: #006633;">LENGTH_SHORT</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span>
                <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onHandleActionBarItemClick</span><span style="color: #009900;">&#40;</span>position<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The result displayed an screen is shown below</p>
<p><img src="http://android.cyrilmottier.com/medias/actionbar/action_bar_3.png" alt="" /></p>
<h2>Easily create tabs with GDTabActivity</h2>
<p>One of the most useful new GreenDroid&#8217;s feature is the ability to rapidly create fancy tabs. The following code shows a demonstration screen containing three tabs:</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p274code34'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p27434"><td class="code" id="p274code34"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.cyrilmottier.android.gdcatalog</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.cyrilmottier.android.gdcatalog.util.ColorUtils</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Intent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.graphics.Color</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.TextView</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">greendroid.app.GDTabActivity</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TabbedActionBarActivity <span style="color: #000000; font-weight: bold;">extends</span> GDTabActivity <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> TAB1 <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;tab_one&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> TAB2 <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;tab_two&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> TAB3 <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;tab_three&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        setTitle<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Screen 2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        Intent intent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, FakeActivity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        intent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span>FakeActivity.<span style="color: #006633;">EXTRA_COLOR</span>, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acolor+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Color</span></a>.<span style="color: #006633;">RED</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        intent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span>FakeActivity.<span style="color: #006633;">EXTRA_TEXT</span>, <span style="color: #0000ff;">&quot;Content of tab #1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        addTab<span style="color: #009900;">&#40;</span>TAB1, <span style="color: #0000ff;">&quot;Tab One&quot;</span>, intent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        intent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, FakeActivity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        intent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span>FakeActivity.<span style="color: #006633;">EXTRA_COLOR</span>, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acolor+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Color</span></a>.<span style="color: #006633;">GREEN</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        intent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span>FakeActivity.<span style="color: #006633;">EXTRA_TEXT</span>, <span style="color: #0000ff;">&quot;Content of tab #2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        addTab<span style="color: #009900;">&#40;</span>TAB2, <span style="color: #0000ff;">&quot;Tab Two&quot;</span>, intent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        intent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, FakeActivity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        intent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span>FakeActivity.<span style="color: #006633;">EXTRA_COLOR</span>, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acolor+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Color</span></a>.<span style="color: #006633;">BLUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        intent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span>FakeActivity.<span style="color: #006633;">EXTRA_TEXT</span>, <span style="color: #0000ff;">&quot;Content of tab #3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        addTab<span style="color: #009900;">&#40;</span>TAB3, <span style="color: #0000ff;">&quot;Tab Three&quot;</span>, intent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> FakeActivity <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> EXTRA_COLOR <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com.cyrilmottier.android.gdcatalog.TabbedActionBarActivity$FakeActivity.extraColor&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> EXTRA_TEXT <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com.cyrilmottier.android.gdcatalog.TabbedActionBarActivity$FakeActivity.extraText&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        @Override
        <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">final</span> Intent intent <span style="color: #339933;">=</span> getIntent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>intent <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                TextView textView <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                textView.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getStringExtra</span><span style="color: #009900;">&#40;</span>EXTRA_TEXT<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000066; font-weight: bold;">int</span> color <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getIntExtra</span><span style="color: #009900;">&#40;</span>EXTRA_COLOR, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acolor+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Color</span></a>.<span style="color: #006633;">BLACK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                textView.<span style="color: #006633;">setBackgroundColor</span><span style="color: #009900;">&#40;</span>color<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                textView.<span style="color: #006633;">setTextColor</span><span style="color: #009900;">&#40;</span>ColorUtils.<span style="color: #006633;">negativeColor</span><span style="color: #009900;">&#40;</span>color<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><img src="http://android.cyrilmottier.com/medias/actionbar/action_bar_4.png" alt="" /></p>
<h2>Conclusion</h2>
<p>One of the biggest problem with previous releases of Android was the lack of great UI/UXs &#8230; I know/think Google is working hard on that right now to help developers creating consistent applications. I hope, GreenDroid will be a great solution to that problem and I strongly encourage you to use it as soon as possible : just keep in mind this is a work in progress that may need to be enhanced by developers like you! Happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://android.cyrilmottier.com/?feed=rss2&amp;p=274</wfw:commentRss>
		</item>
		<item>
		<title>Split information using a SegmentedBar</title>
		<link>http://android.cyrilmottier.com/?p=259</link>
		<comments>http://android.cyrilmottier.com/?p=259#comments</comments>
		<pubDate>Mon, 05 Jul 2010 19:12:20 +0000</pubDate>
		<dc:creator>Cyril</dc:creator>
		
		<category><![CDATA[Non classé]]></category>

		<guid isPermaLink="false">http://android.cyrilmottier.com/?p=259</guid>
		<description><![CDATA[In a previous post, I&#8217;ve been introducing you with a library I&#8217;m developing. I strongly encourage you to read the long post I have written because it explains almost everything about the GreenDroid library: its purpose, the purpose I&#8217;m looking for with it, etc. To sum up, GreenDroid is an approach to unify Android developments [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://android.cyrilmottier.com/?p=240">previous post</a>, I&#8217;ve been introducing you with a library I&#8217;m developing. I strongly encourage you to read the long post I have written because it explains almost everything about the GreenDroid library: its purpose, the purpose I&#8217;m looking for with it, etc. To sum up, GreenDroid is an approach to unify Android developments and UI/UXs by making application development easier and straightforward.</p>
<p>The first release of GreenDroid focused on creating and populating <code>ListView</code> in a very simple manner. This time, I&#8217;ve included a new widget that can be very helpful when designing applications: <code>SegmentedBar</code>. Used with its associated <code>SegmentedHost</code> and <code>SegmentedAdapter</code> classes, a <code>SegmentedBar</code> is really similar to a <code>TabWidget</code>. The main difference relie in the fact a <code>SegmentedBar</code> only uses views and look different:</p>
<p><img src="http://android.cyrilmottier.com/medias/segmented_bar/segmented_bar.png" /></p>
<p>As you may have noticed, a <code>SegmentedBar</code> looks like the widget that were previously (prior to Froyo) used in the Android Market application to switch between applications types. A <code>SegmentedBar</code> is a bunch of segments. Each segment has an indicator that can be on or off depending on its current state (checked or not). Once you&#8217;re tapping a segment, the associated view is displayed.</p>
<p><em><strong>Note :</strong>The snippets of code below are all extracted from the GDCatolog and GreenDroid projects. Those projects are all available on GitHub at the following address: <a href="http://github.com/cyrilmottier/GreenDroid">http://github.com/cyrilmottier/GreenDroid</a></em></p>
<p>As a developer, you probably prefer reading code than explainations &#8230; I&#8217;ve implementing a really tiny application available in the GDCatalog application that uses the 3 classes given above. The first thing to do is to created our layout:</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p259code37'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p25937"><td class="code" id="p259code37"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;greendroid.widget.SegmentedHost</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:greendroid</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/com.cyrilmottier.android.gdcatalog&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/segmentedHost&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">greendroid:segmentedBar</span>=<span style="color: #ff0000;">&quot;@+id/segmentedBar&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">greendroid:segmentedContentView</span>=<span style="color: #ff0000;">&quot;@+id/segmentedContentView&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;greendroid.widget.SegmentedBar</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/segmentedBar&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;FrameLayout</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/segmentedContentView&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;0dp&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:layout_weight</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:foreground</span>=<span style="color: #ff0000;">&quot;@drawable/gd_shadow_top&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/greendroid.widget.SegmentedHost<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>This layout is composed of a root <code>SegmentedHost</code> element that fills (or match according to the brand new <code>match_parent</code> value) the size of the screen. A <code>SegmentedHost</code> is a wrapper that contains a <code>SegmentedBar</code> and a content view (<code>FrameLayout</code>). This two elements will be filled with the data provided by a <code>SegmentedAdapter</code>. The XML attributes <code>greendroid:segmentedBar</code> and <code>greendroid:segmentedContentView</code> are mandatory and must be used to inform the <code>SegmentedHost</code> which subview must be used. I assume the rest is pretty clear so I won&#8217;t explain anything else but the <code>android:foreground</code> attribute. Android often uses shadows to separate two main areas on screen. The GreenDroid library includes shadows (named <code>gd_shadow_top</code> and <code>gd_shadow_bottom</code>) that can be reused easily to enhance your UIs.</p>
<p>Now our layout is ready, the only thing we have to do is to fill the <code>SegmentedHost</code> with the appropriate data. Data are provided through a <code>SegmentedAdapter</code> as shown below:</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p259code38'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p25938"><td class="code" id="p259code38"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.cyrilmottier.android.gdcatalog</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.cyrilmottier.android.gdcatalog.util.ColorUtils</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">greendroid.widget.SegmentedAdapter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">greendroid.widget.SegmentedHost</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.graphics.Color</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Handler</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.Gravity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.ViewGroup</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.ViewGroup.LayoutParams</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.TextView</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SegmentedActivity <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Handler mHandler <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Handler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> PeopleSegmentedAdapter mAdapter<span style="color: #339933;">;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">segmented_controls</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        SegmentedHost segmentedHost <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>SegmentedHost<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">segmentedHost</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        mAdapter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PeopleSegmentedAdapter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        mHandler.<span style="color: #006633;">postDelayed</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Arunnable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Runnable</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            @Override
            <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                mAdapter.<span style="color: #006633;">mReverse</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                mAdapter.<span style="color: #006633;">notifyDataSetChanged</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>, <span style="color: #cc66cc;">4000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        segmentedHost.<span style="color: #006633;">setAdapter</span><span style="color: #009900;">&#40;</span>mAdapter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> PeopleSegmentedAdapter <span style="color: #000000; font-weight: bold;">extends</span> SegmentedAdapter <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> mReverse <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aview+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">View</span></a> getView<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> position, ViewGroup parent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            TextView textView <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TextView<span style="color: #009900;">&#40;</span>SegmentedActivity.<span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            textView.<span style="color: #006633;">setLayoutParams</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> LayoutParams<span style="color: #009900;">&#40;</span>LayoutParams.<span style="color: #006633;">FILL_PARENT</span>, LayoutParams.<span style="color: #006633;">FILL_PARENT</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            textView.<span style="color: #006633;">setGravity</span><span style="color: #009900;">&#40;</span>Gravity.<span style="color: #006633;">CENTER</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> color <span style="color: #339933;">=</span> getColor<span style="color: #009900;">&#40;</span>mReverse <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>getCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> position<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> position<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            textView.<span style="color: #006633;">setBackgroundColor</span><span style="color: #009900;">&#40;</span>color<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            textView.<span style="color: #006633;">setTextColor</span><span style="color: #009900;">&#40;</span>ColorUtils.<span style="color: #006633;">negativeColor</span><span style="color: #009900;">&#40;</span>color<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// It's not necessary to compute the &quot;reversed&quot; position as the</span>
            <span style="color: #666666; font-style: italic;">// getSegmentTitle will do it automatically</span>
            textView.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>getSegmentTitle<span style="color: #009900;">&#40;</span>position<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">return</span> textView<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getSegmentTitle<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> position<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>mReverse <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>getCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> position<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> position<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">:</span>
                    <span style="color: #000000; font-weight: bold;">return</span> getString<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">string</span>.<span style="color: #006633;">segment_1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>
                    <span style="color: #000000; font-weight: bold;">return</span> getString<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">string</span>.<span style="color: #006633;">segment_2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span>
                    <span style="color: #000000; font-weight: bold;">return</span> getString<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">string</span>.<span style="color: #006633;">segment_3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">:</span>
                    <span style="color: #000000; font-weight: bold;">return</span> getString<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">string</span>.<span style="color: #006633;">segment_4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> getColor<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> position<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>position<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">:</span>
                    <span style="color: #000000; font-weight: bold;">return</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acolor+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Color</span></a>.<span style="color: #006633;">RED</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>
                    <span style="color: #000000; font-weight: bold;">return</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acolor+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Color</span></a>.<span style="color: #006633;">GREEN</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span>
                    <span style="color: #000000; font-weight: bold;">return</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acolor+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Color</span></a>.<span style="color: #006633;">BLUE</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">:</span>
                    <span style="color: #000000; font-weight: bold;">return</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acolor+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Color</span></a>.<span style="color: #006633;">CYAN</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acolor+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Color</span></a>.<span style="color: #006633;">TRANSPARENT</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In order to create your own <code>SegmentedAdapter</code>, you simply need to extend the <code>SegmentedAdapter</code> and override the three following methods:</p>
<ul>
<li><code>int getCount()</code>: This method must return the number of segments your <code>SegmentedBar</code> will contain.</li>
<li><code>String getSegmentTitle(int position)</code>: Returns the title for the segment at the given position</li>
<li><code>View getView(int position, ViewGroup parent)</code>: This method is very similar to the traditionnal getView method. The only difference is there is no <code>convertView</code> parameter. Actually this method will be called only once in the lifetime of your <code>SegmentedHost</code>. Once created, The <code>SegmentedHost</code> will automatically keep a reference on the generated view. Hence, there is no need to recycle views. If you want to reorganize your segments, simply call <code>notifyDataSetChanged()</code> as shown in the exemple above</li>
</ul>
<p>Using a <code>SegmentedBar</code> is very useful when your screen have a lot of information that can be split in several pieces. A great sample would be an &#8220;About&#8221; screen : you can now split the developer information from the legal or partners information. I hope you&#8217;ll use this new widget as well as all other cool features integrated to the GreenDroid library. Happy coding and see you soon for new amazing GreenDroid features!</p>
]]></content:encoded>
			<wfw:commentRss>http://android.cyrilmottier.com/?feed=rss2&amp;p=259</wfw:commentRss>
		</item>
		<item>
		<title>Devenez le maitre des références sous Android</title>
		<link>http://android.cyrilmottier.com/?p=255</link>
		<comments>http://android.cyrilmottier.com/?p=255#comments</comments>
		<pubDate>Thu, 27 May 2010 22:35:26 +0000</pubDate>
		<dc:creator>Cyril</dc:creator>
		
		<category><![CDATA[Non classé]]></category>

		<guid isPermaLink="false">http://android.cyrilmottier.com/?p=255</guid>
		<description><![CDATA[Note  : L&#8217;article suivant ne traite pas particulièrement de technologies et d&#8217;APIs propres à Android. La plupart des APIs mentionnées ci-dessous sont en fait des APIs propres à Java lui-même. Néanmoins, elles sont énormément utiles dans le contexte d&#8217;Android &#8230; pourquoi ? Eh bien je ne vais pas le dire ici car c&#8217;est l&#8217;objet [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Note </strong> : L&#8217;article suivant ne traite pas particulièrement de technologies et d&#8217;APIs propres à Android. La plupart des APIs mentionnées ci-dessous sont en fait des APIs propres à Java lui-même. Néanmoins, elles sont énormément utiles dans le contexte d&#8217;Android &#8230; pourquoi ? Eh bien je ne vais pas le dire ici car c&#8217;est l&#8217;objet de cet article ! Bonne lecture</em></p>
<p>La programmation sur terminaux mobile est contrainte par un nombre importants de facteurs. L&#8217;objectif n&#8217;est pas ici de faire une liste exhaustive de tous ces facteurs car je pense l&#8217;avoir fait dans de nombreux précédents posts. Nous allons simplement traiter ici d&#8217;une des contrainte majeure : la gestion de la mémoire.</p>
<p>La gestion de la mémoire étant omniprésente dans tous systèmes informatique, les plateformes offrents des systèmes de gestion tous plus différents les uns que les autres. Certains environnement sont &#8220;garbage collectés&#8221; (c&#8217;est le cas d&#8217;Android) alors que d&#8217;autres offrent une gestion de la mémoire manuelle (c&#8217;est le cas de l&#8217;iPhone par exemple : lorsque vous créer un objet - message <code>alloc</code> - vous devez explicitement mentionner que vous ne l&#8217;utilisez plus - message <code>release</code> - pour que le système le supprime). Je ne vais pas rentrer dans un débat sans fin pour dire quel modèle est vraiment le plus avantageux car je pense qu&#8217;aucun n&#8217;est parfait &#8230; tout dépend du contexte dans lequel on les utilise.</p>
<p>En Java, lorsque vous créer un objet, vous créer en réalité une référence vers cet objet. La durée de vie d&#8217;un objet Java décrit souvent le schéma suivant :</p>
<ul>
<li>L&#8217;objet O est créé et le développeur dispose d&#8217;une référence A sur ce dernier</li>
<li>L&#8217;objet O commence à &#8220;vivre&#8221; : il est référencé par d&#8217;autres objets B, C et D</li>
<li>Les objets B, C et D n&#8217;utilise plus 0 est donc le compte de référence sur O repasse à 1 (référence A)</li>
<li>La référence A n&#8217;est plus utilisée et l&#8217;objet O devient donc &#8220;non référencé&#8221; : c&#8217;est à ce moment là que le garbage collector considère qu&#8217;il est possible de supprimer l&#8217;objet de la mémoire.</li>
</ul>
<p>Laisser le garbage collector gérer lui-même la mémoire facilite grandement le développeur. Malheureusement il arrive parfois que ce dernier prenne ses aises et qu&#8217;il utilise trop de mémoire. Le résultat est souvent impitoyable et se résume en une unique <code>Exception</code> que nous devrions tous essayer d&#8217;éviter : <code>OutOfMemoryException</code>. Comme son nom l&#8217;indique, cette <code>Exception</code> indique qu&#8217;il n&#8217;y a plus assez de mémoire pour créer l&#8217;objet.</p>
<p>Avant d&#8217;introduire la solution à ce problème j&#8217;aimerai donner la philosophie qui a été choisie sur l&#8217;iPhone SDK pour permettre de résoudre ce problème. Le SDK fournit un réalité un centre de notifications : le <code>NSNotificationCenter</code>. Il est possible de s&#8217;enregistrer auprès de ce centre de notification et ainsi être notifié de possibles messages du système. Dans notre cas, il suffit d&#8217;écouter le message <code>UIApplicationDidReceiveMemoryWarningNotification</code> (oui je sais il est long ce nom &#8230; vive l&#8217;autocomplétion !) afin de savoir quand il devient urgent de supprimer des objets inutiles/recréables de la mémoire (caches, objets inutilisé, etc.)</p>
<p>Java n&#8217;inclut pas ce genre de système. Néanmoins il est possible de gérer la mémoire de façon plus précise en utilisant un des 3 types de référence présentés ci-dessous :</p>
<ul>
<li><code>Reference</code> : C&#8217;est la référence classique. C&#8217;est le type de référence par défaut utilisée partout dans Java. La durée de vie d&#8217;un objet rattaché par une <code>Reference</code> a été expliqué précédemment</li>
<li><code>WeakReference</code> : C&#8217;est une référence qui indique au GC que l&#8217;objet référencé peut être supprimé si et seulement si il est <strong>référencé uniquement par une ou plusieurs <code>WeakReference</code>s</strong>.</li>
<li><code>SoftReference</code> : Un objet référencé par une ou plusieurs <code>SoftReference</code> sera supprimé de la mémoire lorsque le <strong>GC va considérer que le système manque de mémoire</strong>. C&#8217;est donc la référence parfaite pour émuler le comportement décrit ci-dessus. C&#8217;est pourquoi on utilise souvent cette référence lors de l&#8217;élaboration de caches par exemple.</li>
</ul>
<p>Il existe encore un autre type de référence nommé <code>PhantomReference</code> mais je ne pense pas qu&#8217;elle est une utilité dans vos développements Android. Vous voilà maintenant fin prêt pour parfaire la gestion mémoire de votre application !</p>
]]></content:encoded>
			<wfw:commentRss>http://android.cyrilmottier.com/?feed=rss2&amp;p=255</wfw:commentRss>
		</item>
		<item>
		<title>Introduction to the GreenDroid library</title>
		<link>http://android.cyrilmottier.com/?p=240</link>
		<comments>http://android.cyrilmottier.com/?p=240#comments</comments>
		<pubDate>Fri, 14 May 2010 19:05:51 +0000</pubDate>
		<dc:creator>Cyril</dc:creator>
		
		<category><![CDATA[Non classé]]></category>

		<guid isPermaLink="false">http://android.cyrilmottier.com/?p=240</guid>
		<description><![CDATA[Introduction to the GreenDroid library
Note :  Before starting with this post I&#8217;d like to explain myself about the fact this article has been written in English. Indeed, I have proclaimed, quite a few times, this blog was intended to French speakers only. Actually I have recently changed my mind and here is a brief [...]]]></description>
			<content:encoded><![CDATA[<h1>Introduction to the GreenDroid library</h1>
<p><em><strong>Note : </strong> Before starting with this post I&#8217;d like to explain myself about the fact this article has been written in English. Indeed, I have proclaimed, quite a few times, this blog was intended to French speakers only. Actually I have recently changed my mind and here is a brief explanation. When I created this blog I was trying to explain to French speakers how to use and code properly for the Android platform. It had been a truly difficult choice for me because I was and I&#8217;m still a big fan of English speeches/talks. Nevertheless, the main reason that pushed me to start writing in French was the fact French documentation dealing with Android was/is very poor. For more than a year, I have written several articles and I really hope you have appreciated all of them. Nowadays, I want to talk about innovative concepts and/or technologies which, I think, will be useful to many users/developers all around the world. As a result, I have to write my posts using the most common language on Earth: English. I&#8217;m sure, French speakers will succeed in reading this blog and I&#8217;d like to warn them: &#8220;Je n&#8217;ai pas prévu d&#8217;arrêter d&#8217;écrire des posts en français !&#8221;</em>.</p>
<p>I&#8217;m really glad to introduce you to my first attempt of a &#8220;true&#8221; development library for Android! As you may have noticed it is named &#8220;GreenDroid&#8221; and is intended to help people in designing Android applications. Keep in mind, it is still a work in progress! Let&#8217;s explain everything about GreenDroid before diving into it!</p>
<p><img src="http://android.cyrilmottier.com/medias/greendroid_intro/greendroid_logo.png" /></p>
<h2>The main purpose of GreenDroid</h2>
<p>As some of you may know, I&#8217;m a mobile software engineer and I&#8217;ve already developed on many constrained platforms. Each platform I have used have inherent advantages or disadvantages. For instance, Android development rocks because it is based on the Java programming language which is easy to use. On the other hand, I&#8217;ve also heavily used the iPhone SDK and I&#8217;m a huge fan of its simplicity - it looks like the Mac OS X user environment: simple and efficient - and most of all, the fact you can create your own libraries.</p>
<p>For a long time I have coded on the Android platform by doing the same things over and over again. To prevent me from doing mistakes and <em>copy&amp;paste</em>s I&#8217;ve decided to initiate a personal project called GreenDroid. Here are the main purposes of this project:</p>
<ul>
<li><strong>Prevent loosing time at copying the same snippets of code over and over again</strong>.</li>
<li><strong>Try to make Android applications alike.</strong> The openness of the Android operating system makes it less coherent. Seing so many applications that use totally different ergonomics or designs drives me mad. Indeed, I truly think, Android applications need coherency in order to get more and more users around the world. GreenDroid is kinda of a try to bring coherency to your applications and therefore to the Android environment. Google recently tried to show a path to &#8220;great UX and UI&#8221; with the Twitter and Google I/O applications - A talk about Android UIs will be given at Google I/O 2010 and I&#8217;m looking forward to watch it; I would like to see if Google has understood Android developers need help creating simple and smart interfaces!</li>
<li><strong>Help developers to code highly functional applications.</strong> The Android framework may look like &#8220;over-engineered&#8221; sometimes. Actually, I believe this is a direct consequence of the fact you can do almost everything you want. Unfortunatly, this openness (again!) makes it harder to apprehend. Let&#8217;s say for instance, you are a beginner and wants to develop your own application. You&#8217;ll have to read a lot of documentation in order to be &#8220;up and ready&#8221;. GreenDroid makes development a lot easier without dicreasing the powerfulness of the amazing Android framework!</li>
<li><strong>Leverage the power of the Android framework.</strong> Developing on the Android platform may be pretty easy if you&#8217;re not taking care of the resources you&#8217;re using. Trying to optimize your application is quite hard sometimes and is a very demanding task. GreenDroid has been developed to be as efficient as possible by integrating basic optimizations.</li>
<li><strong>Use as much XML as possible.</strong> It&#8217;s not a mystery to anybody. Android UI development is based on amazing techniques. Layouts and views are defined in XML and automatically inflated by the system. Being an &#8220;easy-to-read-for-humans&#8221; format, XML is very used among Android developers. GreenDroid puts XML in the middle of the library and takes advantage of all amazing possibilities offered by Android XML files.</li>
</ul>
<h2>Why have I chosen the &#8220;GreenDroid&#8221; name?</h2>
<p>When I started this project, I have spent a few minutes to think about an appropriate name. At the first glance, I was like &#8220;Who cares? It&#8217;s a personal project and I will never share it!&#8221; but once I realized this project could be useful for a lot of people I decided to think about a clear and smart name. The thought ended up on the name GreenDroid which can be seen as a contraction of the &#8220;green&#8221; color and the name &#8220;Android&#8221;. I suppose I don&#8217;t need to explain why the library name contains an occurence to Android &#8230; so I&#8217;ll explain what the &#8220;green&#8221; word stands for:</p>
<ul>
<li><strong>Green because it&#8217;s an &#8220;ecological&#8221; library.</strong> One of the main purpose of this library, as said previously, is to be as efficient as possible by helping developers to optimize their applications. The green color refers to ecology and I thought using it was a good explanation to the library: develop preserving all resources that are available to you!</li>
<li><strong>Green because Android is nothing without green</strong> Come on guys! You all know the Android mascot is always green. Seeing it wearing other colors makes all of us sad. I couldn&#8217;t name my library using another color!</li>
<li><strong>Green because I like being green!</strong>
</ul>
<h2>How hard it is to create an Android library</h2>
<p>The answer is quite simple and straight forward; it&#8217;s impossible! Why? Because it is impossible to bundle R.java in external jars &#8230; :(. This R.java file contains a set of references to external resources as simple integers <code>int</code>. The only external R.java available in your application is the one bundled with the Android framework.</p>
<p>Here are some ways I know that try to overcome that R.java problem:</p>
<ul>
<li>Using no resource. The problem with that method is it very painful to code without XML. Being convinced Android developers enjoy XML, I&#8217;m sure there are just a few people creating their GUI using Java code. Personally, I love the <code>LayoutInflater</code> and its simplicity.</li>
<li>Using methods such as <code>Resources.getIdentifier(String name, String defType, String defPackage)</code>. Unfortunaty those methods are quite slow to execute which is the opposite of GreenDroid&#8217;s main purpose. It&#8217;s a great alternative but makes the Java almost unreadable and still force the developer to manually copy resources from the library to his/her project.</li>
<li>Copying manually all resources from a project to another. This is basically the method I decided to use. The only difference with the &#8220;dumb way&#8221; is I created a Python (I&#8217;m still a newbie with Python - but I really like it) script that applies the GreenDroid library to your application by switching automatically to the project package name. Developing a graphical library being impossible, I have developed many features that will &#8220;fake&#8221; GreenDroid is a library. This is an approach I have designed alone and I&#8217;m still listening to your ideas to overcome that R problem of the Android SDK</li>
</ul>
<p><em><strong>Note : </strong> I haven&#8217;t taken a look at the Maven Android plugin yet. If one of you think it could be a great way to overcome the R problem, feel free to reply</em></p>
<h2>How to use GreenDroid ?</h2>
<p>In order to use GreenDroid you have to do the following steps:</p>
<ul>
<li>1. Download the GreenDroid library on your computer with a simple:
<pre>git clone http://github.com/cyrilmottier/GreenDroid.git</pre>
</li>
<li>2. Apply GreenDroid to your project: Go to the scripts/ folder of the GreenDroid folder. Run the script named &#8220;greendroid.py&#8221; (make sure you can run it - <code>chmod +x greendroid.py</code>) to apply the library to your project :
<pre>./greendroid.py apply &lt;project_directory&gt;</pre>
</li>
<li>3. By default, the GreenDroid theme inherits from <code>@android:style/Theme</code>. If your project inherits from a different theme, you&#8217;ll have to modify the GreenDroid library on your own (and do that everytime you are updating GreenDroid as updating delete all of your changes). Open the <code>res/values/gd_themes.xml</code> and replace the parent theme <code>@android:style/Theme</code> with your own theme</li>
<li>4. You finally need to make your project use the GreenDroid base theme. In your Android Manifest, go to the application tag and add <code>android:theme="@style/Theme.GreenDroid"</code> as a new attribute (if this attribute already exist override it: if you processed the step 3 correctly, you will have a theme that inherits from this theme)</li>
</ul>
<h2>Hello GreenDroid !</h2>
<p>As I said previously, GreenDroid is pretty much a work in progress. For now, it mostly enhances <code>ListView</code> and <code>Adapter</code>s by helping the user creating advanced UIs. I have thousands of ideas about how to make Android development easier and how to create easier amazing UIs but I don&#8217;t have a lot of free time to develop those importants features&#8230;</p>
<h3>Items-based <code>ListView</code>s</h3>
<p>Creating <code>ListView</code> with various cells can be quite painful. You have to understand perfectly how an <code>Adapter</code> work. You also need to create layout for all of those cells and this is a very boring task. Creating advanced <code>ListView</code>s with GreenDroid is very simple and relies on a simple notion: the <em>item</em>. An item is a wrapper of data that contains everything that will be displayed in the associated <em>item view</em>. The following snippet of code shows you how to use an <code>ItemAdapter</code>:</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p240code42'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p24042"><td class="code" id="p240code42"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.cyrilmottier.android.gdcatalog</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">greendroid.widget.ItemAdapter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">greendroid.widget.item.DescriptionItem</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">greendroid.widget.item.DrawableItem</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">greendroid.widget.item.Item</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">greendroid.widget.item.ProgressItem</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">greendroid.widget.item.SeparatorItem</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">greendroid.widget.item.TextItem</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">greendroid.widget.item.ThumbnailItem</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.ListActivity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Handler</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BasicItemActivity <span style="color: #000000; font-weight: bold;">extends</span> ListActivity <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Handler mHandler <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Handler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        List<span style="color: #339933;">&lt;</span>Item<span style="color: #339933;">&gt;</span> items <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Item<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SeparatorItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Class 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ThumbnailItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Powered paragliding&quot;</span>, <span style="color: #0000ff;">&quot;aka paramotoring&quot;</span>, R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">class1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DescriptionItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus consequat leo, et tincidunt justo tristique in.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SeparatorItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Class 2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DrawableItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Trikes&quot;</span>, R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">class2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DescriptionItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus consequat leo, et tincidunt justo tristique in.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SeparatorItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Class 3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ThumbnailItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Multi-axis&quot;</span>, <span style="color: #0000ff;">&quot;Looks like a tiny place&quot;</span>, R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">class3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DescriptionItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus consequat leo, et tincidunt justo tristique in.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SeparatorItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Class 4&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ThumbnailItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Auto-gyro&quot;</span>, <span style="color: #0000ff;">&quot;A scary helicopter&quot;</span>, R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">class4</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DescriptionItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus consequat leo, et tincidunt justo tristique in.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SeparatorItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Class 5&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DrawableItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hot air baloon&quot;</span>, R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">class5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DescriptionItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus consequat leo, et tincidunt justo tristique in.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">final</span> Item item1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SeparatorItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Class 6&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> Item item2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TextItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Airbus/Boeing planes&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> Item item3 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DescriptionItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus consequat leo, et tincidunt justo tristique in.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>item1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>item2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>item3<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">final</span> ProgressItem progressItem <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProgressItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Removing intruders&quot;</span>, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        items.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>progressItem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">final</span> ItemAdapter adapter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ItemAdapter<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, items<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setListAdapter<span style="color: #009900;">&#40;</span>adapter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        mHandler.<span style="color: #006633;">postDelayed</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Arunnable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Runnable</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            @Override
            <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                adapter.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>item1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                adapter.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>item2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                adapter.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>item3<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                adapter.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>progressItem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                adapter.<span style="color: #006633;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ThumbnailItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ultralight aviation&quot;</span>, <span style="color: #0000ff;">&quot;List of French 'ULM' classes&quot;</span>, R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">greendroid_icon</span><span style="color: #009900;">&#41;</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                adapter.<span style="color: #006633;">notifyDataSetChanged</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>,<span style="color: #cc66cc;">8000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>XML generated items</h3>
<p>Android has an amazing feature that consists on inflating data and views from an XML file. I extended it to the GreenDroid library. With GreenDroid you can create and inflate static items and display those items in a simple <code>ListView</code>:</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p240code43'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p24043"><td class="code" id="p240code43"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item-array</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:greendroid</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/com.cyrilmottier.android.gdcatalog&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;text-item</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:text</span>=<span style="color: #ff0000;">&quot;TextItem&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;text-item</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:enabled</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:text</span>=<span style="color: #ff0000;">&quot;TextItem (disabled)&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;text-item</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:text</span>=<span style="color: #ff0000;">&quot;TextItem Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus consequat leo, et tincidunt justo tristique in.&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;longtext-item</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:text</span>=<span style="color: #ff0000;">&quot;LongText Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus consequat leo, et tincidunt justo tristique in.&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description-item</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:text</span>=<span style="color: #ff0000;">&quot;DescriptionItem Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus consequat leo, et tincidunt justo tristique in.&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;separator-item</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:text</span>=<span style="color: #ff0000;">&quot;SeparatorItem&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;progress-item</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:isInProgress</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:text</span>=<span style="color: #ff0000;">&quot;ProgressItem&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;drawable-item</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:enabled</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:text</span>=<span style="color: #ff0000;">&quot;DrawableItem (disabled)&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:drawable</span>=<span style="color: #ff0000;">&quot;@drawable/greendroid_icon&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;drawable-item</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:text</span>=<span style="color: #ff0000;">&quot;DrawableItem (with no drawable)&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;subtitle-item</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:text</span>=<span style="color: #ff0000;">&quot;SubtitleItem&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:subtitle</span>=<span style="color: #ff0000;">&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus consequat leo, et tincidunt justo tristique in.&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;subtext-item</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:text</span>=<span style="color: #ff0000;">&quot;SubtextItem&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:subtext</span>=<span style="color: #ff0000;">&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus consequat leo, et tincidunt justo tristique in.&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;thumbnail-item</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:text</span>=<span style="color: #ff0000;">&quot;ThumbnailItem&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:subtitle</span>=<span style="color: #ff0000;">&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus consequat leo, et tincidunt justo tristique in.&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:thumbnail</span>=<span style="color: #ff0000;">&quot;@drawable/greendroid_icon&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;thumbnail-item</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:enabled</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:text</span>=<span style="color: #ff0000;">&quot;ThumbnailItem (disabled)&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:subtitle</span>=<span style="color: #ff0000;">&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus consequat leo, et tincidunt justo tristique in.&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">greendroid:thumbnail</span>=<span style="color: #ff0000;">&quot;@drawable/greendroid_icon&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item-array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Thanks to XML, the associated Java code is very clear and simple:</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p240code44'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p24044"><td class="code" id="p240code44"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.cyrilmottier.android.gdcatalog</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">greendroid.widget.ItemAdapter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.ListActivity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> XmlItemActivity <span style="color: #000000; font-weight: bold;">extends</span> ListActivity <span style="color: #009900;">&#123;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        ItemAdapter adapter<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            adapter <span style="color: #339933;">=</span> ItemAdapter.<span style="color: #006633;">createFromXml</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, R.<span style="color: #006633;">xml</span>.<span style="color: #006633;">items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            setListAdapter<span style="color: #009900;">&#40;</span>adapter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The GreenDroid library can be download on Github:</p>
<pre>http://github.com/cyrilmottier/GreenDroid</pre>
<p>The library includes a project called GDCatalog that uses the GreenDroid library. It has been develop to help developers understanding how to use GreenDroid in their projects. Keep it mind, the GDCatalog project doesn&#8217;t include the library. Prior to launch it, apply the library using the greendroid.py script. If you kept the directory hierarchy of the Git repository you need to execute the following command from the root directory:</p>
<pre>cd GreenDroid/scripts &#038;&#038; ./greendroid.py apply ../../GDCatalog</pre>
<p>Eclipse may not see the new files that have been added. In order to refresh the project, right-click on the project and select &#8220;Refresh&#8221;.</p>
<p>Happy coding !</p>
]]></content:encoded>
			<wfw:commentRss>http://android.cyrilmottier.com/?feed=rss2&amp;p=240</wfw:commentRss>
		</item>
		<item>
		<title>Parler pour ne rien dire ? Pourquoi pas !</title>
		<link>http://android.cyrilmottier.com/?p=236</link>
		<comments>http://android.cyrilmottier.com/?p=236#comments</comments>
		<pubDate>Sun, 02 May 2010 15:09:29 +0000</pubDate>
		<dc:creator>Cyril</dc:creator>
		
		<category><![CDATA[Non classé]]></category>

		<guid isPermaLink="false">http://android.cyrilmottier.com/?p=236</guid>
		<description><![CDATA[Les lecteurs de ce blog l&#8217;auront très problablement remarqué, les sujets que j&#8217;aime traiter ici sont toujours assez techniques ou traitent de l&#8217;ergonomie et de l&#8217;expérience utilisateur sous Android (c&#8217;est un point qui me tient particulièrement à coeur car je déteste voir ces applications qui viennent littéralement polluer l&#8217;Android Market en ne respectant pas - [...]]]></description>
			<content:encoded><![CDATA[<p>Les lecteurs de ce blog l&#8217;auront très problablement remarqué, les sujets que j&#8217;aime traiter ici sont toujours assez techniques ou traitent de l&#8217;ergonomie et de l&#8217;expérience utilisateur sous Android (c&#8217;est un point qui me tient particulièrement à coeur car je déteste voir ces applications qui viennent littéralement polluer l&#8217;Android Market en ne respectant pas - voire en allant TOTALEMENT à l&#8217;encontre - la logique et les démarches d&#8217;Android. Pour changer j&#8217;avais envie de donner mon ressenti sur les dernières actualités du monde Android.</p>
<h2>Twitter pour Android</h2>
<p>Personne n&#8217;a pu rater cette sortie fracassante de la version officielle de l&#8217;application Twitter pour Android. Les ressentis sont unanimes : cette application est une vraie tuerie ! Personnellement j&#8217;ai vraiment été impressionné par cette application car elle est très proche de l&#8217;ergonomie et du design que je me fais d&#8217;une bonne application Android :</p>
<ul>
<li>Design sobre : le design est totalement adéquat à un écran de téléphone mobile, il est sobre, correspond parfaitement au couleur et à l&#8217;identité de Twitter. Le tout est bien sûr animé ce qui donne une touche de &#8220;vivant&#8221; à l&#8217;application. Malgré un thème totalement &#8220;twiterrisé&#8221;, l&#8217;entreprise à l&#8217;oiseau n&#8217;a pas oublié de respecter les notions d&#8217;états pressé, focusé, etc.</li>
<li>Ergonomie simple et efficace : l&#8217;information est visible rapidement. La prise en main est d&#8217;une simplicité enfantine et les écrans s&#8217;enchainent avec perfection.</li>
</ul>
<p>En clair, Twitter est un vrai coup de coeur pour moi et pour beaucoup d&#8217;autres. Bravo Twitter et bravo Android bien sûr ! J&#8217;apprécie voir ce genre d&#8217;applications sortir sur la plateforme au robot vert car cela montre bien que le framework n&#8217;est absolument pas bloquant d&#8217;un point de vue possibilités techniques ou graphiques (c&#8217;est une idée pourtant reçue et qu&#8217;on n&#8217;hésite pas à me mentionner régulièrement &#8230;).</p>
<h2>Google I/O</h2>
<p>Je retiens mes larmes lorsque j&#8217;écris l&#8217;expression &#8220;Google I/O&#8221;. Pourquoi ? Tout simplement parce que c&#8217;est exactement le genre d&#8217;évènement où j&#8217;aimerai me trouver. Malheureusement, le coût financier a eu raison de moi &#8230; Je ne parle absolument pas du coût de l&#8217;entrée mais plutôt de la logistique qui entoure cet évènement. J&#8217;aurais volontier payé 400 ou 500$ rien que pour rencontrer les brillant ingénieurs de chez Google et/ou les fans des services et technologies Google. Malheureusement les 1000€ (minimum) d&#8217;avion et de logistique annexe (hôtels, nourriture, transport, etc.)</p>
<p>La disponibilité souvent rapide des vidéos des sessions techniques est une aubaine pour les développeurs. Cela permet d&#8217;être rapidement à jour sur les dernières avancées des technologies Google. Je reste scotché à mes flux RSS pour être notifié de leur disponibilité !</p>
<p>Pour finir, il est clair que cette édition des Google I/O sera l&#8217;occasion d&#8217;annoncer des nouveautés. Après &#8220;Google Waves&#8221; l&#8217;année dernière, je pense sincérement que cette année sera la part belle à Android. Les derniers changements de comportement de Google vis à vis des releases Android sont parfaitement en accord avec ma vision de l&#8217;avenir d&#8217;Android. Google commence à réduire la vitesse de mise à jour de son système d&#8217;exploitation mobile. Cela laisse d&#8217;une part le temps aux constructeurs d&#8217;effectuer des mises à jour de leurs terminaux et limite d&#8217;autre part la fragmentation (moins de version d&#8217;Android dans la nature). Je suis impatient de voir ce que les ingénieurs de Google nous ont concocté pour cette prochaine mise à jour qui s&#8217;annonce gigantesque !</p>
<h2>Flash ou pas Flash</h2>
<p>Question délicate que de discuter de la nécessité ou non d&#8217;avoir Flash sur nos téléphones mobiles. Mon avis sur la question est assez simple : je ne suis pas en faveur de la présence de Flash sur nos beaux terminaux Android. Je crois, en effet, qu&#8217;HTML5/CSS3 doit être au coeur de la modernisation du web et je le perçois comme un parfait remplacant à Flash.</p>
<p>Il semblerait que la prochaine version d&#8217;Android incorpore la prise en charge totale de Flash &#8230; cela va sans dire que ça me déçoit :). J&#8217;aurais sincèrement préféré des améliorations du moteur de rendu (encore assez loin des possibilités et de la fluidité offertes par Safari Mobile) qu&#8217;une intégration pure et simple de Flash. J&#8217;ai aussi un peu de mal à comprendre la logique de Google qui a toujours souhaité aller de l&#8217;avant et essayer de limiter de plus en plus l&#8217;utilisation de Flash. On se souviendra, par exemple, de Youtube commenant à afficher des vidéos sans utiliser Flash ou Chrome qui est un très grand acteur de l&#8217;évolution d&#8217;HTML5/CSS3 &#8230;</p>
<h2>Conclusion</h2>
<p>Comme vous vous en douter, l&#8217;objectif de ce post n&#8217;était pas de lancer une guerre mais tout simplement de donner mon ressenti. N&#8217;hésitez pas à laisser un commentaire afin de donner votre opinion sur les différents points abordés précédemment. A très bientôt pour un post un peu particulier &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://android.cyrilmottier.com/?feed=rss2&amp;p=236</wfw:commentRss>
		</item>
		<item>
		<title>Astuce #8 : Utilisez la transparence avec intelligence</title>
		<link>http://android.cyrilmottier.com/?p=234</link>
		<comments>http://android.cyrilmottier.com/?p=234#comments</comments>
		<pubDate>Fri, 23 Apr 2010 06:36:38 +0000</pubDate>
		<dc:creator>Cyril</dc:creator>
		
		<category><![CDATA[Non classé]]></category>

		<guid isPermaLink="false">http://android.cyrilmottier.com/?p=234</guid>
		<description><![CDATA[Note : L&#8217;article ci-dessous traite principalement des optimisations sur les Views. Ne perdez pas à l&#8217;esprit que les concepts décrits ici sont aussi applicables à toutes notions graphiques sur Android (Drawable, Canvas, etc.)
Une belle interface graphique passe généralement par une utilisation souvent excessive de la transparence dans le design. Lorsqu&#8217;on regarde de plus prêt le [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Note : </strong>L&#8217;article ci-dessous traite principalement des optimisations sur les <code>View</code>s. Ne perdez pas à l&#8217;esprit que les concepts décrits ici sont aussi applicables à toutes notions graphiques sur Android (<code>Drawable</code>, <code>Canvas</code>, etc.)</em></p>
<p>Une belle interface graphique passe généralement par une utilisation souvent excessive de la transparence dans le design. Lorsqu&#8217;on regarde de plus prêt le code source d&#8217;Android et notamment les ressource graphiques disponibles dans le dossier <code>res/drawable</code> (images, 9-patchs, etc.), on se rend même compte qu&#8217;il est difficile de &#8220;vivre&#8221; sans transparence. C&#8217;est une des raisons pour laquelle le format PNG24 est largement préconisé dans le développement sous Android (le fait que ce soit un format ouvert et efficace joue également énormément dans la balance). Développer une interface graphique qui s&#8217;adapte à différentes tailles d&#8217;écran mais sans avoir accès à une notion de transparence s&#8217;avère malheureusement un calvaire et ne sera pas sans rappeler le développement web sous IE6 il y a encore peu de temps &#8230;</p>
<p>La transparence (ou parfois appelée &#8220;l&#8217;alpha&#8221;) engendre quelques conséquences sur l&#8217;interface graphique. En effet gérer l&#8217;alpha signifie forcément faire des calculs supplémentaires pour déterminer la couleur finale d&#8217;un pixel. Imaginons par exemple avoir 2 couches à superposer : une première couche (couche 1) totalement verte (#0f0) et une seconde (couche 2), qu&#8217;on pose sur la précédente, rouge transparent (#7f00). Le rendu s&#8217;effectue donc en 2 phases :</p>
<ul>
<li>On applique/dessine la couche verte (couche 1)</li>
<li>On dessine ensuite la couche rouge transparente en effectuant, pour chaque pixel, un calcul permettant de déterminer la couleur résultante en fonction de actuel l&#8217;état (de la couleur) du pixel de coordonnée (x, y) à l&#8217;écran et du pixel (x, y) à appliquer.</li>
</ul>
<p>Déterminer la couleur résultante passe par un calcul qui s&#8217;effectue sur chaque pixel. Bien que ce calcul se fasse via JNI (en C) - ou même parfois de façon matérielle, il n&#8217;en reste pas moins que c&#8217;est un calcul coûteux. L&#8217;utilisation de la transparence dans vos interfaces graphiques est donc forcément synonyme de &#8220;baisse de la rapidité/fluidité&#8221;. Votre objectif est donc maintenant de déterminer la valeur de cette baisse &#8230;</p>
<p>Une optimisation instaurée dans l&#8217;API level 7 sur <code>View</code> est <code>isOpaque</code>. Elle permet tout simplement d&#8217;informer le système si la vue est opaque (sans aucune transparence) ou non. Cette méthode peut sembler inutile mais elle peut pourtant permettre de nombreuses optimisations graphiques. En effet, si une vue opaque recouvre intégralement une autre vue, il n&#8217;est pas nécessaire de dessiner la vue sous-jacente. Les premières version d&#8217;Android n&#8217;intégraient pas ce mécanisme et redessinaient de façon totalement stupide l&#8217;intégralité des vues à chaque passe de dessin. Cette méthode apporte donc quelques optimisations permettant d&#8217;accélérer votre UI.</p>
<p>N&#8217;allez pas vous tracassez pour vos interfaces graphiques. Il n&#8217;est vraiment pas nécessaire de supprimer toute occurrence à la transparence dans vos UIs tant que ces dernières sont statiques. L&#8217;intérêt d&#8217;une telle optimisation est surtout applicable lorsque les vues sont animées car c&#8217;est à ce stade que les ralentissements peuvent se faire sentir. La <code>ListView</code> est probablement le widget le plus concerné par ces problèmes.</p>
]]></content:encoded>
			<wfw:commentRss>http://android.cyrilmottier.com/?feed=rss2&amp;p=234</wfw:commentRss>
		</item>
		<item>
		<title>Astuce #7 : Créer des listes à cellules variées</title>
		<link>http://android.cyrilmottier.com/?p=232</link>
		<comments>http://android.cyrilmottier.com/?p=232#comments</comments>
		<pubDate>Thu, 08 Apr 2010 15:54:05 +0000</pubDate>
		<dc:creator>Cyril</dc:creator>
		
		<category><![CDATA[Non classé]]></category>

		<guid isPermaLink="false">http://android.cyrilmottier.com/?p=232</guid>
		<description><![CDATA[Ce dont je vais parler ci dessous peut paraitre évident pour la plupart des développeurs Android mais c&#8217;est pourtant une notion qui me parait mal connue ou mal utilisée. Le notion que je mentionne ici, c&#8217;est le type de vue des items d&#8217;une ListView.
La ListView est un composant permettant d&#8217;afficher un ensemble de données similaires [...]]]></description>
			<content:encoded><![CDATA[<p>Ce dont je vais parler ci dessous peut paraitre évident pour la plupart des développeurs Android mais c&#8217;est pourtant une notion qui me parait mal connue ou mal utilisée. Le notion que je mentionne ici, c&#8217;est le type de vue des items d&#8217;une <code>ListView</code>.</p>
<p>La <code>ListView</code> est un composant permettant d&#8217;afficher un ensemble de données similaires sous forme de vue scrollable. C&#8217;est un widget élémentaire dans n&#8217;importe quel système d&#8217;exploitation mobile car il permet d&#8217;afficher un grand nombre de données un minimisant l&#8217;impact mémoire (seules les vues affichées à l&#8217;écran sont dans la hiérarchie de vues, système de réutilisation des vues, etc.) et en rendant la lecture par l&#8217;utilisateur la plus optimale possible (cellules souvent larges, gestures avancés comme le scroll ou le fling, etc.). Une <code>ListView</code> s&#8217;utilise, en général, pour afficher un nombre important de données du même type. Néanmoins, il est possible d&#8217;inclure des données un peu &#8220;exotiques&#8221; pour casser l&#8217;impression d&#8217;uniformité. Un exemple récurrent serait de de séparer les données ayant un trait commun.</p>
<p><img src="http://android.cyrilmottier.com/medias/item_view_type/screen1.png" /></p>
<p>Imaginons que nous ayons une longue liste de titre de films et de séries. Pour faciliter la lecture et la recherche, il peut être intéressant de trier ces vidéos par type et donc d&#8217;afficher un séparateur pour chaque section (qui peut par la même occasion casser l&#8217;impression de &#8220;bloc&#8221; d&#8217;une liste). Le code ci-dessous donne une implémentation de cette liste statique (code disponible <a href="http://android.cyrilmottier.com/medias/item_view_type/ItemViewType.zip">ici</a>) :</p>

<div class="wp_codebox_msgheader"><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p232code46'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p23246"><td class="code" id="p232code46"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.cyrilmottier.android.itemviewtype</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.ListActivity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.LayoutInflater</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.ViewGroup</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.BaseAdapter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.TextView</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MainListActivity <span style="color: #000000; font-weight: bold;">extends</span> ListActivity <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> ITEM_VIEW_TYPE_VIDEO <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> ITEM_VIEW_TYPE_SEPARATOR <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> ITEM_VIEW_TYPE_COUNT <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> Video <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> title<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> description<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> Video<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> title<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#40;</span>title, <span style="color: #0000ff;">&quot;bla bla&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> Video<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> title, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> description<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">title</span> <span style="color: #339933;">=</span> title<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">description</span> <span style="color: #339933;">=</span> description<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> OBJECTS <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;Movies&quot;</span>,
			<span style="color: #000000; font-weight: bold;">new</span> Video<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Iron Man 2&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> Video<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Adèle Blanc-Sec&quot;</span><span style="color: #009900;">&#41;</span>,
			<span style="color: #000000; font-weight: bold;">new</span> Video<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Twilight - Chapitre 3 : hésitation&quot;</span><span style="color: #009900;">&#41;</span>,
			<span style="color: #000000; font-weight: bold;">new</span> Video<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Green Zone&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> Video<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Shrek 4, il était une fin&quot;</span><span style="color: #009900;">&#41;</span>,
			<span style="color: #000000; font-weight: bold;">new</span> Video<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;L'Amour c'est mieux à deux&quot;</span><span style="color: #009900;">&#41;</span>,
			<span style="color: #000000; font-weight: bold;">new</span> Video<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sex and the City 2&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> Video<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Predators&quot;</span><span style="color: #009900;">&#41;</span>,
			<span style="color: #000000; font-weight: bold;">new</span> Video<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Inception&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;Series&quot;</span>,
			<span style="color: #000000; font-weight: bold;">new</span> Video<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Dr House (Docteur House)&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> Video<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;True Blood&quot;</span><span style="color: #009900;">&#41;</span>,
			<span style="color: #000000; font-weight: bold;">new</span> Video<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Smallville&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> Video<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sanctuary&quot;</span><span style="color: #009900;">&#41;</span>,
			<span style="color: #000000; font-weight: bold;">new</span> Video<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Desperate Housewives&quot;</span><span style="color: #009900;">&#41;</span>,
			<span style="color: #000000; font-weight: bold;">new</span> Video<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Spartacus: Blood and Sand&quot;</span><span style="color: #009900;">&#41;</span>,
			<span style="color: #000000; font-weight: bold;">new</span> Video<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Lost, les disparus&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> Video<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Stargate Universe&quot;</span><span style="color: #009900;">&#41;</span>,
			<span style="color: #000000; font-weight: bold;">new</span> Video<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;How I Met Your Mother&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		setListAdapter<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> VideoAdapter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> VideoAdapter <span style="color: #000000; font-weight: bold;">extends</span> BaseAdapter <span style="color: #009900;">&#123;</span>
&nbsp;
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> OBJECTS.<span style="color: #006633;">length</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> getItem<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> position<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> OBJECTS<span style="color: #009900;">&#91;</span>position<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> getItemId<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> position<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> position<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getViewTypeCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> ITEM_VIEW_TYPE_COUNT<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getItemViewType<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> position<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>OBJECTS<span style="color: #009900;">&#91;</span>position<span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> ITEM_VIEW_TYPE_SEPARATOR
					<span style="color: #339933;">:</span> ITEM_VIEW_TYPE_VIDEO<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isEnabled<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> position<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// A separator cannot be clicked !</span>
			<span style="color: #000000; font-weight: bold;">return</span> getItemViewType<span style="color: #009900;">&#40;</span>position<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> ITEM_VIEW_TYPE_SEPARATOR<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aview+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">View</span></a> getView<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> position, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aview+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">View</span></a> convertView, ViewGroup parent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> type <span style="color: #339933;">=</span> getItemViewType<span style="color: #009900;">&#40;</span>position<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// First, let's create a new convertView if needed. You can also</span>
			<span style="color: #666666; font-style: italic;">// create a ViewHolder to speed up changes if you want ;)</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>convertView <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				convertView <span style="color: #339933;">=</span> LayoutInflater
						.<span style="color: #006633;">from</span><span style="color: #009900;">&#40;</span>MainListActivity.<span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>
						.<span style="color: #006633;">inflate</span><span style="color: #009900;">&#40;</span>
								type <span style="color: #339933;">==</span> ITEM_VIEW_TYPE_SEPARATOR <span style="color: #339933;">?</span> R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">separator_list_item</span>
										<span style="color: #339933;">:</span> R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">video_list_item</span>, parent,
								<span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// We can now fill the list item view with the appropriate data.</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>type <span style="color: #339933;">==</span> ITEM_VIEW_TYPE_SEPARATOR<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> convertView<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#41;</span> getItem<span style="color: #009900;">&#40;</span>position<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">final</span> Video video <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Video<span style="color: #009900;">&#41;</span> getItem<span style="color: #009900;">&#40;</span>position<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> convertView.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">title</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
						.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>video.<span style="color: #006633;">title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> convertView.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">description</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
						.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>video.<span style="color: #006633;">description</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">return</span> convertView<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Les différences majeures entre la version présentée ci-dessus et une liste &#8220;classique&#8221; se situe au niveau des méthodes :</p>
<ul>
<li><code>getViewTypeCount()</code> : Implémentée par l&#8217;<code>Adapter</code> afin d&#8217;informer la <code>ListView</code> du nombre total de types de vues. Dans notre cas, nous avons bien 2 types de vues : les vues séparateur et les vues &#8220;vidéos&#8221;</li>
<li><code>getItemViewType(int position)</code> : Retourne le type de la vue à la position <em>position</em>. Le type d&#8217;une vue est représenté par un entier compris entre 0 (inclus) et <code>getItemViewCount()</code> (exclus).</li>
</ul>
<p>Le code de <code>getView</code> ressemble fortement à l&#8217;implémentation habituelle :</p>
<ul>
<li>Si <code>convertView</code> est nulle, il faut créer la vue adaptée</li>
<li>On applique la donnée à la cellule</li>
<li>On retourne la cellule</li>
</ul>
<p>Cette facilité d&#8217;utilisation est une conséquence directe de l&#8217;utilisation de type d&#8217;items. En effet, il n&#8217;est pas nécessaire de vérifier le type de la convertView pour savoir si cette dernière est une vue &#8220;séparateur&#8221; ou une vue &#8220;vidéos&#8221;. La <code>ListView</code> s&#8217;assure, elle même, du type de la convertView.</p>
<p>Il existe d&#8217;autres techniques permettant d&#8217;inclure des séparateurs dans vos listes comme l&#8217;inclusion d&#8217;un entête à l&#8217;ensemble des cellules (entête dont le flag de <code>visibility</code> est mis à <code>View.GONE</code> lorsque la vue n&#8217;est pas en début de section) mais celle présentée ici à l&#8217;avantage d&#8217;être très proche de l&#8217;utilisation classique des <code>ListView</code>. Avec cette nouvelle astuce dans votre sac, vous allez maintenant pouvoir présenter l&#8217;information aux utilisateurs de la façon la plus ergonomique et <em>user-friendly</em> possible !</p>
]]></content:encoded>
			<wfw:commentRss>http://android.cyrilmottier.com/?feed=rss2&amp;p=232</wfw:commentRss>
		</item>
	</channel>
</rss>
