<?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>Androgames blog</title>
	<atom:link href="http://blog.androgames.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.androgames.net</link>
	<description>Android Development &#38; Game</description>
	<pubDate>Sat, 24 Jul 2010 06:01:26 +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>Android TaskKiller application</title>
		<link>http://blog.androgames.net/229/android-taskkiller-application/</link>
		<comments>http://blog.androgames.net/229/android-taskkiller-application/#comments</comments>
		<pubDate>Sat, 24 Jul 2010 06:01:26 +0000</pubDate>
		<dc:creator>Tonio</dc:creator>
		
		<category><![CDATA[Application]]></category>

		<category><![CDATA[taskKiller]]></category>

		<category><![CDATA[tool]]></category>

		<guid isPermaLink="false">http://blog.androgames.net/?p=229</guid>
		<description><![CDATA[TaskKiller is an task manager designed to help you to increase the battery life of your phone by killing unused processes. Why is this usefull ? When you start an application on your phone, informations used by the this application process will be kept in memory, even after you quit the application. The Android system [...]]]></description>
			<content:encoded><![CDATA[<p>TaskKiller is an task manager designed to help you to increase the battery life of your phone by killing unused processes. Why is this usefull ? When you start an application on your phone, informations used by the this application process will be kept in memory, even after you quit the application. The Android system will also start diferent services depending on random events and notifications. As a result, your phone memory is full of idle process, which has the effect of making it slow and consume battery. TaskKiller makes it possible to kill those idle process both on demand and automatically. There are four functionalities you can configure to optimize the battery life of your phone :</p>
<h3>TaskKiller Application</h3>
<p>The application allow you to kill running tasks on demand. It is the entry point to configure TaskKiller functionalities and to manage black lists of applications you don&#8217;t want to kill. TaskKiller also has a functionality to bulk uninstall applications from your phone in only one click. This is usefull to remove permanently the applications you don&#8217;t use anymore.</p>
<div style="text-align:center"><img src="http://blog.androgames.net/wp-content/uploads/2010/07/taskkiller2.png" alt="taskkiller2" title="taskkiller2" width="320" height="480" class="alignnone size-full wp-image-235" /><br />
<img src="http://blog.androgames.net/wp-content/uploads/2010/07/taskkiller3.png" alt="taskkiller3" title="taskkiller3" width="320" height="480" class="alignnone size-full wp-image-236" /><br />
<img src="http://blog.androgames.net/wp-content/uploads/2010/07/taskkiller4.png" alt="taskkiller4" title="taskkiller4" width="320" height="480" class="alignnone size-full wp-image-236" /></div>
<h3>TaskKiller Widget</h3>
<p>The TaskKiller widget can be used to kill running task directly from your phone home screen at any time. It will notify you with the number of tasks it has killed and the resulting available memory on your phone. Use it to free memory before starting an application that will run faster with more memory : 3D games, web browsing, video, &#8230;</p>
<div style="text-align:center"><img src="http://blog.androgames.net/wp-content/uploads/2010/07/taskkiller1.png" alt="taskkiller1" title="taskkiller1" width="320" height="480" class="alignnone size-full wp-image-234" /></div>
<h3>TaskKiller Auto kill</h3>
<p>When running, the TaskKiller auto kill feature will kill silently unused tasks at a fixed frequency. You can start and stop the service from the TaskKiller application settings. Once started, the service will remain running until you stop it. It will also be restarted at phone boot it it was running when the phone turns off. The auto kill functionality is particulary usefull for killing useless services started by the Android system when your phone is idle. Your phone battery will no more be over solicited when your phone is in standby. The auto kill functionality will not kill the application you are currently using on your phone.</p>
<h3>TaskKiller Kill on pause</h3>
<p>When your phone screen is turned off, keeping processes in memory is useless. The TaskKiller kill on pause functionality will kill unused tasks automatically each time your phone screen is turned off. Combined with the auto kill functionality, it will optimize your phone battery life. If your phone is turned off with an application running in foreground, this application will not be kill by the kill on pause feature.<br />
<br/><br/></p>
<div style="text-align:center">
Start saving battery now !<br />
Dowload TaskKiller on the Android Market.<br />
Search for &#8220;TaskKiller&#8221;.</p>
<p><img class="size-full wp-image-166" src="http://chart.apis.google.com/chart?cht=qr&amp;chs=135x135&amp;chl=market://search?q=pname:net.androgames.taskkiller" alt="" /></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.androgames.net/229/android-taskkiller-application/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Android custom Dialog</title>
		<link>http://blog.androgames.net/10/custom-android-dialog/</link>
		<comments>http://blog.androgames.net/10/custom-android-dialog/#comments</comments>
		<pubDate>Mon, 10 May 2010 08:44:24 +0000</pubDate>
		<dc:creator>Tonio</dc:creator>
		
		<category><![CDATA[Tutorial]]></category>

		<category><![CDATA[dialog]]></category>

		<category><![CDATA[style]]></category>

		<category><![CDATA[theme]]></category>

		<guid isPermaLink="false">http://blog.androgames.net/?p=10</guid>
		<description><![CDATA[Android provides a AlertDialog class that ease the building of Dialog windows throught it&#8217;s Builder inner class. However, it&#8217;s not possible to customize these AlertDialog windows. To give AlertDialog windows a custom Look and Feel, a solution is to create application specific AlertDialog and AlertDialog.Builder class.

Defining the Look And Feel
What we want is to turn [...]]]></description>
			<content:encoded><![CDATA[<p>Android provides a <a href="http://developer.android.com/reference/android/app/AlertDialog.html">AlertDialog</a> class that ease the building of <a href="http://developer.android.com/reference/android/app/Dialog.html">Dialog</a> windows throught it&#8217;s <a href="http://developer.android.com/reference/android/app/AlertDialog.Builder.html">Builder</a> inner class. However, it&#8217;s not possible to customize these AlertDialog windows. To give AlertDialog windows a custom Look and Feel, a solution is to create application specific AlertDialog and AlertDialog.Builder class.</p>
<p><img src="http://blog.androgames.net/wp-content/uploads/2010/05/default.png" alt="Android default Dialog" title="Android default Dialog" width="294" height="170" class="alignnone size-full wp-image-206" /></p>
<h3>Defining the Look And Feel</h3>
<p>What we want is to turn the default AlertDialog Look and Feel into this custom Look and Feel :</p>
<p><img src="http://blog.androgames.net/wp-content/uploads/2010/05/custom.png" alt="Custom Android Dialog" title="Custom Android Dialog" width="293" height="142" class="alignnone size-full wp-image-203" /></p>
<p>The dialog will support the following features :</p>
<ol>
<li>Specify title from resource or String</li>
<li>Specify content from resource, String or custom layout</li>
<li>Set positive and negative buttons and associated listeners</li>
</ol>
<h3>Writing layout, style and theme</h3>
<p>The dialog will use a custom layout to render it&#8217;s content. The layout defines the ids that will be used to access the title TextView, the dialog message or custom content and the buttons bar.</p>

<div class="wp_syntax"><div class="code"><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;LinearLayout</span> <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:orientation</span>=<span style="color: #ff0000;">&quot;vertical&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:minWidth</span>=<span style="color: #ff0000;">&quot;280dip&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;LinearLayout</span> </span>
<span style="color: #009900;">        <span style="color: #000066;">android:orientation</span>=<span style="color: #ff0000;">&quot;vertical&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:background</span>=<span style="color: #ff0000;">&quot;@drawable/header&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;TextView</span></span>
<span style="color: #009900;">            <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;@style/DialogText.Title&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/title&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:paddingRight</span>=<span style="color: #ff0000;">&quot;8dip&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:paddingLeft</span>=<span style="color: #ff0000;">&quot;8dip&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:background</span>=<span style="color: #ff0000;">&quot;@drawable/title&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;wrap_content&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;/LinearLayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;LinearLayout</span> </span>
<span style="color: #009900;">        <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/content&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:orientation</span>=<span style="color: #ff0000;">&quot;vertical&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:background</span>=<span style="color: #ff0000;">&quot;@drawable/center&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;TextView</span></span>
<span style="color: #009900;">            <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;@style/DialogText&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/message&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:padding</span>=<span style="color: #ff0000;">&quot;5dip&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;/LinearLayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;LinearLayout</span> </span>
<span style="color: #009900;">        <span style="color: #000066;">android:orientation</span>=<span style="color: #ff0000;">&quot;horizontal&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:background</span>=<span style="color: #ff0000;">&quot;@drawable/footer&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;Button</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/positiveButton&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:layout_marginTop</span>=<span style="color: #ff0000;">&quot;3dip&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;0dip&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:layout_weight</span>=<span style="color: #ff0000;">&quot;1&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>
<span style="color: #009900;">            <span style="color: #000066;">android:singleLine</span>=<span style="color: #ff0000;">&quot;true&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;Button</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/negativeButton&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:layout_marginTop</span>=<span style="color: #ff0000;">&quot;3dip&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;0dip&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:layout_weight</span>=<span style="color: #ff0000;">&quot;1&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>
<span style="color: #009900;">            <span style="color: #000066;">android:singleLine</span>=<span style="color: #ff0000;">&quot;true&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;/LinearLayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/LinearLayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The root LinearLayout&#8217;s width is set to <strong>fill_parent</strong> with a minimum of <strong>280dip</strong> so that the dialog width will always be 87,5% of the screen width.</p>
<p>A custom Theme should be used to declare the dialog as floating and using a custom background and a custom title view :</p>

<div class="wp_syntax"><div class="code"><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>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;style</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Dialog&quot;</span> <span style="color: #000066;">parent</span>=<span style="color: #ff0000;">&quot;android:style/Theme.Dialog&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;">name</span>=<span style="color: #ff0000;">&quot;android:windowBackground&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>@null<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;">name</span>=<span style="color: #ff0000;">&quot;android:windowNoTitle&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>true<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;">name</span>=<span style="color: #ff0000;">&quot;android:windowIsFloating&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>true<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;/style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<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></div></div>

<p>Then we need to define the appearence of our dialog title and message :</p>

<div class="wp_syntax"><div class="code"><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>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;style</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;DialogText&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;">name</span>=<span style="color: #ff0000;">&quot;android:textColor&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>#FF000000<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;">name</span>=<span style="color: #ff0000;">&quot;android:textSize&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>12sp<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;/style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;style</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;DialogText.Title&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;">name</span>=<span style="color: #ff0000;">&quot;android:textSize&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>16sp<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;">name</span>=<span style="color: #ff0000;">&quot;android:textStyle&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>bold<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;/style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<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></div></div>

<h3>Writing the Dialog and Builder class</h3>
<p>It&#8217;s preferable for our custom Builder to have the same methods that we have in the <a href="http://developer.android.com/reference/android/app/AlertDialog.Builder.html">AletDialog.Builder</a> class.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">net.androgames.blog.sample.customdialog.dialog</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">net.androgames.blog.sample.customdialog.R</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Dialog</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Context</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.DialogInterface</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.LayoutParams</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.Button</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.LinearLayout</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: #008000; font-style: italic; font-weight: bold;">/**
 * 
 * Create custom Dialog windows for your application
 * Custom dialogs rely on custom layouts wich allow you to 
 * create and use your own look &amp; feel.
 * 
 * Under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html
 * 
 * @author antoine vianey
 *
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CustomDialog <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Dialog</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> CustomDialog<span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span> context, <span style="color: #000066; font-weight: bold;">int</span> theme<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>context, theme<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> CustomDialog<span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span> context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Helper class for creating a custom dialog
     */</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;">class</span> Builder <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Context</span> context<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> title<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> message<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> positiveButtonText<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> negativeButtonText<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">View</span> contentView<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">private</span> DialogInterface.<span style="color: #006633;">OnClickListener</span> 
                        positiveButtonClickListener,
                        negativeButtonClickListener<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> Builder<span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span> context<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;">context</span> <span style="color: #339933;">=</span> context<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #008000; font-style: italic; font-weight: bold;">/**
         * Set the Dialog message from String
         * @param title
         * @return
         */</span>
        <span style="color: #000000; font-weight: bold;">public</span> Builder setMessage<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> message<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;">message</span> <span style="color: #339933;">=</span> message<span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #008000; font-style: italic; font-weight: bold;">/**
         * Set the Dialog message from resource
         * @param title
         * @return
         */</span>
        <span style="color: #000000; font-weight: bold;">public</span> Builder setMessage<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> message<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;">message</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> context.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #008000; font-style: italic; font-weight: bold;">/**
         * Set the Dialog title from resource
         * @param title
         * @return
         */</span>
        <span style="color: #000000; font-weight: bold;">public</span> Builder setTitle<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> 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: #006633;">title</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> context.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span>title<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #008000; font-style: italic; font-weight: bold;">/**
         * Set the Dialog title from String
         * @param title
         * @return
         */</span>
        <span style="color: #000000; font-weight: bold;">public</span> Builder setTitle<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> 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: #006633;">title</span> <span style="color: #339933;">=</span> title<span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #008000; font-style: italic; font-weight: bold;">/**
         * Set a custom content view for the Dialog.
         * If a message is set, the contentView is not
         * added to the Dialog...
         * @param v
         * @return
         */</span>
        <span style="color: #000000; font-weight: bold;">public</span> Builder setContentView<span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> v<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;">contentView</span> <span style="color: #339933;">=</span> v<span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #008000; font-style: italic; font-weight: bold;">/**
         * Set the positive button resource and it's listener
         * @param positiveButtonText
         * @param listener
         * @return
         */</span>
        <span style="color: #000000; font-weight: bold;">public</span> Builder setPositiveButton<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> positiveButtonText,
                DialogInterface.<span style="color: #006633;">OnClickListener</span> listener<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;">positiveButtonText</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> context
                    .<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span>positiveButtonText<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">positiveButtonClickListener</span> <span style="color: #339933;">=</span> listener<span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #008000; font-style: italic; font-weight: bold;">/**
         * Set the positive button text and it's listener
         * @param positiveButtonText
         * @param listener
         * @return
         */</span>
        <span style="color: #000000; font-weight: bold;">public</span> Builder setPositiveButton<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> positiveButtonText,
                DialogInterface.<span style="color: #006633;">OnClickListener</span> listener<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;">positiveButtonText</span> <span style="color: #339933;">=</span> positiveButtonText<span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">positiveButtonClickListener</span> <span style="color: #339933;">=</span> listener<span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #008000; font-style: italic; font-weight: bold;">/**
         * Set the negative button resource and it's listener
         * @param negativeButtonText
         * @param listener
         * @return
         */</span>
        <span style="color: #000000; font-weight: bold;">public</span> Builder setNegativeButton<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> negativeButtonText,
                DialogInterface.<span style="color: #006633;">OnClickListener</span> listener<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;">negativeButtonText</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> context
                    .<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span>negativeButtonText<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">negativeButtonClickListener</span> <span style="color: #339933;">=</span> listener<span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #008000; font-style: italic; font-weight: bold;">/**
         * Set the negative button text and it's listener
         * @param negativeButtonText
         * @param listener
         * @return
         */</span>
        <span style="color: #000000; font-weight: bold;">public</span> Builder setNegativeButton<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> negativeButtonText,
                DialogInterface.<span style="color: #006633;">OnClickListener</span> listener<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;">negativeButtonText</span> <span style="color: #339933;">=</span> negativeButtonText<span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">negativeButtonClickListener</span> <span style="color: #339933;">=</span> listener<span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #008000; font-style: italic; font-weight: bold;">/**
         * Create the custom dialog
         */</span>
        <span style="color: #000000; font-weight: bold;">public</span> CustomDialog create<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            LayoutInflater inflater <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>LayoutInflater<span style="color: #009900;">&#41;</span> context
                    .<span style="color: #006633;">getSystemService</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span>.<span style="color: #006633;">LAYOUT_INFLATER_SERVICE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// instantiate the dialog with the custom Theme</span>
            <span style="color: #000000; font-weight: bold;">final</span> CustomDialog dialog <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CustomDialog<span style="color: #009900;">&#40;</span>context, 
            		R.<span style="color: #006633;">style</span>.<span style="color: #003399;">Dialog</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">View</span> layout <span style="color: #339933;">=</span> inflater.<span style="color: #006633;">inflate</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">dialog</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            dialog.<span style="color: #006633;">addContentView</span><span style="color: #009900;">&#40;</span>layout, <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;">WRAP_CONTENT</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// set the dialog title</span>
            <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> layout.<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>title<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// set the confirm button</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>positiveButtonText <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>
                <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Button</span><span style="color: #009900;">&#41;</span> layout.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">positiveButton</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>positiveButtonText<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>positiveButtonClickListener <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>
                    <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Button</span><span style="color: #009900;">&#41;</span> layout.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">positiveButton</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                            .<span style="color: #006633;">setOnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">View</span>.<span style="color: #006633;">OnClickListener</span><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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                                    positiveButtonClickListener.<span style="color: #006633;">onClick</span><span style="color: #009900;">&#40;</span>
                                    		dialog, 
                                            DialogInterface.<span style="color: #006633;">BUTTON_POSITIVE</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: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</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: #666666; font-style: italic;">// if no confirm button just set the visibility to GONE</span>
                layout.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">positiveButton</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setVisibility</span><span style="color: #009900;">&#40;</span>
                        <span style="color: #003399;">View</span>.<span style="color: #006633;">GONE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #666666; font-style: italic;">// set the cancel button</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>negativeButtonText <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>
                <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Button</span><span style="color: #009900;">&#41;</span> layout.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">negativeButton</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>negativeButtonText<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>negativeButtonClickListener <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>
                    <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Button</span><span style="color: #009900;">&#41;</span> layout.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">negativeButton</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                            .<span style="color: #006633;">setOnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">View</span>.<span style="color: #006633;">OnClickListener</span><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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                                    positiveButtonClickListener.<span style="color: #006633;">onClick</span><span style="color: #009900;">&#40;</span>
                                    		dialog, 
                                            DialogInterface.<span style="color: #006633;">BUTTON_NEGATIVE</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: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</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: #666666; font-style: italic;">// if no confirm button just set the visibility to GONE</span>
                layout.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">negativeButton</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setVisibility</span><span style="color: #009900;">&#40;</span>
                        <span style="color: #003399;">View</span>.<span style="color: #006633;">GONE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #666666; font-style: italic;">// set the content message</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>message <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>
                <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> layout.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>
                		R.<span style="color: #006633;">id</span>.<span style="color: #006633;">message</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>message<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: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>contentView <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>
                <span style="color: #666666; font-style: italic;">// if no message set</span>
                <span style="color: #666666; font-style: italic;">// add the contentView to the dialog body</span>
                <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>LinearLayout<span style="color: #009900;">&#41;</span> layout.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">content</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">removeAllViews</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>LinearLayout<span style="color: #009900;">&#41;</span> layout.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">content</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>contentView, 
                                <span style="color: #000000; font-weight: bold;">new</span> LayoutParams<span style="color: #009900;">&#40;</span>
                                        LayoutParams.<span style="color: #006633;">WRAP_CONTENT</span>, 
                                        LayoutParams.<span style="color: #006633;">WRAP_CONTENT</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            dialog.<span style="color: #006633;">setContentView</span><span style="color: #009900;">&#40;</span>layout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> dialog<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></div></div>

<h3>Using the custom Builder</h3>
<p>Using the custom Builder is just as simple as using the default AlertDialog.Builder :</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Build the desired Dialog
 * CUSTOM or DEFAULT
 */</span>
@Override
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Dialog</span> onCreateDialog<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> dialogId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">Dialog</span> dialog <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>dialogId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">case</span> CUSTOM_DIALOG <span style="color: #339933;">:</span>
            CustomDialog.<span style="color: #006633;">Builder</span> customBuilder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span>
                CustomDialog.<span style="color: #006633;">Builder</span><span style="color: #009900;">&#40;</span>CustomDialogActivity.<span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            customBuilder.<span style="color: #006633;">setTitle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Custom title&quot;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">setMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Custom body&quot;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">setNegativeButton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cancel&quot;</span>, 
                        <span style="color: #000000; font-weight: bold;">new</span> DialogInterface.<span style="color: #006633;">OnClickListener</span><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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span>DialogInterface dialog, <span style="color: #000066; font-weight: bold;">int</span> which<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        CustomDialogActivity.<span style="color: #000000; font-weight: bold;">this</span>
                        .<span style="color: #006633;">dismissDialog</span><span style="color: #009900;">&#40;</span>CUSTOM_DIALOG<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;">&#41;</span>
                .<span style="color: #006633;">setPositiveButton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Confirm&quot;</span>, 
                        <span style="color: #000000; font-weight: bold;">new</span> DialogInterface.<span style="color: #006633;">OnClickListener</span><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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span>DialogInterface dialog, <span style="color: #000066; font-weight: bold;">int</span> which<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        dialog.<span style="color: #006633;">dismiss</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: #009900;">&#41;</span><span style="color: #339933;">;</span>
            dialog <span style="color: #339933;">=</span> customBuilder.<span style="color: #006633;">create</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;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">case</span> DEFAULT_DIALOG <span style="color: #339933;">:</span>
            AlertDialog.<span style="color: #006633;">Builder</span> alertBuilder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span>
                AlertDialog.<span style="color: #006633;">Builder</span><span style="color: #009900;">&#40;</span>CustomDialogActivity.<span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            alertBuilder.<span style="color: #006633;">setTitle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Default title&quot;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">setMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Default body&quot;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">setNegativeButton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cancel&quot;</span>, 
                		<span style="color: #000000; font-weight: bold;">new</span> DialogInterface.<span style="color: #006633;">OnClickListener</span><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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span>DialogInterface dialog, <span style="color: #000066; font-weight: bold;">int</span> which<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        dialog.<span style="color: #006633;">dismiss</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: #009900;">&#41;</span>
                .<span style="color: #006633;">setPositiveButton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Confirm&quot;</span>, 
                		<span style="color: #000000; font-weight: bold;">new</span> DialogInterface.<span style="color: #006633;">OnClickListener</span><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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span>DialogInterface dialog, <span style="color: #000066; font-weight: bold;">int</span> which<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        CustomDialogActivity.<span style="color: #000000; font-weight: bold;">this</span>
                        .<span style="color: #006633;">dismissDialog</span><span style="color: #009900;">&#40;</span>DEFAULT_DIALOG<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;">&#41;</span><span style="color: #339933;">;</span>
            dialog <span style="color: #339933;">=</span> alertBuilder.<span style="color: #006633;">create</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;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">return</span> dialog<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can browse the full source code of the Eclipse project here : <a href="http://code.google.com/p/androgames-sample/">SampleCustomDialog</a></p>
<p>Enjoy !</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.androgames.net/10/custom-android-dialog/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Android Bubble Level application</title>
		<link>http://blog.androgames.net/164/android-bubble-level-application/</link>
		<comments>http://blog.androgames.net/164/android-bubble-level-application/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 05:18:17 +0000</pubDate>
		<dc:creator>Tonio</dc:creator>
		
		<category><![CDATA[Application]]></category>

		<category><![CDATA[bubble level]]></category>

		<category><![CDATA[tool]]></category>

		<guid isPermaLink="false">http://blog.androgames.net/?p=164</guid>
		<description><![CDATA[Bubble level is a spirit level application for android. It turns your Android phone into a multidimensionnal spirit level. Hold any of the phone&#8217;s four sides against an object or surface to test it for level or plumb, or lay it down on an horizontal and flat surface for a 360° level.



Dowload it on the [...]]]></description>
			<content:encoded><![CDATA[<p>Bubble level is a <a href="http://en.wikipedia.org/wiki/Spirit_level">spirit level</a> application for android. It turns your Android phone into a multidimensionnal spirit level. Hold any of the phone&#8217;s four sides against an object or surface to test it for level or plumb, or lay it down on an horizontal and flat surface for a 360° level.</p>
<div style="text-align:center">
<img src="http://blog.androgames.net/wp-content/uploads/2010/03/bubble_level1.png" alt="bubble_level1" title="bubble_level1" width="480" height="320" class="alignnone size-full wp-image-183" /></p>
<p><img src="http://blog.androgames.net/wp-content/uploads/2010/03/getimage.jpg" alt="getimage" title="getimage" width="320" height="480" class="alignnone size-full wp-image-184" /></p>
<p>Dowload it on the Android Market.<br />
Search for &#8220;Bubble Level&#8221;.</p>
<p><img class="size-full wp-image-166" src="http://chart.apis.google.com/chart?cht=qr&amp;chs=135x135&amp;chl=market://search?q=pname:net.androgames.level" alt="" /></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.androgames.net/164/android-bubble-level-application/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Android Orientation Sensor tutorial</title>
		<link>http://blog.androgames.net/135/android-orientation-tutorial/</link>
		<comments>http://blog.androgames.net/135/android-orientation-tutorial/#comments</comments>
		<pubDate>Sun, 07 Mar 2010 19:20:51 +0000</pubDate>
		<dc:creator>Tonio</dc:creator>
		
		<category><![CDATA[Tutorial]]></category>

		<category><![CDATA[orientation]]></category>

		<category><![CDATA[sensor]]></category>

		<guid isPermaLink="false">http://blog.androgames.net/?p=135</guid>
		<description><![CDATA[Android phones includes an orientation sensor that is used to detect the orientation of the phone in space. The orientation is given throught three values :

Azimtuh in degresangle between the x axis of the phone and the north direction0 &#8804; azimuth &#8804; 360
Pitch in degresangle made by the y axis of the phone relatively to [...]]]></description>
			<content:encoded><![CDATA[<p>Android phones includes an orientation sensor that is used to detect the orientation of the phone in space. The orientation is given throught three values :</p>
<ol>
<li><strong>Azimtuh</strong> in degres<br/>angle between the x axis of the phone and the north direction<br/>0 &le; azimuth &le; 360</li>
<li><strong>Pitch</strong> in degres<br/>angle made by the y axis of the phone relatively to the phone&#8217;s horizontal position<br/>-180 &le; pitch &le; 180</li>
<li><strong>Roll</strong> in degres<br/>angle made by the x axis of the phone relatively to the phone&#8217;s horizontal position<br/>-90 &le; roll &le; 90</li>
</ol>
<p>In this tutorial, we&#8217;ll see how to use the orientation sensor in an application in order to capture orientation change events. We&#8217;ll use these events to build a custom <strong>listener</strong> that will trigger phone&#8217;s orientation change events :</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">net.androgames.blog.sample.orientation</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> OrientationListener <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onOrientationChanged<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span> azimuth, 
            <span style="color: #000066; font-weight: bold;">float</span> pitch, <span style="color: #000066; font-weight: bold;">float</span> roll<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Top side of the phone is up
     * The phone is standing on its bottom side
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onTopUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Bottom side of the phone is up
     * The phone is standing on its top side
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onBottomUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Right side of the phone is up
     * The phone is standing on its left side
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onRightUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Left side of the phone is up
     * The phone is standing on its right side
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onLeftUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>An instance of the <a href="http://developer.android.com/reference/android/hardware/SensorManager.html">SensorManager</a> is needed in order to retrieve informations about the supported sensors. No permission is required to access the sensor service and to retrieve the list of available orientation sensors given by the <strong>Sensor.TYPE_ORIENTATION</strong> constant. If at least one <a href="http://developer.android.com/reference/android/hardware/Sensor.html">Sensor</a> exists, it is possible to register a <a href="http://developer.android.com/reference/android/hardware/SensorEventListener.html">SensorEventListener</a> for a <a href="http://developer.android.com/reference/android/hardware/Sensor.html">Sensor</a> of the list. The delivering rate for the orientation sensor events can be specified and must be one of these :</p>
<ol>
<li>SensorManager.SENSOR_DELAY_FASTEST : as fast as possible</li>
<li>SensorManager.SENSOR_DELAY_GAME : rate suitable for game</li>
<li>SensorManager.SENSOR_DELAY_NORMAL : normal rate</li>
<li>SensorManager.SENSOR_DELAY_UI : rate suitable for UI Thread</li>
</ol>
<p>The custom orientation manager is as follow :</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">net.androgames.blog.sample.orientation</span><span style="color: #339933;">;</span>
&nbsp;
<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.content.Context</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.hardware.Sensor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.hardware.SensorEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.hardware.SensorEventListener</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.hardware.SensorManager</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Android Orientation Sensor Manager Archetype
 * @author antoine vianey
 * under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> OrientationManager <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> Sensor sensor<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> SensorManager sensorManager<span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// you could use an OrientationListener array instead</span>
    <span style="color: #666666; font-style: italic;">// if you plans to use more than one listener</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> OrientationListener listener<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/** indicates whether or not Orientation Sensor is supported */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Boolean</span> supported<span style="color: #339933;">;</span>
    <span style="color: #008000; font-style: italic; font-weight: bold;">/** indicates whether or not Orientation Sensor is running */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">boolean</span> running <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/** Sides of the phone */</span>
    <span style="color: #000000; font-weight: bold;">enum</span> Side <span style="color: #009900;">&#123;</span>
        TOP,
        BOTTOM,
        LEFT,
        RIGHT<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Returns true if the manager is listening to orientation changes
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">boolean</span> isListening<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> running<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Unregisters listeners
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> stopListening<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        running <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sensorManager <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> sensorEventListener <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>
                sensorManager.<span style="color: #006633;">unregisterListener</span><span style="color: #009900;">&#40;</span>sensorEventListener<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: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Returns true if at least one Orientation sensor is available
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">boolean</span> isSupported<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;">if</span> <span style="color: #009900;">&#40;</span>supported <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>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Orientation.<span style="color: #006633;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <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>
                sensorManager <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>SensorManager<span style="color: #009900;">&#41;</span> Orientation.<span style="color: #006633;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">getSystemService</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span>.<span style="color: #006633;">SENSOR_SERVICE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                List<span style="color: #339933;">&lt;</span>Sensor<span style="color: #339933;">&gt;</span> sensors <span style="color: #339933;">=</span> sensorManager.<span style="color: #006633;">getSensorList</span><span style="color: #009900;">&#40;</span>
                        Sensor.<span style="color: #006633;">TYPE_ORIENTATION</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                supported <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Boolean</span><span style="color: #009900;">&#40;</span>sensors.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</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>
                supported <span style="color: #339933;">=</span> <span style="color: #003399;">Boolean</span>.<span style="color: #000066; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> supported<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Registers a listener and start listening
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> startListening<span style="color: #009900;">&#40;</span>
            OrientationListener orientationListener<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        sensorManager <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>SensorManager<span style="color: #009900;">&#41;</span> Orientation.<span style="color: #006633;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">getSystemService</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span>.<span style="color: #006633;">SENSOR_SERVICE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        List<span style="color: #339933;">&lt;</span>Sensor<span style="color: #339933;">&gt;</span> sensors <span style="color: #339933;">=</span> sensorManager.<span style="color: #006633;">getSensorList</span><span style="color: #009900;">&#40;</span>
                Sensor.<span style="color: #006633;">TYPE_ORIENTATION</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sensors.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            sensor <span style="color: #339933;">=</span> sensors.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            running <span style="color: #339933;">=</span> sensorManager.<span style="color: #006633;">registerListener</span><span style="color: #009900;">&#40;</span>
                    sensorEventListener, sensor, 
                    SensorManager.<span style="color: #006633;">SENSOR_DELAY_NORMAL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            listener <span style="color: #339933;">=</span> orientationListener<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * The listener that listen to events from the orientation listener
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> SensorEventListener sensorEventListener <span style="color: #339933;">=</span> 
        <span style="color: #000000; font-weight: bold;">new</span> SensorEventListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #008000; font-style: italic; font-weight: bold;">/** The side that is currently up */</span>
        <span style="color: #000000; font-weight: bold;">private</span> Side currentSide <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">private</span> Side oldSide <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span> azimuth<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span> pitch<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span> roll<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onAccuracyChanged<span style="color: #009900;">&#40;</span>Sensor sensor, <span style="color: #000066; font-weight: bold;">int</span> accuracy<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onSensorChanged<span style="color: #009900;">&#40;</span>SensorEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            azimuth <span style="color: #339933;">=</span> event.<span style="color: #006633;">values</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>     <span style="color: #666666; font-style: italic;">// azimuth</span>
            pitch <span style="color: #339933;">=</span> event.<span style="color: #006633;">values</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>     <span style="color: #666666; font-style: italic;">// pitch</span>
            roll <span style="color: #339933;">=</span> event.<span style="color: #006633;">values</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>        <span style="color: #666666; font-style: italic;">// roll</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>pitch <span style="color: #339933;">&lt;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">45</span> <span style="color: #339933;">&amp;&amp;</span> pitch <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">135</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// top side up</span>
                currentSide <span style="color: #339933;">=</span> Side.<span style="color: #006633;">TOP</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>pitch <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">45</span> <span style="color: #339933;">&amp;&amp;</span> pitch <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">135</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// bottom side up</span>
                currentSide <span style="color: #339933;">=</span> Side.<span style="color: #006633;">BOTTOM</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>roll <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">45</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// right side up</span>
                currentSide <span style="color: #339933;">=</span> Side.<span style="color: #006633;">RIGHT</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>roll <span style="color: #339933;">&lt;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">45</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// left side up</span>
                currentSide <span style="color: #339933;">=</span> Side.<span style="color: #006633;">LEFT</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>currentSide <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>currentSide.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>oldSide<span style="color: #009900;">&#41;</span><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>currentSide<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000000; font-weight: bold;">case</span> TOP <span style="color: #339933;">:</span> 
                        listener.<span style="color: #006633;">onTopUp</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;">break</span><span style="color: #339933;">;</span>
                    <span style="color: #000000; font-weight: bold;">case</span> BOTTOM <span style="color: #339933;">:</span> 
                        listener.<span style="color: #006633;">onBottomUp</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;">break</span><span style="color: #339933;">;</span>
                    <span style="color: #000000; font-weight: bold;">case</span> LEFT<span style="color: #339933;">:</span> 
                        listener.<span style="color: #006633;">onLeftUp</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;">break</span><span style="color: #339933;">;</span>
                    <span style="color: #000000; font-weight: bold;">case</span> RIGHT<span style="color: #339933;">:</span> 
                        listener.<span style="color: #006633;">onRightUp</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;">break</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                oldSide <span style="color: #339933;">=</span> currentSide<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// forwards orientation to the OrientationListener</span>
            listener.<span style="color: #006633;">onOrientationChanged</span><span style="color: #009900;">&#40;</span>azimuth, pitch, roll<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The custom <strong>OrientationManager</strong> can be use in any <a href="http://developer.android.com/reference/android/app/Activity.html">Activity</a> or <a href="http://developer.android.com/reference/android/app/Service.html">Service</a> :</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">net.androgames.blog.sample.orientation</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.Context</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: #008000; font-style: italic; font-weight: bold;">/**
 * Android orientation sensor tutorial
 * @author antoine vianey
 * under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Orientation <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #000000; font-weight: bold;">implements</span> OrientationListener <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: #003399;">Context</span> CONTEXT<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>
        CONTEXT <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onResume<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;">super</span>.<span style="color: #006633;">onResume</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;">if</span> <span style="color: #009900;">&#40;</span>OrientationManager.<span style="color: #006633;">isSupported</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            OrientationManager.<span style="color: #006633;">startListening</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</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: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onDestroy<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;">super</span>.<span style="color: #006633;">onDestroy</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;">if</span> <span style="color: #009900;">&#40;</span>OrientationManager.<span style="color: #006633;">isListening</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            OrientationManager.<span style="color: #006633;">stopListening</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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Context</span> getContext<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> CONTEXT<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;">void</span> onOrientationChanged<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span> azimuth, 
            <span style="color: #000066; font-weight: bold;">float</span> pitch, <span style="color: #000066; font-weight: bold;">float</span> roll<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> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">azimuth</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: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>azimuth<span style="color: #009900;">&#41;</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;">pitch</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: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>pitch<span style="color: #009900;">&#41;</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;">roll</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: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>roll<span style="color: #009900;">&#41;</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;">void</span> onBottomUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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;Bottom UP&quot;</span>, <span style="color: #cc66cc;">1000</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: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onLeftUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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;Left UP&quot;</span>, <span style="color: #cc66cc;">1000</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: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onRightUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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;Right UP&quot;</span>, <span style="color: #cc66cc;">1000</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: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onTopUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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;Top UP&quot;</span>, <span style="color: #cc66cc;">1000</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: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As usual, it is preferable to register listeners in the <strong>onResume()</strong> method of the <a href="http://developer.android.com/reference/android/app/Activity.html">Activity</a> and removed in the <strong>onFinish()</strong> method of the <a href="http://developer.android.com/reference/android/app/Activity.html">Activity</a>.</p>
<p>You can browse the full source code of the Eclipse project here : <a href="http://code.google.com/p/androgames-sample/">SampleOrientationSensor</a></p>
<p>Enjoy !</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.androgames.net/135/android-orientation-tutorial/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Android Accelerometer Sensor tutorial</title>
		<link>http://blog.androgames.net/85/android-accelerometer-tutorial/</link>
		<comments>http://blog.androgames.net/85/android-accelerometer-tutorial/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 12:00:16 +0000</pubDate>
		<dc:creator>Tonio</dc:creator>
		
		<category><![CDATA[Tutorial]]></category>

		<category><![CDATA[accelerometer]]></category>

		<category><![CDATA[gesture]]></category>

		<category><![CDATA[sensor]]></category>

		<category><![CDATA[shake]]></category>

		<guid isPermaLink="false">http://blog.androgames.net/?p=85</guid>
		<description><![CDATA[Android supports a wide variety of sensors that can be used by applications to capture informations about the phone&#8217;s environment.
In this tutorial, we&#8217;ll see how to use the accelerometer sensor in an application in order to capture acceleration change events. We&#8217;ll use these events to build a custom listener that will trigger others events such [...]]]></description>
			<content:encoded><![CDATA[<p>Android supports a wide variety of sensors that can be used by applications to capture informations about the phone&#8217;s environment.</p>
<p>In this tutorial, we&#8217;ll see how to use the accelerometer sensor in an application in order to capture acceleration change events. We&#8217;ll use these events to build a custom <strong>listener</strong> that will trigger others events such as shake events :</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">net.androgames.bolg.sample.accelerometer</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> AccelerometerListener <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onAccelerationChanged<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span> x, <span style="color: #000066; font-weight: bold;">float</span> y, <span style="color: #000066; font-weight: bold;">float</span> z<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onShake<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span> force<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>An instance of the <a href="http://developer.android.com/reference/android/hardware/SensorManager.html">SensorManager</a> is required in order to retrieve informations about the supported sensors. No permission is required to access the sensor service. It is then possible to retrieve the list of available sensors of a certain type. For an accelerometer sensor, the type to use is given by the <strong>Sensor.TYPE_ACCELEROMETER</strong> constant. If at least one <a href="http://developer.android.com/reference/android/hardware/Sensor.html">Sensor</a> exists, it is possible to register a <a href="http://developer.android.com/reference/android/hardware/SensorEventListener.html">SensorEventListener</a> for a <a href="http://developer.android.com/reference/android/hardware/Sensor.html">Sensor</a> of the list. It is possible to specify the delivering rate for sensor events. Specified rate must be one of :</p>
<ol>
<li>SensorManager.SENSOR_DELAY_FASTEST : as fast as possible</li>
<li>SensorManager.SENSOR_DELAY_GAME : rate suitable for game</li>
<li>SensorManager.SENSOR_DELAY_NORMAL : normal rate</li>
<li>SensorManager.SENSOR_DELAY_UI : rate suitable for UI Thread</li>
</ol>
<p>In order to allow a correct shake detection, it is preferable to use at least <strong>SensorManager.SENSOR_DELAY_GAME</strong>.</p>
<p>The custom accelerometer manager is as follow :</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">net.androgames.bolg.sample.accelerometer</span><span style="color: #339933;">;</span>
&nbsp;
<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.content.Context</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.hardware.Sensor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.hardware.SensorEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.hardware.SensorEventListener</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.hardware.SensorManager</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Android Accelerometer Sensor Manager Archetype
 * @author antoine vianey
 * under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AccelerometerManager <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/** Accuracy configuration */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">float</span> threshold     <span style="color: #339933;">=</span> 0.2f<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: #000066; font-weight: bold;">int</span> interval     <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1000</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> Sensor sensor<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> SensorManager sensorManager<span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// you could use an OrientationListener array instead</span>
    <span style="color: #666666; font-style: italic;">// if you plans to use more than one listener</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> AccelerometerListener listener<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/** indicates whether or not Accelerometer Sensor is supported */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Boolean</span> supported<span style="color: #339933;">;</span>
    <span style="color: #008000; font-style: italic; font-weight: bold;">/** indicates whether or not Accelerometer Sensor is running */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">boolean</span> running <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Returns true if the manager is listening to orientation changes
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">boolean</span> isListening<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> running<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Unregisters listeners
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> stopListening<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        running <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sensorManager <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> sensorEventListener <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>
                sensorManager.<span style="color: #006633;">unregisterListener</span><span style="color: #009900;">&#40;</span>sensorEventListener<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: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Returns true if at least one Accelerometer sensor is available
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">boolean</span> isSupported<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;">if</span> <span style="color: #009900;">&#40;</span>supported <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>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Accelerometer.<span style="color: #006633;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <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>
                sensorManager <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>SensorManager<span style="color: #009900;">&#41;</span> Accelerometer.<span style="color: #006633;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.
                        <span style="color: #006633;">getSystemService</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span>.<span style="color: #006633;">SENSOR_SERVICE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                List<span style="color: #339933;">&lt;</span>Sensor<span style="color: #339933;">&gt;</span> sensors <span style="color: #339933;">=</span> sensorManager.<span style="color: #006633;">getSensorList</span><span style="color: #009900;">&#40;</span>
                        Sensor.<span style="color: #006633;">TYPE_ACCELEROMETER</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                supported <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Boolean</span><span style="color: #009900;">&#40;</span>sensors.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</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>
                supported <span style="color: #339933;">=</span> <span style="color: #003399;">Boolean</span>.<span style="color: #000066; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> supported<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Configure the listener for shaking
     * @param threshold
     *             minimum acceleration variation for considering shaking
     * @param interval
     *             minimum interval between to shake events
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> configure<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> threshold, <span style="color: #000066; font-weight: bold;">int</span> interval<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        AccelerometerManager.<span style="color: #006633;">threshold</span> <span style="color: #339933;">=</span> threshold<span style="color: #339933;">;</span>
        AccelerometerManager.<span style="color: #006633;">interval</span> <span style="color: #339933;">=</span> interval<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Registers a listener and start listening
     * @param accelerometerListener
     *             callback for accelerometer events
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> startListening<span style="color: #009900;">&#40;</span>
            AccelerometerListener accelerometerListener<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        sensorManager <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>SensorManager<span style="color: #009900;">&#41;</span> Accelerometer.<span style="color: #006633;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.
                <span style="color: #006633;">getSystemService</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span>.<span style="color: #006633;">SENSOR_SERVICE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        List<span style="color: #339933;">&lt;</span>Sensor<span style="color: #339933;">&gt;</span> sensors <span style="color: #339933;">=</span> sensorManager.<span style="color: #006633;">getSensorList</span><span style="color: #009900;">&#40;</span>
                Sensor.<span style="color: #006633;">TYPE_ACCELEROMETER</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sensors.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            sensor <span style="color: #339933;">=</span> sensors.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            running <span style="color: #339933;">=</span> sensorManager.<span style="color: #006633;">registerListener</span><span style="color: #009900;">&#40;</span>
                    sensorEventListener, sensor, 
                    SensorManager.<span style="color: #006633;">SENSOR_DELAY_GAME</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            listener <span style="color: #339933;">=</span> accelerometerListener<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Configures threshold and interval
     * And registers a listener and start listening
     * @param accelerometerListener
     *             callback for accelerometer events
     * @param threshold
     *             minimum acceleration variation for considering shaking
     * @param interval
     *             minimum interval between to shake events
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> startListening<span style="color: #009900;">&#40;</span>
            AccelerometerListener accelerometerListener, 
            <span style="color: #000066; font-weight: bold;">int</span> threshold, <span style="color: #000066; font-weight: bold;">int</span> interval<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        configure<span style="color: #009900;">&#40;</span>threshold, interval<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        startListening<span style="color: #009900;">&#40;</span>accelerometerListener<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * The listener that listen to events from the accelerometer listener
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> SensorEventListener sensorEventListener <span style="color: #339933;">=</span> 
        <span style="color: #000000; font-weight: bold;">new</span> SensorEventListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> now <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: #000066; font-weight: bold;">long</span> timeDiff <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: #000066; font-weight: bold;">long</span> lastUpdate <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: #000066; font-weight: bold;">long</span> lastShake <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span> x <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: #000066; font-weight: bold;">float</span> y <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: #000066; font-weight: bold;">float</span> z <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: #000066; font-weight: bold;">float</span> lastX <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: #000066; font-weight: bold;">float</span> lastY <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: #000066; font-weight: bold;">float</span> lastZ <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: #000066; font-weight: bold;">float</span> force <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onAccuracyChanged<span style="color: #009900;">&#40;</span>Sensor sensor, <span style="color: #000066; font-weight: bold;">int</span> accuracy<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onSensorChanged<span style="color: #009900;">&#40;</span>SensorEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// use the event timestamp as reference</span>
            <span style="color: #666666; font-style: italic;">// so the manager precision won't depends </span>
            <span style="color: #666666; font-style: italic;">// on the AccelerometerListener implementation</span>
            <span style="color: #666666; font-style: italic;">// processing time</span>
            now <span style="color: #339933;">=</span> event.<span style="color: #006633;">timestamp</span><span style="color: #339933;">;</span>
&nbsp;
            x <span style="color: #339933;">=</span> event.<span style="color: #006633;">values</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            y <span style="color: #339933;">=</span> event.<span style="color: #006633;">values</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            z <span style="color: #339933;">=</span> event.<span style="color: #006633;">values</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// if not interesting in shake events</span>
            <span style="color: #666666; font-style: italic;">// just remove the whole if then else bloc</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lastUpdate <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                lastUpdate <span style="color: #339933;">=</span> now<span style="color: #339933;">;</span>
                lastShake <span style="color: #339933;">=</span> now<span style="color: #339933;">;</span>
                lastX <span style="color: #339933;">=</span> x<span style="color: #339933;">;</span>
                lastY <span style="color: #339933;">=</span> y<span style="color: #339933;">;</span>
                lastZ <span style="color: #339933;">=</span> z<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>
                timeDiff <span style="color: #339933;">=</span> now <span style="color: #339933;">-</span> lastUpdate<span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>timeDiff <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    force <span style="color: #339933;">=</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">abs</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">+</span> y <span style="color: #339933;">+</span> z <span style="color: #339933;">-</span> lastX <span style="color: #339933;">-</span> lastY <span style="color: #339933;">-</span> lastZ<span style="color: #009900;">&#41;</span> 
                                <span style="color: #339933;">/</span> timeDiff<span style="color: #339933;">;</span>
                    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>force <span style="color: #339933;">&gt;</span> threshold<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>now <span style="color: #339933;">-</span> lastShake <span style="color: #339933;">&gt;=</span> interval<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                            <span style="color: #666666; font-style: italic;">// trigger shake event</span>
                            listener.<span style="color: #006633;">onShake</span><span style="color: #009900;">&#40;</span>force<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
                        lastShake <span style="color: #339933;">=</span> now<span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                    lastX <span style="color: #339933;">=</span> x<span style="color: #339933;">;</span>
                    lastY <span style="color: #339933;">=</span> y<span style="color: #339933;">;</span>
                    lastZ <span style="color: #339933;">=</span> z<span style="color: #339933;">;</span>
                    lastUpdate <span style="color: #339933;">=</span> now<span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #666666; font-style: italic;">// trigger change event</span>
            listener.<span style="color: #006633;">onAccelerationChanged</span><span style="color: #009900;">&#40;</span>x, y, z<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In the case of a <a href="http://developer.android.com/reference/android/hardware/SensorEvent.html">SensorEvent</a> triggered by a <a href="http://developer.android.com/reference/android/hardware/Sensor.html">Sensor</a> of type Sensor.TYPE_ACCELEROMETER, the event&#8217;s values represents the acceleration of the phone given by a vector in a cartesian coordinate system. Landing on a table, the values returned by the SensorEvent for the phone should be :</p>
<ol>
<li>0 m/s<sup>2</sup> along x axis</li>
<li>0 m/s<sup>2</sup> along y axis</li>
<li>9,80665 m/s<sup>2</sup> along z axis</li>
</ol>
<p>From an event to another, the coordinates of the acceleration vector are stored to detect suddent acceleration changes and to trigger a shake event when the threshold is reached. Others events could be implemented such as the detection of up and down gestures, circular gestures and lot more&#8230;</p>
<p>The custom <strong>AccelerometerManager</strong> can be use in any <a href="http://developer.android.com/reference/android/app/Activity.html">Activity</a> or <a href="http://developer.android.com/reference/android/app/Service.html">Service</a> :</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">net.androgames.bolg.sample.accelerometer</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.Context</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: #008000; font-style: italic; font-weight: bold;">/**
 * Android accelerometer sensor tutorial
 * @author antoine vianey
 * under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Accelerometer <span style="color: #000000; font-weight: bold;">extends</span> Activity 
        <span style="color: #000000; font-weight: bold;">implements</span> AccelerometerListener <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: #003399;">Context</span> CONTEXT<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/** Called when the activity is first created. */</span>
    @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>
        CONTEXT <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onResume<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;">super</span>.<span style="color: #006633;">onResume</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;">if</span> <span style="color: #009900;">&#40;</span>AccelerometerManager.<span style="color: #006633;">isSupported</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            AccelerometerManager.<span style="color: #006633;">startListening</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</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: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onDestroy<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;">super</span>.<span style="color: #006633;">onDestroy</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;">if</span> <span style="color: #009900;">&#40;</span>AccelerometerManager.<span style="color: #006633;">isListening</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            AccelerometerManager.<span style="color: #006633;">stopListening</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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Context</span> getContext<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> CONTEXT<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * onShake callback
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onShake<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span> force<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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;Phone shaked : &quot;</span> <span style="color: #339933;">+</span> force, <span style="color: #cc66cc;">1000</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: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * onAccelerationChanged callback
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onAccelerationChanged<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span> x, <span style="color: #000066; font-weight: bold;">float</span> y, <span style="color: #000066; font-weight: bold;">float</span> z<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> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">x</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: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</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;">y</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: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>y<span style="color: #009900;">&#41;</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;">z</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: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>z<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: #009900;">&#125;</span></pre></div></div>

<p>As usual, it is preferable to register listeners in the <strong>onResume()</strong> method of the <a href="http://developer.android.com/reference/android/app/Activity.html">Activity</a> and removed in the <strong>onFinish()</strong> method of the <a href="http://developer.android.com/reference/android/app/Activity.html">Activity</a>.</p>
<p>You can browse the full source code of the Eclipse project here : <a href="http://code.google.com/p/androgames-sample/">SampleAccelerometerSensor</a></p>
<p>Enjoy !</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.androgames.net/85/android-accelerometer-tutorial/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Android Live Wallpaper tutorial</title>
		<link>http://blog.androgames.net/58/android-live-wallpaper-tutorial/</link>
		<comments>http://blog.androgames.net/58/android-live-wallpaper-tutorial/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 16:41:48 +0000</pubDate>
		<dc:creator>Tonio</dc:creator>
		
		<category><![CDATA[Tutorial]]></category>

		<category><![CDATA[live wallpaper]]></category>

		<guid isPermaLink="false">http://blog.androgames.net/?p=58</guid>
		<description><![CDATA[Live Wallpaper is a new feature added in Android 2.1. Live wallpapers are animated and interactive backgrounds that can be added to your Android home. In this tutorial we&#8217;ll se how to build an interactive animated live wallpaper properly.
A live wallpaper is an Android application that includes a WallpaperService. This service must wrap a WallpaperService.Engine. [...]]]></description>
			<content:encoded><![CDATA[<p>Live Wallpaper is a new feature added in Android 2.1. Live wallpapers are animated and interactive backgrounds that can be added to your Android home. In this tutorial we&#8217;ll se how to build an interactive animated live wallpaper properly.</p>
<p>A live wallpaper is an Android application that includes a <a href="http://developer.android.com/reference/android/service/wallpaper/WallpaperService.html">WallpaperService</a>. This service must wrap a <a href="http://developer.android.com/reference/android/service/wallpaper/WallpaperService.Engine.html">WallpaperService.Engine</a>. The Engine is the bridge between the user, the surface, and the system. It owns the surface in wich the wallpaper is drawn.</p>
<p>First of all, a WallpaperService class must be created with its inner Engine class. This service must be declared in the AndroidManifest.xml with the android.service.wallpaper.WallpaperService Intent so that it would be recognized as a live wallpaper on the device. The android.permission.BIND_WALLPAPER permission is required to attach a live wallpaper to the Android Home application :</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;LiveWallpaperService&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:enabled</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:icon</span>=<span style="color: #ff0000;">&quot;@drawable/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:permission</span>=<span style="color: #ff0000;">&quot;android.permission.BIND_WALLPAPER&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;intent-filter</span> <span style="color: #000066;">android:priority</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.service.wallpaper.WallpaperService&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta-data</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.service.wallpaper&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">android:resource</span>=<span style="color: #ff0000;">&quot;@xml/wallpaper&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>A xml file should be placed in the <strong>/res/xml/</strong> directory of your application. It&#8217;s used to described your live wallpaper.</p>

<div class="wp_syntax"><div class="code"><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;wallpaper</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:thumbnail</span>=<span style="color: #ff0000;">&quot;@drawable/thumbnail&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">android:description</span>=<span style="color: #ff0000;">&quot;@string/description&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:settingsActivity</span>=<span style="color: #ff0000;">&quot;PreferenceActivity&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>As described in the <strong>attrs.xml</strong> Android file, the allowed attributes and there meanings are as follow :</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;declare-styleable</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Wallpaper&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- Component name of an activity that allows the user to modify</span>
<span style="color: #808080; font-style: italic;">         the current settings for this wallpaper. --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;attr</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;settingsActivity&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- Reference to a the wallpaper's thumbnail bitmap. --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;attr</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;thumbnail&quot;</span> <span style="color: #000066;">format</span>=<span style="color: #ff0000;">&quot;reference&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- Name of the author of this component, e.g. Google. --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;attr</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;author&quot;</span> <span style="color: #000066;">format</span>=<span style="color: #ff0000;">&quot;reference&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- Short description of the component's purpose or behavior. --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;attr</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;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;/declare-styleable<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The live wallpaper archetype is as follow :</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">net.androgames.blog.sample.livewallpaper</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.SharedPreferences</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.service.wallpaper.WallpaperService</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.MotionEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.SurfaceHolder</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Android Live Wallpaper Archetype
 * @author antoine vianey
 * under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LiveWallpaperService <span style="color: #000000; font-weight: bold;">extends</span> WallpaperService <span style="color: #009900;">&#123;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> Engine onCreateEngine<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: #000000; font-weight: bold;">new</span> SampleEngine<span style="color: #009900;">&#40;</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;">void</span> onCreate<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;">super</span>.<span style="color: #006633;">onCreate</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;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onDestroy<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;">super</span>.<span style="color: #006633;">onDestroy</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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SampleEngine <span style="color: #000000; font-weight: bold;">extends</span> Engine <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">private</span> LiveWallpaperPainting painting<span style="color: #339933;">;</span>
&nbsp;
        SampleEngine<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            SurfaceHolder holder <span style="color: #339933;">=</span> getSurfaceHolder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            painting <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LiveWallpaperPainting<span style="color: #009900;">&#40;</span>holder, 
                getApplicationContext<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;
        @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>SurfaceHolder surfaceHolder<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>surfaceHolder<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// register listeners and callbacks here</span>
            setTouchEventsEnabled<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</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;">void</span> onDestroy<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;">super</span>.<span style="color: #006633;">onDestroy</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// remove listeners and callbacks here</span>
            painting.<span style="color: #006633;">stopPainting</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;
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onVisibilityChanged<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">boolean</span> visible<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>visible<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// register listeners and callbacks here</span>
                painting.<span style="color: #006633;">resumePainting</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: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// remove listeners and callbacks here</span>
                painting.<span style="color: #006633;">pausePainting</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>
&nbsp;
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onSurfaceChanged<span style="color: #009900;">&#40;</span>SurfaceHolder holder, <span style="color: #000066; font-weight: bold;">int</span> format, 
                <span style="color: #000066; font-weight: bold;">int</span> width, <span style="color: #000066; font-weight: bold;">int</span> height<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;">onSurfaceChanged</span><span style="color: #009900;">&#40;</span>holder, format, width, height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            painting.<span style="color: #006633;">setSurfaceSize</span><span style="color: #009900;">&#40;</span>width, height<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;">void</span> onSurfaceCreated<span style="color: #009900;">&#40;</span>SurfaceHolder holder<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;">onSurfaceCreated</span><span style="color: #009900;">&#40;</span>holder<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// start painting</span>
            painting.<span style="color: #006633;">start</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;
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onSurfaceDestroyed<span style="color: #009900;">&#40;</span>SurfaceHolder holder<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;">onSurfaceDestroyed</span><span style="color: #009900;">&#40;</span>holder<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">boolean</span> retry <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
            painting.<span style="color: #006633;">stopPainting</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;">while</span> <span style="color: #009900;">&#40;</span>retry<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                    painting.<span style="color: #006633;">join</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    retry <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</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><span style="color: #003399;">InterruptedException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</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;">void</span> onOffsetsChanged<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span> xOffset, <span style="color: #000066; font-weight: bold;">float</span> yOffset, 
                <span style="color: #000066; font-weight: bold;">float</span> xStep, <span style="color: #000066; font-weight: bold;">float</span> yStep, <span style="color: #000066; font-weight: bold;">int</span> xPixels, <span style="color: #000066; font-weight: bold;">int</span> yPixels<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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;">void</span> onTouchEvent<span style="color: #009900;">&#40;</span>MotionEvent event<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;">onTouchEvent</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            painting.<span style="color: #006633;">doTouchEvent</span><span style="color: #009900;">&#40;</span>event<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></div></div>

<p>The Engine&#8217;s methods <strong>onCreate</strong>, <strong>onDestroy</strong>, <strong>onVisibilityChanged</strong>, <strong>onSurfaceChanged</strong>, <strong>onSurfaceCreated</strong> and <strong>onSurfaceDestroyed</strong> are called when the wallpaper visibility, state or size changes. With this methods, the live wallpaper could be animated only if necessary. Touch events are activated through <strong>setTouchEventsEnabled(true)</strong> and handled with the <strong>onTouchEvent(MotionEvent event)</strong> callback.</p>
<p>To do the actual painting of the wallpaper, a separate painting thread is used :</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">net.androgames.blog.sample.livewallpaper</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Context</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.graphics.Canvas</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.MotionEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.SurfaceHolder</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Android Live Wallpaper painting thread Archetype
 * @author antoine vianey
 * GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LiveWallpaperPainting <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Thread</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/** Reference to the View and the context */</span>
    <span style="color: #000000; font-weight: bold;">private</span> SurfaceHolder surfaceHolder<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Context</span> context<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/** State */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> wait<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> run<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/** Dimensions */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> width<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> height<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/** Time tracking */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> previousTime<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> currentTime<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> LiveWallpaperPainting<span style="color: #009900;">&#40;</span>SurfaceHolder surfaceHolder, 
            <span style="color: #003399;">Context</span> context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// keep a reference of the context and the surface</span>
        <span style="color: #666666; font-style: italic;">// the context is needed if you want to inflate</span>
        <span style="color: #666666; font-style: italic;">// some resources from your livewallpaper .apk</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">surfaceHolder</span> <span style="color: #339933;">=</span> surfaceHolder<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">context</span> <span style="color: #339933;">=</span> context<span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// don't animate until surface is created and displayed</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">wait</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Pauses the live wallpaper animation
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> pausePainting<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;">this</span>.<span style="color: #006633;">wait</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">synchronized</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><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;">notify</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>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Resume the live wallpaper animation
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> resumePainting<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;">this</span>.<span style="color: #006633;">wait</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">synchronized</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><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;">notify</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>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Stop the live wallpaper animation
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> stopPainting<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;">this</span>.<span style="color: #006633;">run</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">synchronized</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><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;">notify</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>
&nbsp;
    @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>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">run</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">Canvas</span> c <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>run<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                c <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">surfaceHolder</span>.<span style="color: #006633;">lockCanvas</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">surfaceHolder</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    currentTime <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    updatePhysics<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    doDraw<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    previousTime <span style="color: #339933;">=</span> currentTime<span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c <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>
                    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">surfaceHolder</span>.<span style="color: #006633;">unlockCanvasAndPost</span><span style="color: #009900;">&#40;</span>c<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: #666666; font-style: italic;">// pause if no need to animate</span>
            <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>wait<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                        wait<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: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Invoke when the surface dimension change
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setSurfaceSize<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> width, <span style="color: #000066; font-weight: bold;">int</span> height<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;">width</span> <span style="color: #339933;">=</span> width<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">height</span> <span style="color: #339933;">=</span> height<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">synchronized</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><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;">notify</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>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Invoke while the screen is touched
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doTouchEvent<span style="color: #009900;">&#40;</span>MotionEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// handle the event here</span>
        <span style="color: #666666; font-style: italic;">// if there is something to animate</span>
        <span style="color: #666666; font-style: italic;">// then wake up</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">wait</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">synchronized</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            notify<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>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Do the actual drawing stuff
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> doDraw<span style="color: #009900;">&#40;</span><span style="color: #003399;">Canvas</span> canvas<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Update the animation, sprites or whatever.
     * If there is nothing to animate set the wait
     * attribute of the thread to true
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> updatePhysics<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// if nothing was updated :</span>
        <span style="color: #666666; font-style: italic;">// this.wait = true;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This class is optimized in the sens that it draws the canvas only if the wallpaper surface is visible and if there is something new to draw. If there is nothing to animate anymore, then the <strong>updatePhysics</strong> should tell the thread to wait. If so, the canvas needs to be drawn one last time because of the SurfaceView behaviour that use two Canvas alternating&#8230;</p>
<p>To allow configuration of a live wallpaper, just create a <a href="http://developer.android.com/reference/android/preference/PreferenceActivity.html">PreferenceActivity</a> and link it into the wallpaper xml declaration file described previously. Preference values can be retrieved through a <a href="http://developer.android.com/reference/android/content/SharedPreferences.html">SharedPreference</a> object.</p>
<p>You can browse the full source code of the Eclipse project here : <a href="http://code.google.com/p/androgames-sample/">SampleLiveWallpaper</a>.</p>
<p><object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/FW6LIAkbuZE&#038;hl=fr_FR&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/FW6LIAkbuZE&#038;hl=fr_FR&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object></p>
<p>Enjoy !</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.androgames.net/58/android-live-wallpaper-tutorial/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Custom Android Button Style and Theme</title>
		<link>http://blog.androgames.net/40/custom-button-style-and-theme/</link>
		<comments>http://blog.androgames.net/40/custom-button-style-and-theme/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 19:30:08 +0000</pubDate>
		<dc:creator>Tonio</dc:creator>
		
		<category><![CDATA[Tutorial]]></category>

		<category><![CDATA[style]]></category>

		<category><![CDATA[theme]]></category>

		<guid isPermaLink="false">http://blog.androgames.net/?p=40</guid>
		<description><![CDATA[In this tutorial, we&#8217;ll see how it&#8217;s possible to create a custom button style for an Android application by using the Android styling API. First of all we need to define the new look for our custom button style. We would use three different NinePatch drawables.
A custom black nine patch drawable  background for enabled, [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, we&#8217;ll see how it&#8217;s possible to create a custom button style for an Android application by using the Android styling API. First of all we need to define the new look for our custom button style. We would use three different <a href="http://developer.android.com/reference/android/graphics/NinePatch.html">NinePatch drawables</a>.</p>
<p>A custom black nine patch drawable  background for enabled, not pressed buttons :</p>
<p><img class="alignnone size-full wp-image-41" title="black button background" src="http://blog.androgames.net/wp-content/uploads/2009/11/btn_black9.png" alt="black button background" width="88" height="47" /></p>
<p>A custom orange nine patch drawable  background for enabled, pressed buttons :</p>
<p><img class="alignnone size-full wp-image-42" title="Orange button background" src="http://blog.androgames.net/wp-content/uploads/2009/11/btn_orange9.png" alt="Orange button background" width="88" height="47" /></p>
<p>A custom red nine patch drawable background for disabled buttons :</p>
<p><img class="alignnone size-full wp-image-43" title="Red button background" src="http://blog.androgames.net/wp-content/uploads/2009/11/btn_red9.png" alt="Red button background" width="88" height="47" /></p>
<p>All of these three custom drawable must be placed in the /res/drawables directory of our Android application. Now we must declare wich background should be used for each possible state of our custom button (pressed, disabled, focused, &#8230;). This is done by declaring a selector in a custom XML file :</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;selector</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&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>
<span style="color: #009900;">	<span style="color: #000066;">android:state_enabled</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:drawable</span>=<span style="color: #ff0000;">&quot;@drawable/btn_red&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>
<span style="color: #009900;">    	<span style="color: #000066;">android:state_pressed</span>=<span style="color: #ff0000;">&quot;true&quot;</span> </span>
<span style="color: #009900;">    	<span style="color: #000066;">android:state_enabled</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:drawable</span>=<span style="color: #ff0000;">&quot;@drawable/btn_orange&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>
<span style="color: #009900;">    	<span style="color: #000066;">android:state_focused</span>=<span style="color: #ff0000;">&quot;true&quot;</span> </span>
<span style="color: #009900;">    	<span style="color: #000066;">android:state_enabled</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:drawable</span>=<span style="color: #ff0000;">&quot;@drawable/btn_orange&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>
<span style="color: #009900;">    	<span style="color: #000066;">android:state_enabled</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:drawable</span>=<span style="color: #ff0000;">&quot;@drawable/btn_black&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/selector<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>﻿</pre></div></div>

<p>Each item in the selector associates a drawable to a button state. Items must be declared in a specific order. The android selector draws the first selector item that match the current button state, no matter if another matching item is defined in the selector declaration. For exemple, the selector bellow will never draw the orange background for a disabled and focused button as the first item always applies to disabled button :</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">android:state_enabled</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:drawable</span>=<span style="color: #ff0000;">&quot;@drawable/btn_red&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>
<span style="color: #009900;">    <span style="color: #000066;">android:state_pressed</span>=<span style="color: #ff0000;">&quot;true&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">android:state_enabled</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:drawable</span>=<span style="color: #ff0000;">&quot;@drawable/btn_orange&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>﻿</pre></div></div>

<p>In order to draw orange background for disabled and focused buttons, and a red background for disabled buttons that are not focused we should have invert the items declaration order :</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">android:state_pressed</span>=<span style="color: #ff0000;">&quot;true&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">android:state_enabled</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:drawable</span>=<span style="color: #ff0000;">&quot;@drawable/btn_orange&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>
<span style="color: #009900;">    <span style="color: #000066;">android:state_enabled</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:drawable</span>=<span style="color: #ff0000;">&quot;@drawable/btn_red&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>Let&#8217;s put our selector declaration in a file called btn_custom.xml in the /res/drawables directory of our Android application. Now, it&#8217;s time to define a custom button style that will apply to every buttons in the application. This can be achieve with both the /res/values/styles.xml file and the /res/values/themes.xml file. In the first one we&#8217;ll customize our button appearence (selector, text size, text color, shadow, &#8230;).</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><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;style</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Button&quot;</span> <span style="color: #000066;">parent</span>=<span style="color: #ff0000;">&quot;@android:style/Widget.Button&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;">name</span>=<span style="color: #ff0000;">&quot;android:gravity&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>center_vertical|center_horizontal<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;">name</span>=<span style="color: #ff0000;">&quot;android:textColor&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>#FFFFFFFF<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;">name</span>=<span style="color: #ff0000;">&quot;android:shadowColor&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>#FF000000<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;">name</span>=<span style="color: #ff0000;">&quot;android:shadowDx&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>0<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;">name</span>=<span style="color: #ff0000;">&quot;android:shadowDy&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>-1<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;">name</span>=<span style="color: #ff0000;">&quot;android:shadowRadius&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>0.2<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;">name</span>=<span style="color: #ff0000;">&quot;android:textSize&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>16dip<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;">name</span>=<span style="color: #ff0000;">&quot;android:textStyle&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>bold<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;">name</span>=<span style="color: #ff0000;">&quot;android:background&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>@drawable/btn_custom<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;">name</span>=<span style="color: #ff0000;">&quot;android:focusable&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>true<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;">name</span>=<span style="color: #ff0000;">&quot;android:clickable&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>true<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;/style<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></div></div>

<p>Then, we must set this style to be used by the Android button Widget. In the /res/values/themes.xml file&nbsp;:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><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;style</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;CustomButton&quot;</span> <span style="color: #000066;">parent</span>=<span style="color: #ff0000;">&quot;android:style/Theme.NoTitleBar&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;">name</span>=<span style="color: #ff0000;">&quot;android:buttonStyle&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>@style/Button<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;/style<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></div></div>

<p>The theme can be applied to the whole application by setting the <strong>android:theme</strong> attribute in the <strong>&lt;application&gt;</strong> tag of the AndroidManifest.xml file.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;application</span> <span style="color: #000066;">android:icon</span>=<span style="color: #ff0000;">&quot;@drawable/icon&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">android:label</span>=<span style="color: #ff0000;">&quot;Custom button&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:theme</span>=<span style="color: #ff0000;">&quot;@style/CustomButton&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></pre></div></div>

<p>Now, that&#8217;s what your buttons looks like :</p>
<p style="text-align: center;"><img class="size-full wp-image-44 aligncenter" title="Android custom button" src="http://blog.androgames.net/wp-content/uploads/2009/11/custom1.png" alt="Android custom button" width="183" height="103" /></p>
<p style="text-align: center;"><img class="size-full wp-image-45 aligncenter" title="Android custom button" src="http://blog.androgames.net/wp-content/uploads/2009/11/custom2.png" alt="Android custom button" width="182" height="106" /></p>
<p>You can browse the full source code of the Eclipse project here :<br />
<a href="http://code.google.com/p/androgames-sample/">SampleCustomButton</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.androgames.net/40/custom-button-style-and-theme/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Android Asynchronous HTTPClient tutorial</title>
		<link>http://blog.androgames.net/12/retrieving-data-asynchronously/</link>
		<comments>http://blog.androgames.net/12/retrieving-data-asynchronously/#comments</comments>
		<pubDate>Sun, 18 Oct 2009 16:07:38 +0000</pubDate>
		<dc:creator>Tonio</dc:creator>
		
		<category><![CDATA[Tutorial]]></category>

		<category><![CDATA[HTTPClient]]></category>

		<guid isPermaLink="false">http://blog.androgames.net/?p=12</guid>
		<description><![CDATA[In Android, the system guards against applications that are insufficiently responsive for a period of time by displaying a dialog to the user, called the Application Not Responding (ANR) dialog. The user can choose to let the application continue, but the user won&#8217;t appreciate having this dialog displayed every time he or she uses your [...]]]></description>
			<content:encoded><![CDATA[<p>In Android, the system guards against applications that are insufficiently responsive for a period of time by displaying a dialog to the user, called the Application Not Responding (ANR) dialog. The user can choose to let the application continue, <strong>but the user won&#8217;t appreciate having this dialog displayed every time he or she uses your application</strong>. So it&#8217;s important to design responsiveness into your application, so that the system never has cause to display an ANR to the user.</p>
<p>In this tutorial we are going to see how to perform asynchronous request to distant servers using the build&#8217;in HTTPClient without blocking the UI thread. the problem is that we have no possibilities to control the distant server responsiveness and the user Internet connection speed wich can cause the server&#8217;s response to be received after several seconds&#8230;</p>
<h3>The problem to solve</h3>
<p>The problem we want to solve is quite simple. Running an Activity, we want to fetch data from a distant server and process it without blocking the UI thread.</p>
<h3>The solution</h3>
<p>The Activity will react to a user action and send data to a distant server. The distant server will send back raw data to the application wich will produce processed data. The running Activity can then display the processed data to the user.</p>
<p>We need :</p>
<ul>
<li>An Activity running in the UI Thred</li>
<li>A client to communicate with the distant server</li>
<li>A thread to process the raw data</li>
<li>A listener to pass the processed data back to the Activity</li>
</ul>
<h3>The source code</h3>
<p>Our Activity will send data to the distant server with a static call on our client passing the request to send to the server and the callback listener to recieve the processed data.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Client.<span style="color: #006633;">sendRequest</span><span style="color: #009900;">&#40;</span>request, <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>A static method from our client will start a new AsynchronousSender in a new Thread</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><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;">import</span> <span style="color: #006699;">org.apache.http.HttpRequest</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> Client <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: #000066; font-weight: bold;">void</span> sendRequest<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> HttpRequest request,
			ResponseListener callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> AsynchronousSender<span style="color: #009900;">&#40;</span>request, <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: #000000; font-weight: bold;">new</span> CallbackWrapper<span style="color: #009900;">&#40;</span>callback<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</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></pre></div></div>

<p>The running Activity must implemment the ResponseListener interface to recieve the processed data from the server.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.HttpResponse</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> ResponseListener <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onResponseReceived<span style="color: #009900;">&#40;</span>HttpResponse response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Finaly, the AsynchronousSender will send the request to the distant server and process the server&#8217;s response before passing the processed data back to the specified ResponseListener.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.HttpResponse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.HttpRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.client.ClientProtocolException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.client.HttpClient</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.impl.client.DefaultHttpClient</span><span style="color: #339933;">;</span>
&nbsp;
<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> AsynchronousSender <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Thread</span> <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> DefaultHttpClient httpClient <span style="color: #339933;">=</span>
		<span style="color: #000000; font-weight: bold;">new</span> DefaultHttpClient<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;">private</span> <span style="color: #003399;">Request</span> request<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Handler handler<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> CallbackWrapper wrapper<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> AsynchronousSender<span style="color: #009900;">&#40;</span>HttpRequest request,
			Handler handler, CallbackWrapper wrapper<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;">request</span> <span style="color: #339933;">=</span> request<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">handler</span> <span style="color: #339933;">=</span> handler<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">wrapper</span> <span style="color: #339933;">=</span> wrapper<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<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>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">final</span> HttpResponse response<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #009900;">&#40;</span>httpClient<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				response <span style="color: #339933;">=</span> getClient<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #666666; font-style: italic;">// process response</span>
			wrapper.<span style="color: #006633;">setResponse</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			handler.<span style="color: #006633;">post</span><span style="color: #009900;">&#40;</span>wrapper<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>ClientProtocolException 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> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> 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>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> HttpClient getClient<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> httpClient<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We use a CallbackWrapper to send the processed data back to the Activity as the Hander needs a Runnable to post.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.HttpResponse</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> CallbackWrapper <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Runnable</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> ResponseListener callbackActivity<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> HttpResponse response<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> CallbackWrapper<span style="color: #009900;">&#40;</span>ResponseListener callbackActivity<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;">callbackActivity</span> <span style="color: #339933;">=</span> callbackActivity<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<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>
		callbackActivity.<span style="color: #006633;">onResponseReceived</span><span style="color: #009900;">&#40;</span>response<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: #000066; font-weight: bold;">void</span> setResponse<span style="color: #009900;">&#40;</span>HttpResponse response<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;">response</span> <span style="color: #339933;">=</span> response<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.androgames.net/12/retrieving-data-asynchronously/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Make call from Android Activity</title>
		<link>http://blog.androgames.net/5/start-phone-call/</link>
		<comments>http://blog.androgames.net/5/start-phone-call/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 17:34:47 +0000</pubDate>
		<dc:creator>Tonio</dc:creator>
		
		<category><![CDATA[Tutorial]]></category>

		<category><![CDATA[call]]></category>

		<category><![CDATA[Intent]]></category>

		<guid isPermaLink="false">http://blog.androgames.net/?p=5</guid>
		<description><![CDATA[Android make it simple to start a phone call from any application. Just use the following sample code into your activity :

String uri = &#34;tel:666-666-666&#34;;
Intent intent = new Intent&#40;Intent.ACTION_CALL&#41;;
intent.setData&#40;Uri.parse&#40;url&#41;&#41;;
startActivity&#40;intent&#41;;

]]></description>
			<content:encoded><![CDATA[<p>Android make it simple to start a phone call from any application. Just use the following sample code into your activity :</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> uri <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;tel:666-666-666&quot;</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>Intent.<span style="color: #006633;">ACTION_CALL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
intent.<span style="color: #006633;">setData</span><span style="color: #009900;">&#40;</span>Uri.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</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></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.androgames.net/5/start-phone-call/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
