<?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>Learn Android</title>
	<atom:link href="http://www.learn-android.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learn-android.com</link>
	<description>Tutorials for Developing with Android</description>
	<lastBuildDate>Tue, 13 Dec 2011 13:48:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Using Android Intents Tutorial</title>
		<link>http://www.learn-android.com/2011/12/13/using-android-intents/</link>
		<comments>http://www.learn-android.com/2011/12/13/using-android-intents/#comments</comments>
		<pubDate>Tue, 13 Dec 2011 06:19:19 +0000</pubDate>
		<dc:creator>Sheridan</dc:creator>
				<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://www.learn-android.com/?p=411</guid>
		<description><![CDATA[Intents are just what they sound like, a way to declare to the Android Framework what you intend to do. This can be starting a specific activity, or it can be just asking Android to find some program that can perform an action (whether or not you know what programs are available). We can also [...]]]></description>
			<content:encoded><![CDATA[<p>Intents are just what they sound like, a way to declare to the Android Framework what you intend to do.  This can be starting a specific activity, or it can be just asking Android to find some program that can perform an action (whether or not you know what programs are available).  We can also go in the other direction, and determine what actions are available to us for a particular piece of content.  We are going to cover each of these topics in this article.</p>
<p>In <a href="http://www.learn-android.com/2011/11/21/half-dozen-hello-worlds-part-6-opening-activities/" title="Half Dozen Hello Worlds Part 6 - Opening Activities">this article</a> I showed you how to open a specific activity using the startActivity method and an Intent that specifies a class.  This is just the tip of the iceberg when it comes to using intents.</p>
<p>Here is the basic activity I am going to be using for the rest of this article.  At this point, it is just a ListActivity which is going to reference the different examples we are going to see.</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;">com.learnandroid.intents</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.ListActivity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.ArrayAdapter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.ListAdapter</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> UsingIntentsActivity <span style="color: #000000; font-weight: bold;">extends</span> ListActivity <span style="color: #009900;">&#123;</span>
    <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>
        ListAdapter adapter <span style="color: #339933;">=</span> createAdapter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setListAdapter<span style="color: #009900;">&#40;</span>adapter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> ListAdapter createAdapter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    	<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> listValues <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
    			<span style="color: #0000ff;">&quot;The Activity You Know&quot;</span>,
    			<span style="color: #0000ff;">&quot;The URI You Know&quot;</span>,
    			<span style="color: #0000ff;">&quot;When You Just Don't Know&quot;</span>
    	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #666666; font-style: italic;">// Create a simple array adapter (of type string) with the test values</span>
    	ListAdapter adapter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayAdapter<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, android.<span style="color: #006633;">R</span>.<span style="color: #006633;">layout</span>.<span style="color: #006633;">simple_list_item_1</span>
    			, listValues<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #000000; font-weight: bold;">return</span> adapter<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Next: <a href="http://www.learn-android.com/?p=411&#038;page=2">The Activity You Know</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.learn-android.com/2011/12/13/using-android-intents/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Lots of Lists: Part 2, List with custom objects and adapter</title>
		<link>http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/</link>
		<comments>http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 00:38:12 +0000</pubDate>
		<dc:creator>Kenley</dc:creator>
				<category><![CDATA[Intermediate]]></category>

		<guid isPermaLink="false">http://www.learn-android.com/?p=342</guid>
		<description><![CDATA[Note: This is part two of a tutorial series. Each part can be read independently, but you can find the other tutorials at the bottom of this article In Part 1 of Lots of Lists, we examined how easy it is to create a screen that shows a list. However, real-world lists are rarely the [...]]]></description>
			<content:encoded><![CDATA[<p><em>Note: This is part two of a tutorial series. Each part can be read independently, but you can find the other tutorials at the bottom of this article</em></p>
<p>In <a title="Lots of Lists: Part 1" href="http://www.learn-android.com/2009/12/05/lots-of-lists-1-simple/" target="_blank">Part 1 of Lots of Lists</a>, we examined how easy it is to create a screen that shows a list. However, real-world lists are rarely the entire layout, and even more rarely a simple lists of strings.</p>
<p>In this tutorial we&#8217;ll look at:</p>
<ol>
<li>What is an adapter? New Project! (<a title="Lots of Lists 2: Page 1" href="http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/" target="_self">Page 1</a>)</li>
<li>Custom Object and List Item Layout (<a title="Lots of Lists 2: Page 2" href="http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/2/" target="_self">Page 2</a>)</li>
<li>Custom Adapter (<a title="Lots of Lists 2: Page 3" href="http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/3/" target="_self">Page 3</a>)</li>
<li>All changes together (<a title="Lots of Lists 2: Page 4" href="http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/4/" target="_self">Page 4</a>)</li>
</ol>
<p>Here is a picture of our goal:</p>
<div id="attachment_365" class="wp-caption alignnone" style="width: 198px"><a href="http://www.learn-android.com/wp-content/uploads/2011/11/Screen-shot-2011-11-22-at-12.47.08-PM.png"><img class="size-medium wp-image-365" title="Lots of Lists 2: News Entries" src="http://www.learn-android.com/wp-content/uploads/2011/11/Screen-shot-2011-11-22-at-12.47.08-PM-188x300.png" alt="Lots of Lists 2: News Entries list" width="188" height="300" /></a><p class="wp-caption-text">Lots of Lists 2: News Entries list</p></div>
<p><strong>Download this project now: </strong>[ <a title="Github project link" href="https://github.com/kenleycapps/LA--Lots-of-Lists-Part-2" target="_blank">Git</a> | <a title="Zip project download" href="https://github.com/kenleycapps/LA--Lots-of-Lists-Part-2/zipball/master" target="_blank">ZIP</a> ] or continue reading to learn about ListViews with custom adapters!</p>
<p><span id="more-342"></span></p>
<h1>What is an Adapter?</h1>
<p>An adapter is how Android translates data from some data source onto a view. Typically adapters are used on ListViews and GridViews, and Android provides some very helpful adapters out of the box.</p>
<p>The adapter goes hand-in-hand with a corresponding view (which in our case is a ListView.) Together they bring cleaner code, optimized performance, and reusability to your project.</p>
<p><em><strong>Cleaner separation of concerns</strong></em></p>
<p>When you add an object to an adapter, the action also adds the representative view for that object to any lists bound to the adapter. Your code that is adding data into the list through the adapter doesn&#8217;t have to know how the list is built for each item. Likewise, the code that knows how to build views for each item in your list doesn&#8217;t have to know where the data is coming from.</p>
<p>This makes a somewhat <strong>clean separation of concerns that normally overlap in one spot</strong>. Those of you who have written many lines of code to populate UI elements with varied data can understand how tangled the project becomes when the code retrieving data is also the code rendering it.</p>
<p><em><strong>Optimizations: View Recycling</strong></em></p>
<p>Another great benefit of using a ListView/adapter is the immense performance gains it can bring. If you&#8217;ve ever built a ScrollView with a list of child views, you&#8217;ll notice that performance becomes slow quickly. The ListView/adapter pair solves this with a traditional technique: view recycling.</p>
<p>View recycling is a technique in which view objects are re-used once their current locations are no longer visible. This has a great performance advantage&#8211; <strong>as your list of items grows, the total number of views remains the same</strong>, which corresponds to the distinct number of views drawn in a single screenful. Even better yet, the process is handled behind the scenes, so your adapter should only be aware that views are reused, not necessarily how they are reused.</p>
<p><em><strong>Code Reusability</strong></em></p>
<p>Finally the code you write for an adapter, and the separation it forces in the data, view, and adaptation concerns, will result in your code being much more reusable. Where you may have previously written all of the code directly into some Activity for displaying a list of complex objects, now you have a separate class that you can reuse for other lists. In fact, you can even vary the layout the adapter uses without changing anything about the adapter itself.</p>
<p>Now that we&#8217;ve covered a tiny portion of adapters in theory, let&#8217;s move on to practice. First we need a new project.</p>
<h1>New Project</h1>
<p>Create a new Android Project (2.1+) in Eclipse (If you&#8217;re not using Eclipse: <a title="Getting Setup with Android" href="http://www.learn-android.com/2009/11/03/getting-setup-with-android/">Getting Setup with Android</a>.)</p>
<p>You can name package and Activity whatever you would like. You should be able to immediately run this project on a device or emulator. Here&#8217;s what we have for our Activity and Layout:</p>
<p><strong><em>Starting activity: MainActivity</em></strong>:</p>
<div id="gist1386436" class="gist">
      <div class="gist-file">
        <div class="gist-data gist-syntax">



  <div class="file-data">
    <table cellpadding="0" cellspacing="0" class="lines highlight">
      <tr>
        <td class="line-numbers">
          <span class="line-number" id="file-mainactivity-java-L1" rel="file-mainactivity-java-L1">1</span>
          <span class="line-number" id="file-mainactivity-java-L2" rel="file-mainactivity-java-L2">2</span>
          <span class="line-number" id="file-mainactivity-java-L3" rel="file-mainactivity-java-L3">3</span>
          <span class="line-number" id="file-mainactivity-java-L4" rel="file-mainactivity-java-L4">4</span>
          <span class="line-number" id="file-mainactivity-java-L5" rel="file-mainactivity-java-L5">5</span>
          <span class="line-number" id="file-mainactivity-java-L6" rel="file-mainactivity-java-L6">6</span>
          <span class="line-number" id="file-mainactivity-java-L7" rel="file-mainactivity-java-L7">7</span>
          <span class="line-number" id="file-mainactivity-java-L8" rel="file-mainactivity-java-L8">8</span>
          <span class="line-number" id="file-mainactivity-java-L9" rel="file-mainactivity-java-L9">9</span>
          <span class="line-number" id="file-mainactivity-java-L10" rel="file-mainactivity-java-L10">10</span>
          <span class="line-number" id="file-mainactivity-java-L11" rel="file-mainactivity-java-L11">11</span>
          <span class="line-number" id="file-mainactivity-java-L12" rel="file-mainactivity-java-L12">12</span>
          <span class="line-number" id="file-mainactivity-java-L13" rel="file-mainactivity-java-L13">13</span>
        </td>
        <td class="line-data">
          <pre class="line-pre"><div class="line" id="file-mainactivity-java-LC1"><span class="kn">package</span> <span class="n">com</span><span class="o">.</span><span class="na">learnandroid</span><span class="o">.</span><span class="na">listviewtutorial</span><span class="o">.</span><span class="na">adapter</span><span class="o">;</span></div><div class="line" id="file-mainactivity-java-LC2">&nbsp;</div><div class="line" id="file-mainactivity-java-LC3"><span class="kn">import</span> <span class="nn">android.app.Activity</span><span class="o">;</span></div><div class="line" id="file-mainactivity-java-LC4"><span class="kn">import</span> <span class="nn">android.os.Bundle</span><span class="o">;</span></div><div class="line" id="file-mainactivity-java-LC5">&nbsp;</div><div class="line" id="file-mainactivity-java-LC6"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MainActivity</span> <span class="kd">extends</span> <span class="n">Activity</span> <span class="o">{</span></div><div class="line" id="file-mainactivity-java-LC7">    <span class="cm">/** Called when the activity is first created. */</span></div><div class="line" id="file-mainactivity-java-LC8">    <span class="nd">@Override</span></div><div class="line" id="file-mainactivity-java-LC9">    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onCreate</span><span class="o">(</span><span class="n">Bundle</span> <span class="n">savedInstanceState</span><span class="o">)</span> <span class="o">{</span></div><div class="line" id="file-mainactivity-java-LC10">        <span class="kd">super</span><span class="o">.</span><span class="na">onCreate</span><span class="o">(</span><span class="n">savedInstanceState</span><span class="o">);</span></div><div class="line" id="file-mainactivity-java-LC11">        <span class="n">setContentView</span><span class="o">(</span><span class="n">R</span><span class="o">.</span><span class="na">layout</span><span class="o">.</span><span class="na">main</span><span class="o">);</span></div><div class="line" id="file-mainactivity-java-LC12">    <span class="o">}</span></div><div class="line" id="file-mainactivity-java-LC13"><span class="o">}</span></div></pre>
        </td>
      </tr>
    </table>
  </div>

        </div>

        <div class="gist-meta">
          <a href="https://gist.github.com/kenleycapps/1386436/raw/a64fe5d1dadfaf6f42189531a5be999c2644e089/MainActivity.java" style="float:right">view raw</a>
          <a href="https://gist.github.com/kenleycapps/1386436#file-mainactivity-java" style="float:right; margin-right:10px; color:#666;">MainActivity.java</a>
          <a href="https://gist.github.com/kenleycapps/1386436">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
        </div>
      </div>
</div>

<p><strong><em>Starting layout: res/layout/main.xml</em></strong></p>
<div id="gist1386447" class="gist">
      <div class="gist-file">
        <div class="gist-data gist-syntax">



  <div class="file-data">
    <table cellpadding="0" cellspacing="0" class="lines highlight">
      <tr>
        <td class="line-numbers">
          <span class="line-number" id="file-main-xml-L1" rel="file-main-xml-L1">1</span>
          <span class="line-number" id="file-main-xml-L2" rel="file-main-xml-L2">2</span>
          <span class="line-number" id="file-main-xml-L3" rel="file-main-xml-L3">3</span>
          <span class="line-number" id="file-main-xml-L4" rel="file-main-xml-L4">4</span>
          <span class="line-number" id="file-main-xml-L5" rel="file-main-xml-L5">5</span>
          <span class="line-number" id="file-main-xml-L6" rel="file-main-xml-L6">6</span>
          <span class="line-number" id="file-main-xml-L7" rel="file-main-xml-L7">7</span>
          <span class="line-number" id="file-main-xml-L8" rel="file-main-xml-L8">8</span>
          <span class="line-number" id="file-main-xml-L9" rel="file-main-xml-L9">9</span>
          <span class="line-number" id="file-main-xml-L10" rel="file-main-xml-L10">10</span>
          <span class="line-number" id="file-main-xml-L11" rel="file-main-xml-L11">11</span>
          <span class="line-number" id="file-main-xml-L12" rel="file-main-xml-L12">12</span>
        </td>
        <td class="line-data">
          <pre class="line-pre"><div class="line" id="file-main-xml-LC1"><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></div><div class="line" id="file-main-xml-LC2"><span class="nt">&lt;LinearLayout</span> <span class="na">xmlns:android=</span><span class="s">&quot;http://schemas.android.com/apk/res/android&quot;</span></div><div class="line" id="file-main-xml-LC3">    <span class="na">android:layout_width=</span><span class="s">&quot;fill_parent&quot;</span></div><div class="line" id="file-main-xml-LC4">    <span class="na">android:layout_height=</span><span class="s">&quot;fill_parent&quot;</span></div><div class="line" id="file-main-xml-LC5">    <span class="na">android:orientation=</span><span class="s">&quot;vertical&quot;</span> <span class="nt">&gt;</span></div><div class="line" id="file-main-xml-LC6">&nbsp;</div><div class="line" id="file-main-xml-LC7">    <span class="nt">&lt;TextView</span></div><div class="line" id="file-main-xml-LC8">        <span class="na">android:layout_width=</span><span class="s">&quot;fill_parent&quot;</span></div><div class="line" id="file-main-xml-LC9">        <span class="na">android:layout_height=</span><span class="s">&quot;wrap_content&quot;</span></div><div class="line" id="file-main-xml-LC10">        <span class="na">android:text=</span><span class="s">&quot;@string/hello&quot;</span> <span class="nt">/&gt;</span></div><div class="line" id="file-main-xml-LC11">&nbsp;</div><div class="line" id="file-main-xml-LC12"><span class="nt">&lt;/LinearLayout&gt;</span></div></pre>
        </td>
      </tr>
    </table>
  </div>

        </div>

        <div class="gist-meta">
          <a href="https://gist.github.com/kenleycapps/1386447/raw/bc12cd82317f12a943415a8f3999a6b505a458d8/main.xml" style="float:right">view raw</a>
          <a href="https://gist.github.com/kenleycapps/1386447#file-main-xml" style="float:right; margin-right:10px; color:#666;">main.xml</a>
          <a href="https://gist.github.com/kenleycapps/1386447">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
        </div>
      </div>
</div>

<p>Running this project gives us a pretty simple screen:</p>
<div id="attachment_356" class="wp-caption alignnone" style="width: 198px"><a href="http://www.learn-android.com/wp-content/uploads/2011/11/Screen-shot-2011-11-22-at-10.31.09-AM.png"><img class="size-medium wp-image-356" title="Screen shot 2011-11-22 at 10.31.09 AM" src="http://www.learn-android.com/wp-content/uploads/2011/11/Screen-shot-2011-11-22-at-10.31.09-AM-188x300.png" alt="Lots of lists 2: Hello World" width="188" height="300" /></a><p class="wp-caption-text">Lots of lists 2: Hello World</p></div>
<p>We have a project that is up and running&#8211; now we need to actually add a ListView to our layout.</p>
<h1>ListView in LinearLayout</h1>
<p>A ListView is pretty self-explanatory&#8211; it&#8217;s a view that groups child views into a list. While it&#8217;s a little complex behind the scenes (as we&#8217;ll see with the adapter below), the actual layout we need for the view is very simple.</p>
<p>Here is the new main.xml with our ListView.</p>
<div id="gist1386869" class="gist">
      <div class="gist-file">
        <div class="gist-data gist-syntax">



  <div class="file-data">
    <table cellpadding="0" cellspacing="0" class="lines highlight">
      <tr>
        <td class="line-numbers">
          <span class="line-number" id="file-main-xml-L1" rel="file-main-xml-L1">1</span>
          <span class="line-number" id="file-main-xml-L2" rel="file-main-xml-L2">2</span>
          <span class="line-number" id="file-main-xml-L3" rel="file-main-xml-L3">3</span>
          <span class="line-number" id="file-main-xml-L4" rel="file-main-xml-L4">4</span>
          <span class="line-number" id="file-main-xml-L5" rel="file-main-xml-L5">5</span>
          <span class="line-number" id="file-main-xml-L6" rel="file-main-xml-L6">6</span>
          <span class="line-number" id="file-main-xml-L7" rel="file-main-xml-L7">7</span>
          <span class="line-number" id="file-main-xml-L8" rel="file-main-xml-L8">8</span>
          <span class="line-number" id="file-main-xml-L9" rel="file-main-xml-L9">9</span>
          <span class="line-number" id="file-main-xml-L10" rel="file-main-xml-L10">10</span>
          <span class="line-number" id="file-main-xml-L11" rel="file-main-xml-L11">11</span>
          <span class="line-number" id="file-main-xml-L12" rel="file-main-xml-L12">12</span>
          <span class="line-number" id="file-main-xml-L13" rel="file-main-xml-L13">13</span>
          <span class="line-number" id="file-main-xml-L14" rel="file-main-xml-L14">14</span>
          <span class="line-number" id="file-main-xml-L15" rel="file-main-xml-L15">15</span>
          <span class="line-number" id="file-main-xml-L16" rel="file-main-xml-L16">16</span>
        </td>
        <td class="line-data">
          <pre class="line-pre"><div class="line" id="file-main-xml-LC1"><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></div><div class="line" id="file-main-xml-LC2"><span class="nt">&lt;LinearLayout</span> <span class="na">xmlns:android=</span><span class="s">&quot;http://schemas.android.com/apk/res/android&quot;</span></div><div class="line" id="file-main-xml-LC3">    <span class="na">android:layout_width=</span><span class="s">&quot;fill_parent&quot;</span></div><div class="line" id="file-main-xml-LC4">    <span class="na">android:layout_height=</span><span class="s">&quot;fill_parent&quot;</span></div><div class="line" id="file-main-xml-LC5">    <span class="na">android:orientation=</span><span class="s">&quot;vertical&quot;</span> <span class="nt">&gt;</span></div><div class="line" id="file-main-xml-LC6">&nbsp;</div><div class="line" id="file-main-xml-LC7">    <span class="nt">&lt;TextView</span></div><div class="line" id="file-main-xml-LC8">        <span class="na">android:layout_width=</span><span class="s">&quot;fill_parent&quot;</span></div><div class="line" id="file-main-xml-LC9">        <span class="na">android:layout_height=</span><span class="s">&quot;wrap_content&quot;</span></div><div class="line" id="file-main-xml-LC10">        <span class="na">android:text=</span><span class="s">&quot;@string/hello&quot;</span> <span class="nt">/&gt;</span></div><div class="line" id="file-main-xml-LC11">    </div><div class="line" id="file-main-xml-LC12">    <span class="nt">&lt;ListView</span> <span class="na">android:id=</span><span class="s">&quot;@+id/list&quot;</span></div><div class="line" id="file-main-xml-LC13">        <span class="na">android:layout_width=</span><span class="s">&quot;fill_parent&quot;</span></div><div class="line" id="file-main-xml-LC14">        <span class="na">android:layout_height=</span><span class="s">&quot;fill_parent&quot;</span> <span class="nt">/&gt;</span></div><div class="line" id="file-main-xml-LC15">&nbsp;</div><div class="line" id="file-main-xml-LC16"><span class="nt">&lt;/LinearLayout&gt;</span></div></pre>
        </td>
      </tr>
    </table>
  </div>

        </div>

        <div class="gist-meta">
          <a href="https://gist.github.com/kenleycapps/1386869/raw/dd5ea0782d67a7ba7fcdeec3cbf0227f23b20b7a/main.xml" style="float:right">view raw</a>
          <a href="https://gist.github.com/kenleycapps/1386869#file-main-xml" style="float:right; margin-right:10px; color:#666;">main.xml</a>
          <a href="https://gist.github.com/kenleycapps/1386869">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
        </div>
      </div>
</div>

<p>Pretty simple right? That&#8217;s all we&#8217;ll need for the final product. In real-world scenarios we would likely see a much more complex layout in main.xml, however, we have achieved our first object: <strong>ListView within a standard layout</strong>.</p>
<p>The next objective covers the majority of this tutorial, which is binding custom objects using a custom adapter.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Half Dozen Hello Worlds Part 6 &#8211; Opening Activities</title>
		<link>http://www.learn-android.com/2011/11/21/half-dozen-hello-worlds-part-6-opening-activities/</link>
		<comments>http://www.learn-android.com/2011/11/21/half-dozen-hello-worlds-part-6-opening-activities/#comments</comments>
		<pubDate>Tue, 22 Nov 2011 03:46:11 +0000</pubDate>
		<dc:creator>Sheridan</dc:creator>
				<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://www.learn-android.com/?p=310</guid>
		<description><![CDATA[Our final article in the Half Dozen Hello Worlds will introduce you to activities. What is an activity? It is a single screen that a user interacts with on their Android device. You&#8217;ve actually been using activities throughout these tutorials, we just haven&#8217;t examined them. First, let&#8217;s create a new Android Project. Here is the [...]]]></description>
			<content:encoded><![CDATA[<p>Our final article in the Half Dozen Hello Worlds will introduce you to activities.  What is an activity?  It is a single screen that a user interacts with on their Android device.  You&#8217;ve actually been using activities throughout these tutorials, we just haven&#8217;t examined them.</p>
<p>First, let&#8217;s create a new Android Project.</p>
<p><img src="http://www.learn-android.com/wp-content/uploads/2011/11/1_New_Project.png" alt="New Project" title="1_New_Project" width="521" height="438" class="size-full wp-image-325" /></p>
<p>Here is the source code that is generated for us:</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;">com.learnandroid.helloworld</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorld6Activity <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span>
    <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>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Since we are discussing Activities, you should notice that we are importing android.app.Activity.  You should also note that our class is extending Activity.  This gives us basic functionality to present our screen to the user without much work on our part.  There are other classes we can use, that also inherit from Activity to provide us different functionality.  In <a href="http://www.learn-android.com/2009/12/05/lots-of-lists-1-simple/">this article</a> we used a ListActivity to present a list to the user.  In fact, let&#8217;s go ahead and make the changes described in that article to our code.</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;">com.learnandroid.helloworld</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.ListActivity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.ArrayAdapter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.ListAdapter</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> HelloWorld6Activity <span style="color: #000000; font-weight: bold;">extends</span> ListActivity <span style="color: #009900;">&#123;</span>
    <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>
&nbsp;
        ListAdapter adapter <span style="color: #339933;">=</span> createAdapter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setListAdapter<span style="color: #009900;">&#40;</span>adapter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Creates and returns a list adapter for the current list activity
     * @return
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> ListAdapter createAdapter<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;">// Create some mock data</span>
    	<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> testValues <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
    			<span style="color: #0000ff;">&quot;Hello 1&quot;</span>,
    			<span style="color: #0000ff;">&quot;Hello 2&quot;</span>
    	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #666666; font-style: italic;">// Create a simple array adapter (of type string) with the test values</span>
    	ListAdapter adapter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayAdapter<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, android.<span style="color: #006633;">R</span>.<span style="color: #006633;">layout</span>.<span style="color: #006633;">simple_list_item_1</span>, testValues<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #000000; font-weight: bold;">return</span> adapter<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You might have noticed that I named the items on my list &#8220;Hello 1&#8243; and &#8220;Hello 2&#8243;.  That&#8217;s because we are going to use Intents to call the programs we created in <a href="http://www.learn-android.com/2009/11/16/half-dozen-hello-worlds-part-1/">Part 1</a> and <a href="http://www.learn-android.com/2009/11/19/half-dozen-hello-worlds-part-2/">Part 2</a> of this series.</p>
<p>Next: <a href="http://www.learn-android.com/2011/11/21/half-dozen-hello-worlds-part-6-opening-activities/2/">Intents</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.learn-android.com/2011/11/21/half-dozen-hello-worlds-part-6-opening-activities/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Android Layout Tutorial</title>
		<link>http://www.learn-android.com/2010/01/05/android-layout-tutorial/</link>
		<comments>http://www.learn-android.com/2010/01/05/android-layout-tutorial/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 04:31:50 +0000</pubDate>
		<dc:creator>Sheridan</dc:creator>
				<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://www.learn-android.com/?p=222</guid>
		<description><![CDATA[This tutorial covers what a Layout is and gives examples of the five types of layout that come with the Android Framework.  Examples are given for AbsoluteLayout, FrameLayout, LinearLayout, RelativeLayout, and TableLayout.]]></description>
			<content:encoded><![CDATA[<p>An Android layout is a class that handles arranging the way its children appear on the screen.  Anything that is a View (or inherits from View) can be a child of a layout.  All of the layouts inherit from ViewGroup (which inherits from View) so you can nest layouts.  You could also create your own custom layout by making a class that inherits from ViewGroup.  </p>
<p>The standard Layouts are:</p>
<p><a href="http://www.learn-android.com/2010/01/05/android-layout-tutorial/2/">AbsoluteLayout</a><br />
<a href="http://www.learn-android.com/2010/01/05/android-layout-tutorial/3/">FrameLayout</a><br />
<a href="http://www.learn-android.com/2010/01/05/android-layout-tutorial/4/">LinearLayout</a><br />
<a href="http://www.learn-android.com/2010/01/05/android-layout-tutorial/5/">RelativeLayout</a><br />
<a href="http://www.learn-android.com/2010/01/05/android-layout-tutorial/6/">TableLayout</a></p>
<p>In this article we will examine each of these layouts in detail.</p>
<p>I have also created a demo project that uses the code samples from this tutorial and from the <a href="http://www.learn-android.com/2009/12/05/lots-of-lists-1-simple/">Lots of Lists: Part 1, Simple List Activity tutorial</a>.<br />
You can download the demo source code <a href="http://www.learn-android.com/projects/LayoutDemo.zip">here</a>.</p>
<p>Next: <a href="http://www.learn-android.com/2010/01/05/android-layout-tutorial/2/">AbsoluteLayout</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.learn-android.com/2010/01/05/android-layout-tutorial/feed/</wfw:commentRss>
		<slash:comments>70</slash:comments>
		</item>
		<item>
		<title>Half Dozen Hello Worlds Part 5 – Android and OpenGL</title>
		<link>http://www.learn-android.com/2009/12/17/half-dozen-hello-worlds-part-5-opengl/</link>
		<comments>http://www.learn-android.com/2009/12/17/half-dozen-hello-worlds-part-5-opengl/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 04:28:55 +0000</pubDate>
		<dc:creator>Sheridan</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[3D]]></category>

		<guid isPermaLink="false">http://www.learn-android.com/?p=188</guid>
		<description><![CDATA[Half Dozen Hello Worlds explores different forms of Android I/O through six simple programs. Part 5 uses OpenGL, a 3D graphics library, to render Hello World using Line Segments.]]></description>
			<content:encoded><![CDATA[<p>This is the last post of the series &#8220;Half Dozen Hello Worlds&#8221; where we explored different ways of communicating with Android users.</p>
<p><a href="http://www.learn-android.com/2009/11/16/half-dozen-hello-worlds-part-1/">Part 1</a> used a simple TextView widget to display &#8220;Hello World&#8221; on the screen.</p>
<p><a href="http://www.learn-android.com/2009/11/19/half-dozen-hello-worlds-part-2/">Part 2</a> explored using a Layout XML.</p>
<p><a href="http://www.learn-android.com/2009/11/25/half-dozen-hello-worlds-part-3/">Part 3</a> showed you two different kinds of pop-up message.</p>
<p><a href="http://www.learn-android.com/2009/12/03/half-dozen-hello-worlds-part-4/">Part 4</a> explored using Text to Speech to have Android actually say &#8220;Hello World&#8221; out loud.</p>
<p>When I was in college one of my favorite professors was Dr. Heath.  He taught Calculus and he would often spend ten or fifteen minutes in front of the board showing the steps to work out a single problem.  When he finished and the answer was 3 (or whatever) he would face the class with a big grin on his face and say, &#8220;That&#8217;s how you get 3, THE HARD WAY!&#8221;  Today we are going to display &#8220;Hello World&#8221; THE HARD WAY.</p>
<p>OpenGL is a very powerful Graphics Library (which is what GL stands for incidentally) which is intended to be hardware independent.  The version of OpenGL that is implemented on Android is called <a href="http://www.khronos.org/opengles/" target="_blank">OpenGL ES</a> 1.0 (The ES stands for Embedded Systems).  If you are unfamiliar with OpenGL all you really need to know right now is that it is a library for rendering 3D graphics.  We will be rendering our Hello World as a series of Line Segments in 3D space.  Rendering, in case you don&#8217;t know, simply refers to displaying three dimensional objects on a two dimensional surface (i.e. your screen).</p>
<p>This is a more advanced topic and will, by necessity, be more involved than previous examples.  I will try my best to explain each part in detail, but please leave a comment if you have any questions as there is a lot to cover.  We are only going to scratch the surface of OpenGL on Android here, but it should be enough to get you started.</p>
<p>First we are going to create a new Android Project using Android 1.5 (Minimum SDK 3).  I will assume you know how to do this since it was covered in <a href="http://www.learn-android.com/2009/11/16/half-dozen-hello-worlds-part-1/">Part 1</a>.  I named my main Activity Hello5.  Here is the source code from that activity.</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;">com.learnandroid.helloworld</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.opengl.GLSurfaceView</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Hello5 <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span>
    <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>
        GLSurfaceView drawSurface <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GLSurfaceView<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>
        drawSurface.<span style="color: #006633;">setRenderer</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> HelloRenderer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setContentView<span style="color: #009900;">&#40;</span>drawSurface<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first thing you will probably notice here is the new import statement at the top.</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.opengl.GLSurfaceView</span><span style="color: #339933;">;</span></pre></div></div>

<p>GLSurfaceView is a new kind of view that will provide us with a drawing surface on which to render our 3D objects.  GLSurfaceView was introduced in Android 1.5 and is the reason we selected 1.5 for our minimum SDK for this project.  GLSurfaceView will take care of providing us a place to display our graphics with very little effort on our part.</p>
<p>Inside on create we&#8217;ve added the following lines to the default code.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">GLSurfaceView drawSurface <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GLSurfaceView<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>
drawSurface.<span style="color: #006633;">setRenderer</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> HelloRenderer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The first line initialized the GLSurfaceView with a Context (our Activity).  The second line tells our GLSurfaceView the name of the class that will perform the rendering. When you enter this code you will get an error and Eclipse will underline HelloRenderer in red.  Hover over it, and select Create class &#8216;HelloRenderer&#8217;.</p>
<p><img class="aligncenter size-full wp-image-193" title="CreateHelloRenderer" src="http://www.learn-android.com/wp-content/uploads/2009/12/CreateHelloRenderer.png" alt="CreateHelloRenderer" width="698" height="259" />On the create screen that comes up you can leave all of the defaults and just click finish.  On the next page we will look at HelloRenderer.java in detail.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learn-android.com/2009/12/17/half-dozen-hello-worlds-part-5-opengl/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Lots of Lists: Part 1, Simple List Activity</title>
		<link>http://www.learn-android.com/2009/12/05/lots-of-lists-1-simple/</link>
		<comments>http://www.learn-android.com/2009/12/05/lots-of-lists-1-simple/#comments</comments>
		<pubDate>Sun, 06 Dec 2009 05:27:03 +0000</pubDate>
		<dc:creator>Kenley</dc:creator>
				<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://www.learn-android.com/?p=140</guid>
		<description><![CDATA[A simple look at ListActivity and using an Adapter to connect data]]></description>
			<content:encoded><![CDATA[<p>In most mobile applications, you&#8217;re going to be presenting your users a list of something. Most of the time, it&#8217;s not as simple as an array of strings; your data may be stored away in a local Sqlite database, or perhaps behind a RESTful API. In any case, we&#8217;ll be taking a look at what it takes to begin with a simple ListActivity that can be easily updated later on.</p>
<p>Create a new Android Project (1.1+) and name the activity MyListActivity (or whatever you prefer.) Run the app to ensure you&#8217;re working off a valid clean slate.</p>
<p>Next, modify your activity (MyListActivity) to extend the Android class ListActivity and adjust your code to look like the following:</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;">com.learnandroid.listviewtutorial.simple</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.ListActivity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.ListAdapter</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> MainListView <span style="color: #000000; font-weight: bold;">extends</span> ListActivity <span style="color: #009900;">&#123;</span>
    <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>
&nbsp;
        ListAdapter adapter <span style="color: #339933;">=</span> createAdapter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setListAdapter<span style="color: #009900;">&#40;</span>adapter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Creates and returns a list adapter for the current list activity
     * @return
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> ListAdapter createAdapter<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: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So far, so good. We have a method to create our list adapter, so essentially, our <strong>onCreate()</strong> method is done. Let&#8217;s move on to adapters.</p>
<p><strong>Adapters</strong> basically glue a collection of objects to an activity, usually by providing a mapping or instructions on how to render each object in the list for the activity. In our case, we have a simple list of strings, so our adapter is pretty straightforward (and in fact, is already provided by Android: <strong>ArrayAdapter</strong>)</p>
<p>Modify your <strong>createAdapter()</strong> method to look like the following:</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;">/**
     * Creates and returns a list adapter for the current list activity
     * @return
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> ListAdapter createAdapter<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;">// Create some mock data</span>
    	<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> testValues <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
    			<span style="color: #0000ff;">&quot;Test1&quot;</span>,
    			<span style="color: #0000ff;">&quot;Test2&quot;</span>,
    			<span style="color: #0000ff;">&quot;Test3&quot;</span>
    	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #666666; font-style: italic;">// Create a simple array adapter (of type string) with the test values</span>
    	ListAdapter adapter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayAdapter<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, android.<span style="color: #006633;">R</span>.<span style="color: #006633;">layout</span>.<span style="color: #006633;">simple_list_item_1</span>, testValues<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #000000; font-weight: bold;">return</span> adapter<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This encapsulates everything we need to get an adapter up and running. Typically here we may make a call-out to data, which may be a Sqlite query or a RESTful GET request, but for now we&#8217;re using simple strings. We create an ArrayAdapter and specify three things: <strong>Context</strong>, <strong>Layout</strong>, and <strong>Data</strong>.</p>
<p>The <strong>Context</strong> is simply where the list came from. In our app it&#8217;s simple, but sometimes it can get a little complex when you&#8217;re passing intents back and forth across activities, so this is used to keep a reference to the owner activity at all times.<br />
The <strong>Layout</strong> is where the data will be rendered (and how it will look.) We&#8217;re using a built-in Android layout for our needs (simple_list_item_1).<br />
The <strong>Data</strong> is what will be rendered, and is a simple list of strings.</p>
<p>Final result should look like the following:</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;">com.learnandroid.listviewtutorial.simple</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.ListActivity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.ArrayAdapter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.ListAdapter</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> MainListView <span style="color: #000000; font-weight: bold;">extends</span> ListActivity <span style="color: #009900;">&#123;</span>
    <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>
&nbsp;
        ListAdapter adapter <span style="color: #339933;">=</span> createAdapter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setListAdapter<span style="color: #009900;">&#40;</span>adapter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Creates and returns a list adapter for the current list activity
     * @return
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> ListAdapter createAdapter<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;">// Create some mock data</span>
    	<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> testValues <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
    			<span style="color: #0000ff;">&quot;Test1&quot;</span>,
    			<span style="color: #0000ff;">&quot;Test2&quot;</span>,
    			<span style="color: #0000ff;">&quot;Test3&quot;</span>
    	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #666666; font-style: italic;">// Create a simple array adapter (of type string) with the test values</span>
    	ListAdapter adapter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayAdapter<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, android.<span style="color: #006633;">R</span>.<span style="color: #006633;">layout</span>.<span style="color: #006633;">simple_list_item_1</span>, testValues<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #000000; font-weight: bold;">return</span> adapter<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Run the app, and you should see something like this:</p>
<div id="attachment_144" class="wp-caption alignnone" style="width: 221px"><img class="size-medium wp-image-144" title="Lots of Lists - Simple List - 1" src="http://www.learn-android.com/wp-content/uploads/2009/11/listViewTutorial-run1-211x300.png" alt="A screen shot of an example list of strings" width="211" height="300" /><p class="wp-caption-text">A screen shot of an example list of strings</p></div>
<p>And there you have a nice introduction to a ListActivity that can be expanded upon by focusing on the <strong>createAdapter()</strong> method. In Part 2, we&#8217;ll explore using more complex Object lists and custom Adapters, as well as a brief introduction to the layout files. Thanks for reading!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learn-android.com/2009/12/05/lots-of-lists-1-simple/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Half Dozen Hello Worlds Part 4 &#8211; Android Text to Speech</title>
		<link>http://www.learn-android.com/2009/12/03/half-dozen-hello-worlds-part-4/</link>
		<comments>http://www.learn-android.com/2009/12/03/half-dozen-hello-worlds-part-4/#comments</comments>
		<pubDate>Fri, 04 Dec 2009 03:28:56 +0000</pubDate>
		<dc:creator>Sheridan</dc:creator>
				<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://www.learn-android.com/?p=138</guid>
		<description><![CDATA[Half Dozen Hello Worlds explores different forms of Android I/O through six simple programs. Part 4 has Android actually saying "Hello World" using its Text to Speech capabilities.]]></description>
			<content:encoded><![CDATA[<p>This is part 4 of a multi-part series that looks at some basic Android I/O using simple Hello World programs. At this point I’ll assume you know how to make a new Android Project in Eclipse as that was covered in <a href="http://www.learn-android.com/2009/11/16/half-dozen-hello-worlds-part-1/">Part 1</a>.</p>
<p>In this part we are going to look at using Android&#8217;s Text to Speech capabilities to get our Android device (or emulator) to actually say &#8220;Hello World&#8221; to the user.  Text to Speech was introduced in Android 1.6, so when you create your new Android project make sure your minimum required SDK is set to Android 1.6 (or API level 4).  Here is the code we are going to use to make our Android device speak to us.</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;">com.learnandroid.helloworld</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.speech.tts.TextToSpeech</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.speech.tts.TextToSpeech.OnInitListener</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> Hello4 <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #000000; font-weight: bold;">implements</span> OnInitListener
<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> TextToSpeech tts<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> TTS_CHECK_CODE <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><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>
&nbsp;
        tts <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TextToSpeech<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</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> onInit<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> initStatus<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>initStatus <span style="color: #339933;">==</span> TextToSpeech.<span style="color: #006633;">SUCCESS</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			tts.<span style="color: #006633;">speak</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World&quot;</span>, TextToSpeech.<span style="color: #006633;">QUEUE_FLUSH</span>, <span style="color: #000066; font-weight: bold;">null</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></div></div>

<p>The first thing you should notice here is the line:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Hello4 <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #000000; font-weight: bold;">implements</span> OnInitListener</pre></div></div>

<p>This is different from what we have seen before because it has the extra part at the end &#8220;implements OnInitListener&#8221;.  If you are familiar with Java you know that implements the OnInitListener interface.  This will allow our activity to respond to the event triggered when the TextToSpeech engine finishes initializing.  We will use the onInit method for this purpose later in our code.</p>
<p>Inside the class I&#8217;ve defined a class variable:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> TextToSpeech tts<span style="color: #339933;">;</span></pre></div></div>

<p>so it will be available to both methods in our activity.  Then in the onCreate method I initialize the variable using the TextToSpeech class.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">tts <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TextToSpeech<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you get an error on this line make sure you have the import android.speech.tts.TextToSpeech; statement at the top. in Eclipse you can use Ctrl + Shift + O to automatically update your import statements.</p>
<p>The constructor for the TextToSpeech class takes two parameters.  The first specifies the the context in which we&#8217;re running, and the second is the OnInitListener that should be called.  Since our Hello4 class inherits from Activity we can use it for the context, and since it implements OnInitListener we can use it for the OnInitListener.  Thus I simply provided the keyword &#8220;this&#8221; for both parameters.</p>
<p>Since we specified our class as the onInitListener to use, the class method onInit will automatically be called when the TextToSpeech class is initialized.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onInit<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> initStatus<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>initStatus <span style="color: #339933;">==</span> TextToSpeech.<span style="color: #006633;">SUCCESS</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			tts.<span style="color: #006633;">speak</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World&quot;</span>, TextToSpeech.<span style="color: #006633;">QUEUE_FLUSH</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This method is passed a integer indicating the status after initialization.  We will compare this status code to the constant provided by the TextToSpeech class to indicate successful initialization (TextToSpeech.SUCCESS).  If Initialization was successful we will call the speak method to actually have your android device speak &#8220;Hello World&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">tts.<span style="color: #006633;">speak</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World&quot;</span>, TextToSpeech.<span style="color: #006633;">QUEUE_FLUSH</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This method takes three parameters.  The first is the text that should be spoken.  The second is the mode to use for speech.  The mode used for speech can allow you to either speak the text immediately (using QUEUE_FLUSH as we did here) or queue up multiple strings to be read.  Since this is intended to be a very simple example we won&#8217;t cover this functionality here.  The final parameter allows you to pass a list of additional parameters.  This is also outside the scope of this example.</p>
<p>This is the end of our tutorial on Text To Speech.  Thanks for reading and be sure to check back; there is one more Hello World example forthcoming.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learn-android.com/2009/12/03/half-dozen-hello-worlds-part-4/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Half Dozen Hello Worlds Part 3 &#8211; Using Popup Notifications</title>
		<link>http://www.learn-android.com/2009/11/25/half-dozen-hello-worlds-part-3/</link>
		<comments>http://www.learn-android.com/2009/11/25/half-dozen-hello-worlds-part-3/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 04:54:22 +0000</pubDate>
		<dc:creator>Sheridan</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[android 1.1]]></category>
		<category><![CDATA[android 1.5]]></category>

		<guid isPermaLink="false">http://www.learn-android.com/?p=87</guid>
		<description><![CDATA[Half Dozen Hello Worlds explores different forms of Android I/O through six simple programs.  Part 3 looks at two types of pop-up messages that allow you to communicate with the user outside of the normal program flow.]]></description>
			<content:encoded><![CDATA[<p>This is part 3 of a multi-part series that looks at some basic Android I/O using simple Hello World programs. At this point I&#8217;ll assume you know how to make a new Android Project in Eclipse as that was covered in <a href="http://www.learn-android.com/2009/11/16/half-dozen-hello-worlds-part-1/">Part 1</a>.  In this part we are going to look at two types of pop-up messages that allow you to communicate with the user outside of the normal program flow.</p>
<h1>Toasts</h1>
<p>Imagine you are at a large dinner party.  You want to make a toast to your host.  You raise your glass a call out a toast.  Half the people at the party don&#8217;t notice (none of the kids at the kids table notice at all), but you aren&#8217;t worried about that as you clink glasses with the dozen people who did notice.  The first pop-up we are going to look at is Android&#8217;s Toast, which does exactly this.  It allows you to present a message to the user without any confirmation that they noticed the message.  Build a new project using Android SDK 1.1 and update your Activity Java to look like this.</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;">com.learnandroid.helloworld</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.Gravity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.Toast</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Hello3 <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span>
    <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>
&nbsp;
        Toast helloToast <span style="color: #339933;">=</span> Toast.<span style="color: #006633;">makeText</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #0000ff;">&quot;Hello World&quot;</span>, Toast.<span style="color: #006633;">LENGTH_LONG</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        helloToast.<span style="color: #006633;">setGravity</span><span style="color: #009900;">&#40;</span>Gravity.<span style="color: #006633;">CENTER</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        helloToast.<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>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first line of code we added to the default code generated by the project was this.</p>
<p>Toast helloToast = Toast.makeText(this, &#8220;Hello World&#8221;, Toast.LENGTH_LONG);</p>
<p>This line of code calls the Static method makeText of the Toast class.  The makeText method returns a Toast object that will display the message you provide, in our case &#8220;Hello World&#8221;, for the duration you specify.  The two available durations are Toast.LENGTH_LONG and Toast.LENGTH_SHORT.  Once we have our Toast object we can just call the show method to display it on the screen.  Before displaying our toast, however, I thought I would center it on the screen.  The setGravity method lets you tell Android where the toast should be displayed by using one of the Gravity constants, and then specifying an x offset and y offset.  Since I wanted it in the very center of the screen I used Gravity.Center and specified offsets of 0.</p>
<p>helloToast.setGravity(Gravity.CENTER, 0, 0);</p>
<p>You can see a list of available Gravity constants <a title="Gravity Class on android.com" href="http://developer.android.com/reference/android/view/Gravity.html" target="_blank">here</a>.  When you run this code you should see a small pop-up with the message &#8220;Hello World&#8221; appear, and then disappear automatically without any user interaction.  On the next page we&#8217;ll look at a pop-up that requires user interaction.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learn-android.com/2009/11/25/half-dozen-hello-worlds-part-3/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Half Dozen Hello Worlds Part 2 &#8211; Using Layout XML</title>
		<link>http://www.learn-android.com/2009/11/19/half-dozen-hello-worlds-part-2/</link>
		<comments>http://www.learn-android.com/2009/11/19/half-dozen-hello-worlds-part-2/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 03:51:43 +0000</pubDate>
		<dc:creator>Sheridan</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[I/O]]></category>
		<category><![CDATA[android 1.1]]></category>

		<guid isPermaLink="false">http://www.learn-android.com/?p=71</guid>
		<description><![CDATA[Half Dozen Hello Worlds explores different forms of Android I/O through six simple programs.  Part two of this series introduces the use of Layout XML files.]]></description>
			<content:encoded><![CDATA[<p>This is part 2 of a multipart series that looks at some basic Android I/O using some simple Hello World programs.  If you haven&#8217;t already read it you should probably start with <a href="http://www.learn-android.com/2009/11/16/half-dozen-hello-worlds-part-1/">Part 1</a>.  For this part we are going to create a new project with these settings.</p>
<p><img class="aligncenter size-full wp-image-59" title="New Project Hello World 2" src="http://www.learn-android.com/wp-content/uploads/2009/11/8-New-Project.png" alt="New Project Hello World 2" width="613" height="824" />The default Hello2.java will look very similar to the default Hello.java from <a href="http://www.learn-android.com/2009/11/16/half-dozen-hello-worlds-part-1/">Part 1</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;">com.learnandroid.hellowworld</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Hello2 <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span>
    <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>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This time, however, we are going to find out more about the line setContentView(R.layout.main);  In the first example we used setContentView to render our TextView to the screen.  By default, however, Android is designed to use XML files to setup your UI.  In addition to the src folder, which holds the source code intended to be edited by the developer, there is a gen folder and a res folder.  The gen folder holds automatically generated code.  If you open this folder and expand the namespace you should see R.java.  We&#8217;ll come back to R.java in a moment.  Before we open that file, lets also expand the contents of the res folder.  The res folder holds resources for your android app.  By default you should see three folders in here, drawable (which holds an android icon), layout (which contains main.xml), and values (which contains strings.xml).  If you open main.xml and click on the main.xml tab you should see the XML that provides your activity its layout.</p>
<p><img class="aligncenter size-full wp-image-62" title="Hello World 2 Main.xml" src="http://www.learn-android.com/wp-content/uploads/2009/11/10-Main-XML.png" alt="Hello World 2 Main.xml" width="609" height="310" />We aren&#8217;t going to go into too much detail in this post on the UI XML.  I will highlight, however, that the main node is LinearLayout which is a Layout Container that simply lines its children up in order one after another.  android:orientation indicates whether they are lined up vertically or horizontally.  The one child node we have in LinearLayout in a TextView.  This is the same control we used in <a href="http://www.learn-android.com/?p=44">Part 1</a>, but instead of instantiating it in our code we are using the UI XML to tell Android to provide us with one.  The other thing I want you to notice is the line android:text=&#8221;@string/hello&#8221;.  This is going to set the value of our TextView.  If you go back to the res folder, open values, open strings.xml and click on the strings.xml tab you can see exactly what value we are putting into our TextView.  In strings.xml there is a line</p>
<p>&lt;string name=&#8221;hello&#8221;&gt;Hello World, Hello2!&lt;/string&gt;</p>
<p>so android:text=&#8221;@string/hello&#8221; will get this value and place it in the TextView that is displayed to the user.  Let&#8217;s go ahead and change the value to our simple &#8220;Hello World&#8221; now.</p>
<p>&lt;string name=&#8221;hello&#8221;&gt;Hello World&lt;/string&gt;</p>
<p>Go ahead and save your change and close both strings.xml and main.xml.  At this point you are probably wondering how Android gets the information from the XML files and uses them in your program.  Go back to the gen folder and open R.java and we&#8217;ll see the answer.</p>
<p><img class="aligncenter size-full wp-image-65" title="11-R Java" src="http://www.learn-android.com/wp-content/uploads/2009/11/11-R-Java.png" alt="11-R Java" width="453" height="436" />When R.Java is generated it creates a class for each resource type (drawable, layout, and string) and variables in each class providing access to your resources.  so R.layout.main actually maps to your main.xml file via this R Class and the layout inner class.  If you don&#8217;t exactly understand what is going on here don&#8217;t worry.  Just remember that R will let you access items in your res folder so the line.</p>
<p>setContentView(R.layout.main);</p>
<p>tells android to use the layout in your main.xml file in res/layout.  Since we edited the value in string.xml we should be able to run this program and see our &#8220;Hello World&#8221; displayed.  Before we move on to part three, let&#8217;s modify this program a little bit to show how to access a View element (such as our TextView) in our code.<!--<br /--> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.learn-android.com/2009/11/19/half-dozen-hello-worlds-part-2/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Half Dozen Hello Worlds Part 1 &#8211; Your First Android Project</title>
		<link>http://www.learn-android.com/2009/11/16/half-dozen-hello-worlds-part-1/</link>
		<comments>http://www.learn-android.com/2009/11/16/half-dozen-hello-worlds-part-1/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 05:01:48 +0000</pubDate>
		<dc:creator>Sheridan</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[I/O]]></category>

		<guid isPermaLink="false">http://www.learn-android.com/?p=44</guid>
		<description><![CDATA[Half Dozen Hello Worlds explores different forms of Android I/O through six simple programs.  Part 1 gets you creating your first android program and using a simple widget to display "Hello World" to the user.]]></description>
			<content:encoded><![CDATA[<p>Hello World is a traditional first program because it shows the very simple task of letting the user know you are there (or letting them know that you know that they are there).  It&#8217;s simple I/O to send a simple message.  I am going to give a very brief tour of some of the ways you can communicate with a user in Android in this post.  Half Dozen Hello Worlds will highlight some simple I/O and get you started writing your first Android programs.  First let&#8217;s go ahead and open Eclipse, where you <a href="http://www.learn-android.com/2009/11/03/getting-setup-with-android/" target="_blank">have already setup Android</a>, and create a new Android Project.  Go to File -&gt; New -&gt; Project.  Select Android Project and click next.</p>
<p><img class="aligncenter size-full wp-image-45" title="New Project" src="http://www.learn-android.com/wp-content/uploads/2009/11/1-New-Project-.png" alt="New Project" width="613" height="500" /></p>
<p>Fill out the information on the next screen for your project.</p>
<p><img class="aligncenter size-full wp-image-46" title="New Android Project" src="http://www.learn-android.com/wp-content/uploads/2009/11/2-New-Android-Project.png" alt="New Android Project" width="613" height="824" /></p>
<p>You can put whatever you like for the project name, and application name.  Note that I selected Android SDK 1.1.  It is a good idea to select the oldest SDK which has all of the features your program needs, to increase compatibility across devices.  You can think of Create Activity as being similar to create Main Method.  The name of the Activity you place here is the class that will be called when Android tries to run your code.  Once you click Finish you should see the Project.<img class="size-full wp-image-47 aligncenter" title="3-src folder" src="http://www.learn-android.com/wp-content/uploads/2009/11/3-src-folder.png" alt="3-src folder" width="254" height="302" /> If you expand the project, then expand the src folder, and then the package you will see a Java file. This file will be named whatever you called your Activity (so mine is Hello.java).  For simplicity I am going to assume you used the same settings I did when creating your project.  If you did not just substitute your Activity name for mine.  Double click on Hello.java and look at the code that Eclipse has already provided you.</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;">com.learnandroid.hellowworld</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Hello <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span>
    <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>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Just to give a quick overview.  The package declaration just places this class in the package we specified when we created the project.  The two import statements just import the minimum Android libraries needed for an Activity.  Since Eclipse created this as an Activity it is going to inherit from the Activity class, and we are overriding onCreate, which is called when an Activity is created.  We call onCreate of the superclass.  Finally, we are at the one line of code we really care about right now.</p>
<p>setContentView() tells android what to display to the user.  We are going to change this from the default so that our code looks like this:</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;">com.learnandroid.helloworld</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.widget.TextView</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Hello <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span>
    <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>
        TextView helloText <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TextView<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>
        helloText.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setContentView<span style="color: #009900;">&#40;</span>helloText<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Let&#8217;s briefly look at exactly what we did before moving on.  TextView is an android widget that displays text.  You can think of it as a label, something the user can read but cannot edit.  We instantiated a new TextView.  Then we called the setText method to set the text of it to &#8220;Hello World&#8221;.</p>
<p>Next we used setContentView to tell Android that our TextView was what we wanted to display in the main part of our application.</p>
<p>Once your code looks like mine, go to the Run Menu and select Run&#8230;  In the Run As Popup select Android Application.  Your Android Emulator should launch.  The Emulator will normally start on a lock screen, but as soon as you unlock the device you should see your application launch.</p>
<p><img class="aligncenter size-full wp-image-52" title="6-Hello 1 Emu" src="http://www.learn-android.com/wp-content/uploads/2009/11/6-Hello-1-Emu.png" alt="6-Hello 1 Emu" width="801" height="563" /></p>
<p>Congratulations!  You have built the first Hello World.  This is not the preferred method for creating an android application, however, because the contents of the View (what the user sees) is being generated directly from the code.  Android prefers that this be separated out of the code and placed into an XML file.  We&#8217;ll look at how to do this next in <a href="http://www.learn-android.com/2009/11/19/half-dozen-hello-worlds-part-2/">Part 2</a> of this series.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learn-android.com/2009/11/16/half-dozen-hello-worlds-part-1/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
