<?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>gabehabe &#187; android sdk</title>
	<atom:link href="http://www.gabehabe.com/blog/tags/android-sdk/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gabehabe.com/blog</link>
	<description>Cool blog, bro.</description>
	<lastBuildDate>Wed, 29 Jun 2011 01:26:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Developing for Android, Part V</title>
		<link>http://www.gabehabe.com/blog/developing-for-android-part5/</link>
		<comments>http://www.gabehabe.com/blog/developing-for-android-part5/#comments</comments>
		<pubDate>Sat, 10 Oct 2009 14:26:59 +0000</pubDate>
		<dc:creator>Danny</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[android development]]></category>
		<category><![CDATA[android sdk]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[layout.xml]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.gabehabe.com/blog/developing-for-android-part-v/</guid>
		<description><![CDATA[Publishing your apps No code in this tutorial&#8230; and this is actually the last in the series. But never fear! I&#8217;ll be writing more tutorials, I&#8217;ve simply written these 5 to get you familiarised with android. Hopefully, you&#8217;re feeling quite confident on some of the aspects now, and you could well be on your way [...]]]></description>
			<content:encoded><![CDATA[<h3>Publishing your apps</h3>
<p>No code in this tutorial&#8230; and this is actually the last in the series. But never fear! I&#8217;ll be writing more tutorials, I&#8217;ve simply written these 5 to get you familiarised with android. Hopefully, you&#8217;re feeling quite confident on some of the aspects now, and you could well be on your way to developing the next killer app!</p>
<p>With that said, I&#8217;m going to explain how you can publish your app. It&#8217;s a simple process, but you also have to sign your apk before you can publish it to the market.</p>
<p>First off, we need to create a <strong>keystore</strong>. To do this, you&#8217;ll need the JDK installed. Open up a command prompt, and change to whereever you want to save the keystore. Then, type the following:<br />
<code>keytool -genkey -v -keystore mykeystore.keystore -alias my_alias -keyalg RSA -validity 10000</code><br />
<strong>NOTES:</strong><br />
- Replace mykeystore.keystore with whatever_you_want_to_call_it.keystore<br />
- Replace my_alias with whatever alias you want to use (example, android)</p>
<p>Also, if you get this error:<br />
<code>'keytool' is not recognised as an internal or external command, operable program or batch file.</code><br />
You may want to set the PATH variable. You can do it in the computer properties, but since this isn&#8217;t exactly a windows tutorial, I won&#8217;t go into that much detail. Instead, let&#8217;s do this:<br />
<code>PATH="C:\Program Files\Java\jdk1.6.0_14\bin"</code><br />
then try the first command again.<br />
<strong>NOTE:</strong> Replace the path with the path of wherever you installed Java. (Typically you&#8217;ll find it in <em>C:\Program Files\Java\jdk%.%.%_%%\bin</em>)<br />
<strong>ANOTHER NOTE:</strong> This is of course a windows path. Depending on your operating system, this may vary.</p>
<p>After executing the keytool command, it will ask you for various details. Simply enter the values, and it will create your keystore. Make sure you take note of your alias and password, it will ask you for it every time you come to sign an app!</p>
<p>For reference, the questions it will ask are:
<ul>
<li>Enter keystore password:
</li>
<li>Re-enter new password:
</li>
<li>What is your first and last name?
</li>
<li>What is the name of your organisational unit?
</li>
<li>What is the name of your organisation?
</li>
<li>What is the name of your City or Locality?
</li>
<li>What is the name of your State or Province?
</li>
<li>What is the two-letter country code for this unit?</li>
</ul>
<p>You now have your keystore. When you want to sign an apk, you will need to use the following command:<br />
<code>jarsigner -verbose -keystore "/path/to/mykeystore.keystore" "/path/to/myapk.apk" my_alias</code><br />
Enter your password, and you will see some output similar to this:<br />
<blockquote>adding: META-INF/ALIAS_NA.SF<br />
adding: META-INF/ALIAS_NA.RSA<br />
signing: res/drawable/icon.png<br />
signing: res/layout/main.xml<br />
signing: AndroidManifest.xml<br />
signing: resources.arsc<br />
signing: classes.dex</p></blockquote>
<p>Your apk file is now signed! (Note that you need to sign it every time before uploading, since building will reset it)</p>
<h3>Publishing your app</h3>
<p>The next step is of course publishing your newly signed apk file to the android market. Simply navigate your web browser to <a href="http://market.android.com/publish/Home" target="_blank">the market home</a> and follow the steps to register as a developer. If you want to register to sell your apps, you will also need to go through an additional process to become a &#8220;registered seller&#8221;.</p>
<p>Once you&#8217;ve registered and logged in, you&#8217;ll reach the page which displays all your registered apps. Of course, if you&#8217;re following this tutorial, you probably don&#8217;t yet have any, so you won&#8217;t have a list of apps to see. :)<br />
<strong>NOTE:</strong> This page uses a hell of a lot of javascript. Make sure it&#8217;s turned on.</p>
<p>When you&#8217;re ready, click on &#8220;Upload Application&#8221;. You will then be presented with a form where you can upload the apk and a maximum of two screenshots, a promotional graphic, and fill in various details such as the title, description and price.</p>
<p>As soon as you click publish, your application will be on the market &#8212; there&#8217;s no approval system like the iPhone&#8217;s app store, so as soon as you search in the market, you should see your app.</p>
<p>You are now officially a dev. Make me proud. :&#8217;)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gabehabe.com/blog/developing-for-android-part5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Developing for Android, Part IV</title>
		<link>http://www.gabehabe.com/blog/developing-for-android-part4/</link>
		<comments>http://www.gabehabe.com/blog/developing-for-android-part4/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 19:08:00 +0000</pubDate>
		<dc:creator>Danny</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[android development]]></category>
		<category><![CDATA[android sdk]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[layout.xml]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.gabehabe.com/blog/developing-for-android-part-iv/</guid>
		<description><![CDATA[Databases and Menus Two for the price of one in this tutorial, actually. We&#8217;re going to learn about SQLite databases and menus. I&#8217;m hoping you&#8217;ll at least be slightly familiar with databases already, though SQLite is really quite simple. You can pick up the basics, of which we&#8217;ll be using in this tutorial, at the [...]]]></description>
			<content:encoded><![CDATA[<h3>Databases and Menus</h3>
<p>Two for the price of one in this tutorial, actually. We&#8217;re going to learn about SQLite databases and menus. I&#8217;m <em>hoping</em> you&#8217;ll at least be <em>slightly</em> familiar with databases already, though SQLite is really quite simple. You can pick up the basics, of which we&#8217;ll be using in this tutorial, at the following links:
<ul>
<li><a href="http://www.sqlite.org/lang_createtable.html">CREATE TABLE</a>
</li>
<li><a href="http://www.sqlite.org/lang_insert.html">INSERT</a>
</li>
<li><a href="http://www.sqlite.org/lang_select.html">SELECT</a></li>
</ul>
<p>It may look a little intimidating, but once you get to grips with it, it&#8217;s actually quite close to English. :)</p>
<p>Let&#8217;s get to it. As usual, we&#8217;re creating a package:</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;">dreamincode.tutorials.dic_tut4</span><span style="color: #339933;">;</span></pre></div></div>

<p>&#8230;and we&#8217;ll import the usual suspects.</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.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></pre></div></div>

<p>Now there&#8217;s quite a lot that we&#8217;re going to import that hasn&#8217;t already been covered, but <em>do not panic</em>. I&#8217;ll explain it all as we go along, and it&#8217;s honestly nothing to worry about. First up, a <code>ListView</code>. This is just another widget, and it&#8217;s&#8230; well, it&#8217;s a list. :)</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.widget.ListView</span><span style="color: #339933;">;</span></pre></div></div>

<p>Next, we&#8217;ll import the stuff related to the menu. We&#8217;ll be using a <code>Menu</code>, and a couple of <code>MenuItem</code>s.</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.view.Menu</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.MenuItem</span><span style="color: #339933;">;</span></pre></div></div>

<p>The next bits are related to the database. The first is self-explanatory, it&#8217;s a database. The second is a <code>Cursor</code>. A cursor is a way we can store temporary results from a table in memory. (We select stuff into it, explained a little later&#8230; keep reading!)</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.database.sqlite.SQLiteDatabase</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.database.Cursor</span><span style="color: #339933;">;</span></pre></div></div>

<p>And finally, we&#8217;re going to import an <code>ArrayList</code> to add our results into, and an <code>ArrayAdapter</code> to basically plug our <code>ArrayList</code> into the list. Again, this will be explained later, at this point all we really have is an overview.</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.widget.ArrayAdapter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span></pre></div></div>

<p>On to the main part of the code, now. :) First off, we&#8217;ll open up our class and add a few variables. We&#8217;ll have a <code>ListView</code>, an <code>SQLiteDatabase</code>, and a few ints for the IDs of our menu items.</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> dic_tut4 <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">ListView</span> list<span style="color: #339933;">;</span>
	SQLiteDatabase db<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> MENU_ADD <span style="color: #339933;">=</span> <span style="color: #003399;">Menu</span>.<span style="color: #006633;">FIRST</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> MENU_QUIT <span style="color: #339933;">=</span> <span style="color: #003399;">Menu</span>.<span style="color: #006633;">FIRST</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></div></div>

<p> You&#8217;ll see why we use these a little later. :)</p>
<p>This is where it gets a little different. I&#8217;ll give you one method at a time, and talk through each line.</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;">/** 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;
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">list</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ListView</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setContentView<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">list</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">openOrCreateDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dic_tut4&quot;</span>, MODE_PRIVATE, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">db</span>.<span style="color: #006633;">execSQL</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;CREATE TABLE IF NOT EXISTS table_of_dics(value REAL)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">update_list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>So, the start is nothing new. Nothing to worry about here. Then we initialise the list. Nothing new here, either. It&#8217;s exactly the same as when we created a dynamic layout in <a href="http://www.dreamincode.net/forums/showtopic130521.htm">part 3</a>. Then we do some new things with our database. First we use <code>openOrCreateDatabase("dic_tut4", MODE_PRIVATE, null);</code><br />
&#8211; this creates a database if it doesn&#8217;t exist, or opens it if it does.<br />
&#8211; parameter 1 is the name of the database we&#8217;re looking to create/open.<br />
&#8211; parameter 2 is the mode, doesn&#8217;t require much explanation at this point. (Don&#8217;t want to give you an information overload) :)<br />
&#8211; parameter 3, you don&#8217;t even need to worry about. If you&#8217;re interested, do some digging on <code>CursorFactory</code>.</p>
<p>Then another new line: <code>this.db.execSQL("CREATE TABLE IF NOT EXISTS table_of_dics(value REAL)");</code><br />
&#8211; this will create a table if it doesn&#8217;t already exist<br />
&#8211; it will add a single field into the table, called value. It is of type REAL, which is just like a <code>double</code> in Java.</p>
<p>And finally, <code>this.update_list();</code> &#8230; this is actually a custom method we&#8217;ll be writing to query the database and populate the list. More info later, for now let&#8217;s focus on the menu.</p>
<h3>Creating a menu</h3>
<p>Simple stuff actually. We use <code>onCreateOptionsMenu()</code>, which is called when the user hits the menu button.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    @Override 
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onCreateOptionsMenu<span style="color: #009900;">&#40;</span><span style="color: #003399;">Menu</span> menu<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	menu.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, MENU_ADD, <span style="color: #cc66cc;">0</span>, <span style="color: #0000ff;">&quot;Add&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setIcon</span><span style="color: #009900;">&#40;</span>android.<span style="color: #006633;">R</span>.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">ic_menu_add</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	menu.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, MENU_QUIT, <span style="color: #cc66cc;">0</span>, <span style="color: #0000ff;">&quot;Quit&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setIcon</span><span style="color: #009900;">&#40;</span>android.<span style="color: #006633;">R</span>.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">ic_menu_close_clear_cancel</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreateOptionsMenu</span><span style="color: #009900;">&#40;</span>menu<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>We&#8217;re simply adding 2 items: &#8220;Add&#8221;, and &#8220;Quit&#8221;. We also set the icons for them using some default android resources. If you want a full list, <a href="http://www.screaming-penguin.com/info/android_drawables/android_drawables.html">this site</a> is pretty cool.</p>
<p>The next method we&#8217;ll be using is <code>onOptionsItemSelected()</code>, which is called when the user selects an item in the menu. We use the IDs we assigned in <code>onCreateOptionsMenu()</code> MENU_ADD and MENU_QUIT to decide what to do.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onOptionsItemSelected<span style="color: #009900;">&#40;</span><span style="color: #003399;">MenuItem</span> item<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	<span style="color: #000000; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>item.<span style="color: #006633;">getItemId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    		<span style="color: #000000; font-weight: bold;">case</span> MENU_ADD<span style="color: #339933;">:</span>
	        	db.<span style="color: #006633;">execSQL</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO table_of_dics(value) VALUES(&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	        	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">update_list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    			<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    		<span style="color: #000000; font-weight: bold;">case</span> MENU_QUIT<span style="color: #339933;">:</span>
    			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">db</span>.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">finish</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #009900;">&#125;</span>
&nbsp;
    	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onOptionsItemSelected</span><span style="color: #009900;">&#40;</span>item<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>So, we&#8217;re using a switch() to find the ID of the menu item which called this method. If it&#8217;s add, we&#8217;re going to perform some SQL and insert a random value into the table. Then we&#8217;ll update the list, just like we did in <code>onCreate()</code>. (Remember, we&#8217;re defining this method ourselves later)</p>
<p>If the user selected the &#8220;Quit&#8221; option, we&#8217;ll simple <code>close</code> the database and <code>finish</code> the activity. :)</p>
<p><strong>NOTE:</strong> The SQL used here, INSERT INTO will insert a random value into the table. Remember to refer to the links at the beginning if you get stuck on the SQL.</p>
<p>Lastly, we&#8217;re going to define our method for updating the list. It&#8217;s quite simple, but the code is quite different to what we&#8217;ve done so far. If you&#8217;ve ever done any database work through software/web before, you may notice a few similarities here. We basically execute the query. Then loop through each row in the result, adding it into an <code>ArrayList</code>. Lastly, we set the adapter for the list to display the arraylist we created. The code looks a tad complex, but it&#8217;s really quite simple:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> update_list<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	ArrayList<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> db_results <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&gt;&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #003399;">Cursor</span> cursor <span style="color: #339933;">=</span> db.<span style="color: #006633;">rawQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT value FROM table_of_dics ORDER BY value&quot;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>cursor.<span style="color: #006633;">moveToNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    	db_results.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>cursor.<span style="color: #006633;">getDouble</span><span style="color: #009900;">&#40;</span>cursor.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #009900;">&#125;</span>
	    cursor.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">list</span>.<span style="color: #006633;">setAdapter</span><span style="color: #009900;">&#40;</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>, db_results<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>I&#8217;ll break this one up:<br />
<code>ArrayList<String> db_results = new ArrayList><String>();</code> &#8212; create an ArrayList, where we&#8217;ll store the results to display in the list.</p>
<p><code>Cursor cursor = db.rawQuery("SELECT value FROM table_of_dics ORDER BY value", null);</code> &#8212; create a cursor based on the query (this select statement will return all the values stored in the table, and order them)</p>
<p><code>while(cursor.moveToNext()) {</code> &#8212; while there is still something in the cursor to read (while we still have another row to read)</p>
<p><code>db_results.add(String.valueOf(cursor.getDouble(cursor.getColumnIndex("value"))));</code> &#8212; add the value from the row into the arraylist we created.</p>
<p><code>}</code> &#8212; this is self explanatory, really. :)</p>
<p><code>cursor.close();</code> &#8212; close the cursor: we&#8217;re done with it now. We have all our results in the ArrayList, which we can now set as the adapter:</p>
<p><code>this.list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, db_results));</code> &#8212; set the adapter.<br />
- parameter 1: the activity we want it for<br />
- parameter 2: the layout type (we can add multiple widgets to a list item, but simple_list_item_1 is a default in android: simply a single TextView.<br />
- parameter 3: the arraylist which we created and populated earlier.</p>
<p>And that&#8217;s it! Don&#8217;t forget to close off your class</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#125;</span></pre></div></div>

<p>Complete 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;">dreamincode.tutorials.dic_tut4</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.ListView</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.Menu</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.MenuItem</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.database.sqlite.SQLiteDatabase</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.database.Cursor</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;">java.util.ArrayList</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> dic_tut4 <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">ListView</span> list<span style="color: #339933;">;</span>
	SQLiteDatabase db<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> MENU_ADD <span style="color: #339933;">=</span> <span style="color: #003399;">Menu</span>.<span style="color: #006633;">FIRST</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> MENU_QUIT <span style="color: #339933;">=</span> <span style="color: #003399;">Menu</span>.<span style="color: #006633;">FIRST</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</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;
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">list</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ListView</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">registerForContextMenu</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">list</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">openOrCreateDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dic_tut5&quot;</span>, MODE_PRIVATE, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">db</span>.<span style="color: #006633;">execSQL</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;CREATE TABLE IF NOT EXISTS table_of_dics(value REAL)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">update_list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        setContentView<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">list</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override 
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onCreateOptionsMenu<span style="color: #009900;">&#40;</span><span style="color: #003399;">Menu</span> menu<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	menu.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, MENU_ADD, <span style="color: #cc66cc;">0</span>, <span style="color: #0000ff;">&quot;Add&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setIcon</span><span style="color: #009900;">&#40;</span>android.<span style="color: #006633;">R</span>.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">ic_menu_add</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	menu.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, MENU_QUIT, <span style="color: #cc66cc;">0</span>, <span style="color: #0000ff;">&quot;Quit&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setIcon</span><span style="color: #009900;">&#40;</span>android.<span style="color: #006633;">R</span>.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">ic_menu_close_clear_cancel</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreateOptionsMenu</span><span style="color: #009900;">&#40;</span>menu<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onOptionsItemSelected<span style="color: #009900;">&#40;</span><span style="color: #003399;">MenuItem</span> item<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	<span style="color: #000000; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>item.<span style="color: #006633;">getItemId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    		<span style="color: #000000; font-weight: bold;">case</span> MENU_ADD<span style="color: #339933;">:</span>
	        	db.<span style="color: #006633;">execSQL</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO table_of_dics(value) VALUES(&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	        	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">update_list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    			<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    		<span style="color: #000000; font-weight: bold;">case</span> MENU_QUIT<span style="color: #339933;">:</span>
    			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">db</span>.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">finish</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #009900;">&#125;</span>
&nbsp;
    	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onOptionsItemSelected</span><span style="color: #009900;">&#40;</span>item<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> update_list<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> db_results <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #003399;">Cursor</span> cursor <span style="color: #339933;">=</span> db.<span style="color: #006633;">rawQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT value FROM table_of_dics ORDER BY value&quot;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>cursor.<span style="color: #006633;">moveToNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    	db_results.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>cursor.<span style="color: #006633;">getDouble</span><span style="color: #009900;">&#40;</span>cursor.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #009900;">&#125;</span>
	    cursor.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">list</span>.<span style="color: #006633;">setAdapter</span><span style="color: #009900;">&#40;</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>, db_results<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.gabehabe.com/blog/developing-for-android-part4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Developing for Android, Part III</title>
		<link>http://www.gabehabe.com/blog/developing-for-android-part3/</link>
		<comments>http://www.gabehabe.com/blog/developing-for-android-part3/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 18:40:04 +0000</pubDate>
		<dc:creator>Danny</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[android development]]></category>
		<category><![CDATA[android sdk]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[layout.xml]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.gabehabe.com/blog/developing-for-android-part-iii/</guid>
		<description><![CDATA[Dynamic Layouts in Android No XML in this tutorial! We&#8217;ll be completely designing our application in Java, and creating our layout completely dynamically. The benefits of this are that we can add an undefined amount of widgets at run time. This is a pretty useful skill, but not brilliantly documented. (Everything is ZOMG LAYOUT.XML FTW!!1!) [...]]]></description>
			<content:encoded><![CDATA[<h3>Dynamic Layouts in Android</h3>
<p>No XML in this tutorial! We&#8217;ll be completely designing our application in Java, and creating our layout completely dynamically. The benefits of this are that we can add an undefined amount of widgets at run time. This is a pretty useful skill, but not brilliantly documented. (Everything is ZOMG LAYOUT.XML FTW!!1!)</p>
<p>Okay, so let&#8217;s get started. We won&#8217;t be touching on events in this tutorial, our app won&#8217;t actually <em>do</em> anything, per se&#8230; it&#8217;s merely an example of adding stuff dynamically. You&#8217;ll notice how we set the properties through Java, quite similarly to how we define them in the XML.</p>
<p>To start off, remember we&#8217;re creating a package, as usual.</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;">dreamincode.tutorials.dic_tut3</span><span style="color: #339933;">;</span></pre></div></div>

<p>Next, we&#8217;re going to <code>import</code> the usual suspects&#8230; <code>Activity</code> and <code>Bundle</code>.</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.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></pre></div></div>

<p>The next two are new. One you may recognise from our layouts before&#8230; <code>LinearLayout</code>. We&#8217;re also going to use a <code>ScrollView</code>. The reason we&#8217;ll be using a <code>ScrollView</code> is that we&#8217;re going to add a <em>hell</em> of a lot of widgets, and they wouldn&#8217;t all fit on screen. If we don&#8217;t put our <code>LinearLayout</code> inside a <code>ScrollView</code>, it simply won&#8217;t scroll and the user won&#8217;t be able to reach all the widgets!</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.widget.ScrollView</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.LinearLayout</span><span style="color: #339933;">;</span></pre></div></div>

<p>The last things we need to import are our widgets. We&#8217;ll be using an <code>EditText</code>, a <code>TextView</code>, a <code>Button</code> and a <code>CheckBox</code>. We&#8217;ve already seen the first three in parts 1 and 2 of this tutorial, but the <code>CheckBox</code> is new. (And I hope the name is self-explanatory for what it is!) ;)</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.widget.Button</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.TextView</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.EditText</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.CheckBox</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now it&#8217;s time to get on with our code. We&#8217;re only going to <code>extend Activity</code> this time, remember we won&#8217;t be using events. This bit&#8217;s nothing new, so I&#8217;ll just throw in the opening of the class and the opening of the <code>onCreate()</code> method. (Note, if you&#8217;re not familiar with this, check <a href="http://www.dreamincode.net/forums/index.php?showtopic=130052">part 1</a> and <a href="http://www.dreamincode.net/forums/index.php?showtopic=130264">part 2</a>)</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> dic_tut3 <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></pre></div></div>

<p>Time to get dirty. Dynamic layouts aren&#8217;t always the most elegant of solutions, but they can be fun, and sometimes easier to implement. They&#8217;re also <em>really</em> useful for certain occasions (such as <a href="http://www.gabehabe.com/blog/smartcalc-for-android/">SmartCalc</a>, my app: it uses a dynamic layout to decide how many textboxes are needed at runtime)</p>
<p>Code first, explanation afterwards.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">        ScrollView sv <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ScrollView<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>
        LinearLayout ll <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinearLayout<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>
        ll.<span style="color: #006633;">setOrientation</span><span style="color: #009900;">&#40;</span>LinearLayout.<span style="color: #006633;">VERTICAL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        sv.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>ll<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>LINE 1:</strong> Creates a <code>ScrollView</code> object, which is a part of <strong>this</strong> <code>Activity</code>.<br />
<strong>LINE 2:</strong> Creates a <code>LinearLayout</code> object, which you hopefully remember from the layout.xml we discussed before.<br />
<strong>LINE 3:</strong> Sets the orientation of our <code>LinearLayout</code>. Remember how we used <code>android:orientation="vertical"</code> before? Same principal applies here.<br />
<strong>LINE 4:</strong> Adds the <code>LinearLayout</code> to the <code>ScrollView</code>. Remember, we want to be able to <code>Scroll</code> the <code>LinearLayout</code> <code>View</code>.<br />
<strong>NOTE:</strong> A <code>ScrollView</code> can **only** have one view added to it. If you try to add more than one, your application <strong>WILL</strong> force close. If you need to reset it, use <code>ScrollView.removeAllViews();</code> and re-add whatever you need. (This is why we&#8217;re going to add all our widgets to the LinearLayout as opposed to the ScrollView!)</p>
<p><strong>Adding widgets dynamically</strong><br />
Actually, this same principal applies to all our widgets. But I&#8217;ll use a few, just to demonstrate. :) This one should look relatively familiar, since we played with <code>TextView.setText()</code> in part 2.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">        TextView tv <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>
        tv.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Dynamic layouts ftw!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ll.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>tv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Notice how we use <code>addView()</code> on ll, our LinearLayout, just like we added ll to sv? That&#8217;s all it takes! :)</p>
<p>Now, let&#8217;s do a <code>Button</code> and a <code>EditText</code>. Same principal, just like I said:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">        EditText et <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EditText<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>
        et.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;weeeeeeeeeee~!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ll.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>et<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">Button</span> b <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Button</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        b.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I don't do anything, but I was added dynamically. :)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ll.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p> (Remember, when you run the application, the button won&#8217;t actually do anything: We haven&#8217;t added an <code>OnClickListener</code>! If you want, you may want to revisit the button at the end of this tutorial and try adding an event by yourself. (Use part 2 of this series as a reference if you get stuck)</p>
<p>The next is still the same principal, but I&#8217;ve added it in a loop. 20 pointless <code>CheckBox</code>es, coming up!</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	        CheckBox cb <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CheckBox<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>
	        cb.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I'm dynamic!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	        ll.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>cb<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is really just to pad out our layout, so we can really see how useful that <code>ScrollView</code> is. :)</p>
<p>The very last thing we need to do is use <code>setContentView()</code> &#8212; similar to how we did before. Remember the <code>this.setContentView(R.layout.main);</code>? Pretty much the same, only now we&#8217;re going to use the ScrollView we created earlier instead: </pre>

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

<p>And lastly, close off the method and class!</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, if you run this, you'll get a rather crude, but biiiig layout, complete with all the widgets that we added dynamically.</p>
<p>Complete 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;">dreamincode.tutorials.dic_tut3</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.ScrollView</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.LinearLayout</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.Button</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.TextView</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.EditText</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.CheckBox</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> dic_tut3 <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>
&nbsp;
        ScrollView sv <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ScrollView<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>
        LinearLayout ll <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinearLayout<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>
        ll.<span style="color: #006633;">setOrientation</span><span style="color: #009900;">&#40;</span>LinearLayout.<span style="color: #006633;">VERTICAL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        sv.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>ll<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        TextView tv <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>
        tv.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Dynamic layouts ftw!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ll.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>tv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        EditText et <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EditText<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>
        et.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;weeeeeeeeeee~!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ll.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>et<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">Button</span> b <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Button</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        b.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I don't do anything, but I was added dynamically. :)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ll.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	        CheckBox cb <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CheckBox<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>
	        cb.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I'm dynamic!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	        ll.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>cb<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">setContentView</span><span style="color: #009900;">&#40;</span>sv<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>See if you can add an <code>OnClickListener</code> to the button, just have a play about with it until you feel fairly confident... I won't be explaining too much about the layouts in the coming tutorials, more on actual features such as SQLite. :)</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.gabehabe.com/blog/developing-for-android-part3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Developing for Android, Part II</title>
		<link>http://www.gabehabe.com/blog/developing-for-android-part2/</link>
		<comments>http://www.gabehabe.com/blog/developing-for-android-part2/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 18:00:17 +0000</pubDate>
		<dc:creator>Danny</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[android development]]></category>
		<category><![CDATA[android sdk]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[layout.xml]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.gabehabe.com/blog/?p=91</guid>
		<description><![CDATA[Again, originally written for &#60;/dream.in.code&#62; but I like to add this stuff to my blog as well. :) Basic Layouts and Events Now that we got the basics done, it&#8217;s time to get into developing our first app. It&#8217;s not the most impressive application, but it&#8217;s a great starting point. This will most likely seem [...]]]></description>
			<content:encoded><![CDATA[<p>Again, originally written for <a href="http://dreamincode.net">&lt;/dream.in.code&gt;</a> but I like to add this stuff to my blog as well. :)</p>
<h3>Basic Layouts and Events</h3>
<p>Now that we got the basics done, it&#8217;s time to get into developing our first app. It&#8217;s not the most impressive application, but it&#8217;s a great starting point.</p>
<p>This will most likely seem very familiar if you&#8217;ve learned programming on the console in the past. We&#8217;re going to prompt for a name, and display a customised message. However, we&#8217;ll have a user interface rather than printing on the console. :)</p>
<p>We&#8217;re going to start with the XML. We need a layout which we can program around.</p>
<h3>Step 0: Creating the project</h3>
<p>So we&#8217;re on the same wavelength, let&#8217;s use the same project name, package, etc.</p>
<h3>Step 1: Putting the UI XML together</h3>
<p>I&#8217;ll run through this one element at a time. it&#8217;s all really simple, and you should recognise a lot of this from <a href="http://www.dreamincode.net/forums/showtopic130052.htm">part 1</a>.</p>
<p>To start, remember, it&#8217;s XML:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt; ?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span></pre></div></div>

<p>We&#8217;re going to use a linear layout, which fills the parent and has a vertical layout (again)</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;linearlayout</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:orientation</span>=<span style="color: #ff0000;">&quot;vertical&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/linearlayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Next, we&#8217;re going to the first TextView. Remember, the TextView is like a &#8220;Label&#8221;, from .NET. At this point, all we need to do is set the width, height, and set the text. This time, we&#8217;re just going to use a static string, rather than linking into the <em>./res/values/strings.xml</em> file.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;textview</span> <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;Enter your name:&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>The next thing we need in our <code>LinearLayout</code> is an <code>EditText</code>. This is basically a <code>TextBox</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;edittext</span> <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/txt_name&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>Only one new thing here, we give it an ID. We need to give it an ID so that we can reference it in the actual Java application.<br />
<strong>android:id=&#8221;@+id/txt_name&#8221;</strong> &#8212; I&#8217;ll call it txt_name, I like to prefix my widgets so they&#8217;re easier to remember and reference later on.<br />
Only two things left now: The <code>Button</code>, and the <code>TextView</code>. Nothing new in either of these now.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;button</span> <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/btn_confirm&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;Confirm~&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;textview</span> <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/tv_welcome&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>Note that we don&#8217;t set the <code>android:text</code> attribute in the final TextView? That&#8217;s because we&#8217;re going to give it its text when the user presses the &#8220;Confirm&#8221; button. (btn_confirm)</p>
<p>Lastly, we just need to close off our layout:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">&nbsp;</pre></div></div>

<h3>Step 2: Bringing it all together in Java</h3>
<p>This time we&#8217;re going to write our own Java. We&#8217;re going to use <strong>multiple inheritance</strong> (don&#8217;t be put off, it&#8217;s really very simple) so that we can make our class both an Activity (the main application) and an OnClickListener (to listen for clicks on the button)</p>
<p>First up, remember we&#8217;re working on a package:</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;">dreamincode.tutorials.part_two</span><span style="color: #339933;">;</span></pre></div></div>

<p>Next, we&#8217;re going to import some stuff. The first two are nothing new:</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.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></pre></div></div>

<p>Remember we used both of these in part 1.</p>
<p>This is where it gets interesting, though. We&#8217;re going to <code>import</code> three <code>widget</code>s.</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.widget.TextView</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.EditText</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.Button</span><span style="color: #339933;">;</span></pre></div></div>

<p> I won&#8217;t explain them&#8230; hopefully your memory is good enough to remember that we used these names in the XML layout earlier. :)</p>
<p>The next two will take a little explaining.</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.view.View</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View.OnClickListener</span><span style="color: #339933;">;</span></pre></div></div>

<p>- We <code>import</code> the <code>View</code> because that&#8217;s the parameter which our onClick event requires.<br />
- We <code>import</code> the <code>OnClickListener</code> because, well&#8230; we want our Activity to be an OnClickListener, too.</p>
<p>This is where the multiple inheritance comes in. We&#8217;re calling our class <code>dic_tut2</code>, and it&#8217;s going to <code>extend Activity</code>, just like we did in part 1. It&#8217;s also going to <code>implement OnClickListener</code> because we&#8217;re going to be listening for clicks on the button.</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> dic_tut2 <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #000000; font-weight: bold;">implements</span> OnClickListener <span style="color: #009900;">&#123;</span></pre></div></div>

<p>Now it&#8217;s time for the <code>onCreate</code> method. Just like we did in part 1, only with a few extra lines (explained afterwards!)</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;">/** 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;
        <span style="color: #003399;">Button</span> b <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Button</span><span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">btn_confirm</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        b.<span style="color: #006633;">setOnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Specifically, the last two lines are the new ones. We&#8217;re going to create a <code>Button</code> from <code>R.id.btn_confirm</code> (remember, that&#8217;s the ID we used in the XML!) Then we&#8217;re simply going to set <code>this</code> as the onClickListener for said button. Simple stuff, eh? :)<br />
<strong>NOTE:</strong> We need to cast the return value of <code>findViewById()</code> to a <code>Button</code>, by default it will return a <code>View</code>.</p>
<p>The very last thing we need to do is set our <code>onClick</code> method: the one which is called when the user clicks the button (after we set <code>this</code> as the <code>OnClickListener</code> for said button!)</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	TextView tv <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">tv_welcome</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	EditText et <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>EditText<span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">txt_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #003399;">String</span> text <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello, &quot;</span> <span style="color: #339933;">+</span> et.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;.<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    	text <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;Welcome to android development. :)&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    	tv.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that we use <code>findViewById()</code> again to get the <code>TextView</code> for which we&#8217;re going to set the text, and also the <code>EditText</code> which contains the user&#8217;s name. Then, all we do is create our string (I hope this is nothing new!) and simply <code>setText()</code> on our <code>TextView</code> which displays the final message.</p>
<p>Don&#8217;t forget to close off your class!</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#125;</span></pre></div></div>

<p>Finally, here&#8217;s the complete code. dic_tut2.java:</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;">dreamincode.tutorials.part_two</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>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.EditText</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.Button</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View.OnClickListener</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> dic_tut2 <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #000000; font-weight: bold;">implements</span> OnClickListener <span style="color: #009900;">&#123;</span>
    <span style="color: #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;
        <span style="color: #003399;">Button</span> b <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Button</span><span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">btn_confirm</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        b.<span style="color: #006633;">setOnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	TextView tv <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">tv_welcome</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	EditText et <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>EditText<span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">txt_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #003399;">String</span> text <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello, &quot;</span> <span style="color: #339933;">+</span> et.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;.<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    	text <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;Welcome to android development. :)&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    	tv.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>text<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>And the layout, main.xml:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt; ?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;linearlayout</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:orientation</span>=<span style="color: #ff0000;">&quot;vertical&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;textview</span> <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;Enter your name:&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;edittext</span> <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/txt_name&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;button</span> <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/btn_confirm&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;Confirm~&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;textview</span> <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/tv_welcome&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/linearlayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Enjoy! :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gabehabe.com/blog/developing-for-android-part2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Developing for Android, Part I</title>
		<link>http://www.gabehabe.com/blog/developing-for-android-part1/</link>
		<comments>http://www.gabehabe.com/blog/developing-for-android-part1/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 18:21:55 +0000</pubDate>
		<dc:creator>Danny</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[android development]]></category>
		<category><![CDATA[android sdk]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[layout.xml]]></category>
		<category><![CDATA[strings.xml]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.gabehabe.com/blog/?p=84</guid>
		<description><![CDATA[I originally wrote this tutorial for &#60;/dream.in.code&#62;, but I like to get my tutorials around a bit. Welcome to Android. We hope you enjoy your stay. So, first off, a bit of an introduction. This is the first part of (hopefully) many tutorials related to the android platform. In this tutorial, we&#8217;re only going to [...]]]></description>
			<content:encoded><![CDATA[<p>I originally wrote this tutorial for <a href="http://www.dreamincode.net/forums/showtopic130052.htm" target="_blank">&lt;/dream.in.code&gt;</a>, but I like to get my tutorials around a bit.</p>
<h2>Welcome to Android.</h2>
<p> We hope you enjoy your stay.</p>
<p>So, first off, a bit of an introduction. This is the first part of (hopefully) many tutorials related to the android platform. In this tutorial, we&#8217;re only going to cover the very basics: how to design your application, and how to utilise the XML layouts to design your user interface.</p>
<p>Since this series of tutorials is more directed at the code, I&#8217;m not going to cover setup &#038; installation. Instead, <a href="http://developer.android.com/sdk/1.6_r1/index.html">here&#8217;s a link</a> to get yourself set up with the SDK and an emulator.</p>
<p>Personally, I&#8217;ve never really been a huge fan of Java. And when I first tried Android, I wasn&#8217;t too keen. But it&#8217;s come a long way since it was <a href="http://mobile.slashdot.org/article.pl?sid=07/11/12/1626245">first released</a>. Once you get into the android SDK, it begins to get really interesting. Plus, the idea of earning a little bit of cash on the side for designing a little app for your phone has quite a nice appeal. :)</p>
<h2>The market</h2>
<p>The market is awesome. It&#8217;s extremely easy to release and update your applications, and I&#8217;ll be writing a tutorial all about publishing your apps, how to sign them, how to create a jar keystore, all the fun stuff.</p>
<h3>Get started already!!1!one!</h3>
<p>Okay. Enough intro, let&#8217;s get to some development! :)</p>
<p>&#8230;sorry. No programming yet. We&#8217;re going to be reading the project template, and I&#8217;ll be explaining it line for line. It&#8217;s best to take this stuff slow at first, because it&#8217;s so different to a lot of &#8220;regular&#8221; projects. But at least there&#8217;s code! :)</p>
<p>Create a project, you can call it anything, but I&#8217;m going to be following this structure along the series of tutorials:</p>
<h3>layout.xml</h3>
<p>The layout.xml is&#8230; sorta important. It&#8217;s a great way to design your user interfaces, but it&#8217;s not always necessary: you can create your interfaces through pure Java, which is personally my preferred method&#8230; but it&#8217;s still good to know how to use the xml layouts. They help to keep the code for your <strong>activity</strong> more organised. (Coming from a C++/wxWidgets background, I&#8217;m in the habit of creating my programs through pure code, you may prefer the XML process)</p>
<p>Right. Enough rambling. I&#8217;ve analysed the <em>./res/layout/main.xml</em>, and broken it up, line by line.</p>
<p>yep: we just use standard XML to declare the layout:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt; ?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span></pre></div></div>

<p>We use a LinearLayout to define our layout as being, well&#8230; linear:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;linearlayout</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></pre></div></div>

<p>Next up, we have some properties:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    android:orientation=&quot;vertical&quot;
    android:layout_width=&quot;fill_parent&quot;
    android:layout_height=&quot;fill_parent&quot;</pre></div></div>

<p>Note the three properties:<br />
<strong>android:orientation</strong> &#8212; which direction the layout is filled. Contrary to what you may think, this isn&#8217;t based on the angle of the phone. Using &#8220;vertical&#8221; simply means &#8220;add widget number 2 below widget number 1, add widget number 3 below widget number 2, &#8230; add widget number n below widget number n-1&#8243;. :)<br />
<strong>android:layout_width</strong> &#8212; the width of the object. We&#8217;re going to use &#8220;fill_parent&#8221; to fill the width of the screen<br />
<strong>android:layout_height</strong> &#8212; the height of the object. We&#8217;re going to use &#8220;fill_parent&#8221; again, to fill the height of the screen.<br />
Lastly, we&#8217;re going to close the <code>LinearLayout</code> item. Note we want more items inside the <code>LinearLayout</code>, so we&#8217;re just going to use <code>></code> instead of <code>/></code></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    &gt;</pre></div></div>

<p>Next, we&#8217;re going to define a <code>TextView</code>. The <code>TextView</code> is like a Label in .NET, or a wxStaticText in wxWidgets. It basically puts a static piece of text on the screen.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;textview</span> <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;@string/hello&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>You&#8217;ll notice it&#8217;s pretty similar to how we defined the <code>LinearLayout</code>. Differences:<br />
<strong>android:layout_height=&#8221;wrap_content&#8221;</strong> &#8212; the TextView will be resized according to the text it displays. The longer the string, the taller the View.<br />
<strong>android:text=&#8221;@string/hello&#8221;</strong> &#8212; the string to display. <code>@string</code> refers to the <em>./res/values/strings.xml</em> file. (More on this later)<br />
<strong>/></strong> &#8212; this time, we&#8217;re not putting any other items inside it, so we can close it off XML stylee. We could just as easily use</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;textview</span> ... <span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/textview<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p> but it seems a little pointless here. :)</p>
<p>Since that&#8217;s all we want in the <code>LinearLayout</code>, we&#8217;re going to close our layout off:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">&nbsp;</pre></div></div>

</linearlayout></pre>
<h3>strings.xml</h3>
<p>This one's much simpler. Put simply, this file contains strings which are built in to the application at compile time. Note the <strong>hello</strong> string, which we referenced in our <code>TextView</code> in <code>./res/layout/main.xml</code></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt; ?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hello&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Hello World, dic_tut1!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;app_name&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>dic_tut1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3>The Java!</h3>
<p>Lastly, we're going to put our layout together in the code. I've commented this bit up, so it's easy for you to understand.</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;">dreamincode.tutorials.part_one</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: #666666; font-style: italic;">// Activity: the &quot;main&quot; part of the application</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: #666666; font-style: italic;">// Bundle: stores a &quot;saved state&quot; for your app to remember how it was when moved to the background</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> dic_tut1 <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// inheriting the Activity class</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: #666666; font-style: italic;">// consider this the &quot;entry point&quot; (main) of your application</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> <span style="color: #666666; font-style: italic;">// call the super to restore the state (does the work for you)</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: #666666; font-style: italic;">// set the main content view to ./res/layout/main.xml</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>That's all for now!</h3>
<p>There's obviously more to it than this, things such as the <strong>AndroidManifest.xml</strong> file, but I'm not going to go into that now. We'll dive into that as and when we need it, it's really rather trivial and honestly nothing to worry about. :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gabehabe.com/blog/developing-for-android-part1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

