<?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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Meerlight - Android Adventures</title>
	<atom:link href="http://meerlight.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://meerlight.com</link>
	<description>Android adventures, C# diversions</description>
	<lastBuildDate>Sat, 25 Jun 2011 10:12:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Windows Operating System Internals for Developers</title>
		<link>http://meerlight.com/2011/06/windows-operating-system-internals-for-developers/</link>
		<comments>http://meerlight.com/2011/06/windows-operating-system-internals-for-developers/#comments</comments>
		<pubDate>Sat, 25 Jun 2011 10:12:24 +0000</pubDate>
		<dc:creator>meerlight</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Process Explorer]]></category>
		<category><![CDATA[windbg]]></category>
		<category><![CDATA[Windows Internals]]></category>

		<guid isPermaLink="false">http://meerlight.com/?p=127</guid>
		<description><![CDATA[In my day job I&#8217;m a .NET developer who delves into C++ every now and again. As a result I don&#8217;t spend much time having to much about with crash dumps and so on, but this week I&#8217;ve been on a Windows Internals given by Alex Ionescu (one of the authors of the Windows Internels [...]]]></description>
			<content:encoded><![CDATA[<p>In my day job I&#8217;m a .NET developer who delves into C++ every now and again. As a result I don&#8217;t spend much time having to much about with crash dumps and so on, but this week I&#8217;ve been on a <a href="http://www.solsem.com/class_internals.html">Windows Internals</a> given by Alex Ionescu (one of the authors of the <a href="http://amzn.com/0735625301">Windows Internels</a> book.) It wasn&#8217;t my intention to blog about this sort of stuff, but the week long course was an eye opener to what goes on &#8220;under the hood&#8221; of Windows. It wasn&#8217;t all new, I&#8217;ve been about a long while, but it was a real refresh on my understanding, and a wealth of new, detailed, information as well. The course was provided by the company I work for, and all I can say is that if you are asked if you want to attend one of these courses, say &#8220;yes&#8221; without any hesitation.</p>
<p>The course we took was aimed at developers and exposed the bowels of the Windows kernel, including memory management, thread scheduling, interrupt processing, time accounting, security, and crash dump analysis. The course included advanced explanations of windbg, Process Exploerer in particular, but a lot of the System Internals were touched upon somewhere during the course.</p>
<p>Just to reiterate: Get on one of these courses if you can.</p>
]]></content:encoded>
			<wfw:commentRss>http://meerlight.com/2011/06/windows-operating-system-internals-for-developers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding AdMob ads to your applications</title>
		<link>http://meerlight.com/2011/04/adding-admob-ads-to-your-applications/</link>
		<comments>http://meerlight.com/2011/04/adding-admob-ads-to-your-applications/#comments</comments>
		<pubDate>Sat, 30 Apr 2011 07:35:30 +0000</pubDate>
		<dc:creator>meerlight</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[AdMob]]></category>
		<category><![CDATA[Relative Layout]]></category>

		<guid isPermaLink="false">http://meerlight.com/?p=19</guid>
		<description><![CDATA[This post will look at putting adverts into your application. To use this code you will need to sign up to google&#8217;s AdMob service. I&#8217;m going to assume that you have done all that and have a publisher id that you can use for this example. OK, so how to go about this. The idea [...]]]></description>
			<content:encoded><![CDATA[<p>This post will look at putting adverts into your application. To use this code you will need to sign up to google&#8217;s <a href="http://www.admob.com/">AdMob</a> service. I&#8217;m going to assume that you have done all that and have a publisher id that you can use for this example.</p>
<p>OK, so how to go about this. The idea is to create an advert at the bottom of the screen that is invisible before the advert arrives, and slides up from the bottom of the screen when it arrives.</p>
<p>Step 1 is to create a new Android application in eclipse called <strong>AdMobExample</strong>. When you have done that you need to right click on the project and select <strong>Properties</strong>. On the <em>Properties for AdMobExample</em> dialog, click on <em>Java Build Path</em> from the list on the right. You should now see something like this:</p>
<p><img src="http://meerlight.com/images/admobproperties.jpg" alt="Properties dialog" /></p>
<p>To add the <span id="fixed">GoogleAdMobAdsSdk-4.0.4.jar</span> library you need to click on the <strong>Add External Jars</strong> button and select the jar file (you must install the library yourself by downloading the SDK from the AdMob site.) I installed the SDK to <span id="fixed">/usr/lib</span> (on Linux), this might be something like  <span id="fixed">C:/Program Files/GoogleAdsMobAdsSdk</span> on Windows.</p>
<p>Now we can start to build the app. First up is the <span id="fixed">AndroidManifest.xml</span> file. We need add an activity to the &lt;application&gt; so that AdMob can do its thing. Add this just before the &lt;/application&gt; tag:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- AdMobActivity definition --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;activity</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;com.google.ads.AdActivity&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:theme</span>=<span style="color: #ff0000;">&quot;@android:style/Theme.NoTitleBar.Fullscreen&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:configChanges</span>=<span style="color: #ff0000;">&quot;orientation|keyboard|keyboardHidden&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p>In order for this activity to work it needs access to the internet, and access to the network state of the device. This means that we must request these permissions by adding these lines after the &lt;application&gt; tag:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #808080; font-style: italic;">&lt;!-- AdMob SDK requires Internet permission  --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.permission.INTERNET&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.permission.ACCESS_NETWORK_STATE&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p>Finally, AdMob can use the device&#8217;s location when it serves adverts. To enable this feature you need to add the following after the &jt;uses-permission&gt; lines :</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #808080; font-style: italic;">&lt;!-- Allow AdMob to use the devices location when delivering ads --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta-data</span> <span style="color: #000066;">android:value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;ADMOB_ALLOW_LOCATION_FOR_ADS&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p>That completes the changes to the manifest. Now we need to create a layout that will lock the adverts to the bottom of the screen. This is done by using <strong>RelativeLayout</strong>. Here is the complete <span id="fixed">main.xml</span> file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td 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;RelativeLayout</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;">xmlns:myapp</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/com.example.admobexample&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- This is the main view. Here it is just a simple TextView, but it will</span>
<span style="color: #808080; font-style: italic;">      probably be another layout in a more complex app --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextView</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>
<span style="color: #009900;">    <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;@string/hello&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RelativeLayout</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>
<span style="color: #009900;">     <span style="color: #000066;">android:layout_alignParentBottom</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>  
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!-- Set myapp:adUnitId to be your publisher ID --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;com.google.ads.AdView</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/ad&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>
<span style="color: #009900;">        <span style="color: #000066;">myapp:adUnitId</span>=<span style="color: #ff0000;">&quot;000000000000000&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">myapp:adSize</span>=<span style="color: #ff0000;">&quot;BANNER&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">myapp:refreshInterval</span>=<span style="color: #ff0000;">&quot;30&quot;</span></span>
<span style="color: #009900;">     <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/RelativeLayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/RelativeLayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The first <strong>RelativeLayout</strong> contains the main view, in this example it is just a simple <strong>TextView</strong>, followed by another <strong>RelativeLayout</strong> that contains the view for the advert. In terms of the layout, the crucial part of this layout is line 19, setting the <em>android:layout_alignParentBottom</em> element to <em>true</em> tells android to put the <strong>AdView</strong> at the bottom of the screen. To get the view to deliver your adverts you need to change the <em>myapp:adUnitId</em> value to be your publisher id.</p>
<p>Now we have got our manifest and layout configured all that is left is to request an advert in the <em>onCreate</em> method:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
&nbsp;
    // Request an ad
    AdRequest ad = new AdRequest();
    ad.setTesting(true);
    AdView adView = (AdView)findViewById(R.id.ad);
    adView.loadAd(ad);
}</pre></td></tr></table></div>

<p>The <em>setTesting(true)</em> call tells AdMob to simulate adverts in the emulator, but it will still serve real adverts on a device.</p>
<p>If you run this now in the emulator you should see a screen like this:</p>
<p><img src="http://meerlight.com/images/admobexample.jpg" alt="Properties dialog" /></p>
<p>If you  deploy this to a device then it will deliver a real advert.</p>
<h2>Download</h2>
<p>You can download this example from <a href="http://meerlight.com/packages/AdMobExample.zip">here</a>.</p>
<p>To add this example to eclipse go to <strong>File|New|Android Project</strong>, select &#8220;Create project from existing source&#8221; and then browse to the folder inside of the downloaded zip file.</p>
]]></content:encoded>
			<wfw:commentRss>http://meerlight.com/2011/04/adding-admob-ads-to-your-applications/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Simple ViewFlipper Example</title>
		<link>http://meerlight.com/2011/03/simple-viewflipper-example/</link>
		<comments>http://meerlight.com/2011/03/simple-viewflipper-example/#comments</comments>
		<pubDate>Tue, 22 Mar 2011 17:08:20 +0000</pubDate>
		<dc:creator>meerlight</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[ViewFlipper]]></category>

		<guid isPermaLink="false">http://meerlight.com/?p=13</guid>
		<description><![CDATA[The ViewFlipper is a really useful view on android. It allows you to build up a set of pages and easily move between them. At its simplest, you call the showPrevious and showNext methods on the view and it moves through your pages and wraps back to the beginning when it gets to the end. [...]]]></description>
			<content:encoded><![CDATA[<p>The <strong>ViewFlipper</strong> is a really useful view on android. It allows you to build up a set of pages and easily move between them. At its simplest, you call the <em>showPrevious</em> and <em>showNext</em> methods on the view and it moves through your pages and wraps back to the beginning when it gets to the end. At the other end of the spectrum you can incorporate gestures to enable the user to swipe through the pages, and with the <strong>ViewFlipper</strong> handling just three views you can allow the user to move through any number of pages.</p>
<p>In this first article, I am just going to show you a simple application so that you can see what it does. I will go into the more complex examples in other posts.</p>
<p>So, this application is going to show two buttons at the top to allow the user to go to the next and previous pages. Beneath them will be the <strong>ViewFlipper</strong> which contains three <strong>TextViews</strong>.</p>
<p>So firstly create a new android project called <strong>ViewFlipperSimple</strong> and change the<span id="fixed"> res/layout/main.xml</span> file to be the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td 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>
<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:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:orientation</span>=<span style="color: #ff0000;">&quot;vertical&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>
<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>
        <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/previous&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>
<span style="color: #009900;">            <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;Prev&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <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/next&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>
<span style="color: #009900;">            <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;Next&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 style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ViewFlipper</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/flipper&quot;</span> </span>
<span style="color: #009900;">        <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span>
<span style="color: #009900;">        <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextView</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;view 1&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextView</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;view 2&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextView</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;view 3&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ViewFlipper<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<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></td></tr></table></div>

<p>Now replace the contents of ViewFlipperSimple.java with:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.example.android.viewflippersimple</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.os.Bundle</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.View.OnClickListener</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.ViewFlipper</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> ViewFlipperSimple <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #000000; font-weight: bold;">implements</span> OnClickListener <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">Button</span> next<span style="color: #339933;">;</span>
    <span style="color: #003399;">Button</span> previous<span style="color: #339933;">;</span>
    ViewFlipper flipper<span style="color: #339933;">;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        flipper <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ViewFlipper<span style="color: #009900;">&#41;</span>findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">flipper</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        next <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Button</span><span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">next</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        previous <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Button</span><span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">previous</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        next.<span style="color: #006633;">setOnClickListener</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>
        previous.<span style="color: #006633;">setOnClickListener</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>
&nbsp;
    @Override
    <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>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>v <span style="color: #339933;">==</span> next<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            flipper.<span style="color: #006633;">showNext</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: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>v <span style="color: #339933;">==</span> previous<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            flipper.<span style="color: #006633;">showPrevious</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;">&#125;</span></pre></td></tr></table></div>

<p>You can see on line 10 that the <strong>Activity</strong> class implements the <strong>OnClickListener</strong> class. This is so that our activity knows when the user clicks on the buttons: lines 24 and 25 tell the buttons that we want to know about the clicks.</p>
<p>The <em>onClick</em> method determines which button was pressed and calls <em>showNext</em> or <em>showPrevious</em> on the <strong>ViewFlipper</strong>.</p>
<p>That is all we need to do for the application to work, so try running it and see what happens. </p>
<p>Unfortunately, that is not the whole story, because on android 2.1 there is a problem with changing orientation. I also get the crash on android 2.2. An <strong>IllegalArgumentException</strong> occurs in the <em>onDetachedFromWindow</em> method of <strong>ViewFlipper</strong>. You can find out about the about the problem <a href="http://code.google.com/p/android/issues/detail?id=6191">here</a>. In the emulator, with the num lock key off press the <span id="fixed">7</span> key to change to landscape and then the <span id="fixed">9</span> key to go to back to portrait. Did the app crash? If not try going back and forth between landscape and portrait quickly. After the crash you should see something like this in the Debug window:</p>
<pre>Thread [&lt;1&gt; main] (Suspended (exception IllegalArgumentException))
    WindowManagerImpl.removeViewImmediate(View) line: 226
    Window$LocalWindowManager.removeViewImmediate(View) line: 436
    ActivityThread.handleDestroyActivity(IBinder, boolean, int, boolean) line: 3684
    ActivityThread.handleRelaunchActivity(ActivityThread$ActivityRecord, int) line: 3789
    ...</pre>
<p>The java exception is something like:</p>
<pre>java.lang.IllegalArgumentException: Receiver not registered: android.widget.ViewFlipper$1@44b6ab90
    at android.app.ActivityThread$PackageInfo.forgetReceiverDispatcher(ActivityThread.java:667)
    at android.app.ApplicationContext.unregisterReceiver(ApplicationContext.java:747)
    at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:321)
    at android.widget.ViewFlipper.onDetachedFromWindow(ViewFlipper.java:104)</pre>
<p>There is a workaround for this bug. Which is to create a wrapper class around <strong>ViewFlipper</strong> and catch the exception. To fix our application create a new class called <strong>MyViewFlipper</strong> and change the code to be:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><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.Build</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.util.AttributeSet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.util.Log</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.ViewFlipper</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> MyViewFlipper <span style="color: #000000; font-weight: bold;">extends</span> ViewFlipper <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> MyViewFlipper<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: #000000; font-weight: bold;">public</span> MyViewFlipper<span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span> context, <span style="color: #003399;">AttributeSet</span> attrs<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, attrs<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;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onDetachedFromWindow<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><span style="color: #003399;">Integer</span>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>Build.<span style="color: #006633;">VERSION</span>.<span style="color: #006633;">SDK</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;=</span> <span style="color: #cc66cc;">7</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;">super</span>.<span style="color: #006633;">onDetachedFromWindow</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;">IllegalArgumentException</span> e<span style="color: #009900;">&#41;</span> 
            <span style="color: #009900;">&#123;</span>                                  
                Log.<span style="color: #006633;">e</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyViewFlipper&quot;</span>, <span style="color: #0000ff;">&quot;Android issue 6191: http://code.google.com/p/android/issues/detail?id=6191&quot;</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;">finally</span> 
            <span style="color: #009900;">&#123;</span>                                  
                <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">stopFlipping</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: #000000; font-weight: bold;">else</span> 
        <span style="color: #009900;">&#123;</span>                          
            <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onDetachedFromWindow</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;">&#125;</span></pre></td></tr></table></div>

<p>All we need to do to use our extended <strong>ViewFlipper</strong> is to modify the <span id="fixed">AndroidManifest.xml</span> file. Change &#8220;ViewFlipper&#8221; to be &#8220;com.example.android.viewflippersimple.MyViewFlipper&#8221; on lines 21 and 37.</p>
<p>Run the application again and try changing the orientation. The application should function as you would expect. If you look in the LogCat window in eclipse you will see the red exception line that the <em>MyViewFlipper</em> class puts out when it absorbs the exception.</p>
<h2>Download</h2>
<p>You can download this example from <a href="http://meerlight.com/packages/ViewFlipperSimple.zip">here</a>.</p>
<p>To add this example to eclipse go to <strong>File|New|Android Project</strong>, select &#8220;Create project from existing source&#8221; and then browse to the folder inside of the downloaded zip file.</p>
]]></content:encoded>
			<wfw:commentRss>http://meerlight.com/2011/03/simple-viewflipper-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Android Adventures</title>
		<link>http://meerlight.com/2011/03/android-adventures/</link>
		<comments>http://meerlight.com/2011/03/android-adventures/#comments</comments>
		<pubDate>Tue, 15 Mar 2011 14:26:12 +0000</pubDate>
		<dc:creator>meerlight</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://meerlight.com/?p=4</guid>
		<description><![CDATA[By day I am a C#/SQL developer, and have been for&#8230; well, too long to remember. But now Android is here, and it is fascinating stuff, so now I develop Android apps in my spare time. There is a lot of stuff on the internet that has really helped get to grips with developing for [...]]]></description>
			<content:encoded><![CDATA[<p>By day I am a C#/SQL developer, and have been for&#8230; well, too long to remember. But now Android is here, and it is fascinating stuff, so now I develop Android apps in my spare time. There is a lot of stuff on the internet that has really helped get to grips with developing for Android, but quite often there was a step missing in the information that left me piecing bits together. So this blog will be about the blockers that I encounter as I learn.</p>
<p>Some of the stuff will be from other blogs, but the idea is not to regurgitate what others have done, but to provide complete solutions that others can use. I should have posted the missing details as comments on the blogs that I did find useful, but in my haste to get up-to-speed, I did not. That&#8217;s life. In the future I will add comments, as I hope readers of this blog will.</p>
<p>If I feel the urge I will post on C#, SQL and other developer type stuff along the way.</p>
]]></content:encoded>
			<wfw:commentRss>http://meerlight.com/2011/03/android-adventures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
