<?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; Programming</title>
	<atom:link href="http://www.gabehabe.com/blog/topics/programming/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>wxWidgets: Loading Symbols from a DLL (and using ::Connect)</title>
		<link>http://www.gabehabe.com/blog/wxwidgets-loading-symbols-from-a-dll-and-using-connect/</link>
		<comments>http://www.gabehabe.com/blog/wxwidgets-loading-symbols-from-a-dll-and-using-connect/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 20:20:08 +0000</pubDate>
		<dc:creator>Danny</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[wxWidgets]]></category>
		<category><![CDATA[c++]]></category>

		<guid isPermaLink="false">http://www.gabehabe.com/blog/wxwidgets-loading-symbols-from-a-dll-and-using-connect/</guid>
		<description><![CDATA[[Apologies if the entry is a little unclear, I just threw this together in about 10 minutes, I wanna get it out there... it's fucking hard to find any sort of information when it comes to this at the moment] Quick entry, gonna turn this into a tutorial soon. The code is a bit of [...]]]></description>
			<content:encoded><![CDATA[<p>[Apologies if the entry is a little unclear, I just threw this together in about 10 minutes, I wanna get it out there... it's fucking hard to find any sort of information when it comes to this at the moment]</p>
<p>Quick entry, gonna turn this into a tutorial soon. The code is a bit of a mess, since I was just throwing stuff together while I was figuring this out. I&#8217;ll tidy it up when it&#8217;s tutorial tiems.</p>
<p>Basically, I recently developed a [url=http://snip.gd/plugins.php]plugin system[/url] for my [url=http://snip.gd/]snippet manager[/url]. The way it works is simple: It loads symbols from a DLL into the application, which can be called from a dynamically created menu. First off, the main code [this is the messy bit for the time being]</p>
<p>This code opens a subdirectory within the working directory called &#8220;plugins&#8221;, and grabs all the DLLs from them. If the DLL contains a symbol called SnippetManagerPluginMain (the only required symbol for the plugins, I kept them simple) it will add it to the menu.</p>
<p>Additionally, it also checks for two optional optional symbols, which are pretty cool. They simply return strings, which assign the name and the keyboard shortcut for the plugin. If neither is set, it has no keyboard shortcut, and the name defaults to the name of the DLL file.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    <span style="color: #666666;">/// &lt;plugin SYSTEM&gt; &lt;!-- ALPHA! --&gt;</span>
    <span style="color: #339900;">#if defined(__WXMSW__) // windows plugins - load ./plugins/*.dll</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>wxDir<span style="color: #008080;">::</span><span style="color: #007788;">Exists</span><span style="color: #008000;">&#40;</span>wxT<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;./plugins/&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        wxDir plugin_dir<span style="color: #008000;">&#40;</span>wxT<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;./plugins/&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>plugin_dir.<span style="color: #007788;">HasFiles</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            wxArrayString files<span style="color: #008080;">;</span>
            wxDir<span style="color: #008080;">::</span><span style="color: #007788;">GetAllFiles</span><span style="color: #008000;">&#40;</span>wxT<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;./plugins/&quot;</span><span style="color: #008000;">&#41;</span>, <span style="color: #000040;">&amp;</span>files<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            wxMenu<span style="color: #000040;">*</span> plugin_menu <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> wxMenu<span style="color: #008000;">&#40;</span>wxT<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            wxDynamicLibrary<span style="color: #000040;">*</span> lib<span style="color: #008080;">;</span>
            wxString label<span style="color: #008080;">;</span>
            <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> ks<span style="color: #008080;">;</span>
            <span style="color: #0000ff;">bool</span> showmenu <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> files.<span style="color: #007788;">GetCount</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                lib <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> wxDynamicLibrary<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>files.<span style="color: #007788;">Item</span><span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">Find</span><span style="color: #008000;">&#40;</span>wxT<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;.dll&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                    lib<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Load<span style="color: #008000;">&#40;</span>files<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>lib<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>HasSymbol<span style="color: #008000;">&#40;</span>L<span style="color: #FF0000;">&quot;SnippetManagerPluginMain&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                        showmenu <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
                        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>lib<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>HasSymbol<span style="color: #008000;">&#40;</span>L<span style="color: #FF0000;">&quot;SnippetManagerPluginName&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                            label <span style="color: #000080;">=</span> wxString<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>SnippetManagerPluginName<span style="color: #008000;">&#41;</span>lib<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetSymbol<span style="color: #008000;">&#40;</span>L<span style="color: #FF0000;">&quot;SnippetManagerPluginName&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, wxConvUTF8<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                        <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
                            label <span style="color: #000080;">=</span> files<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">AfterFirst</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">'\<span style="color: #000099; font-weight: bold;">\'</span>).AfterFirst('</span>\\<span style="color: #FF0000;">').BeforeLast('</span>.<span style="color: #FF0000;">');
                        }
                        this-&gt;plugins.push_back((SnippetManagerPlugin)lib-&gt;GetSymbol(L&quot;SnippetManagerPluginMain&quot;));
&nbsp;
                        if(lib-&gt;HasSymbol(L&quot;SnippetManagerKeyboardShortcut&quot;)) {
                            ks = ((SnippetManagerKeyboardShortcut)lib-&gt;GetSymbol(L&quot;SnippetManagerKeyboardShortcut&quot;))();
                            label += L&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot; + wxString(ks, wxConvUTF8);
&nbsp;
                        }
                        plugin_menu-&gt;Append(wxID_PLUGIN + i, label);
                        Connect(wxID_PLUGIN + i, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Snippet_ManagerFrame::handle_plugin));
                    }
                }
            }
            if(showmenu) {
                mbar-&gt;Append(plugin_menu, wxT(&quot;&amp;Plugins&quot;));
            }
        }
    }
    #endif
    /// &lt;/plugin&gt;[</span></pre></div></div>

<p>In order to bind the events to the menu, the ::Connect method is called. This is an alternative to using EVENT_TABLE, and allows events to be allocated dynamically at runtime. wxID_PLUGIN is a custom variable, and basically, any of the menu items within the menu will call the main frame&#8217;s [il]handle_plugin[/il] method. The important part, however, is the fact that each one has a different ID, even though they use the same method.</p>
<p>The method itself then uses the ID of the event calling it to lookup the DLL&#8217;s symbol, which were added in <code>this->plugins</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> Snippet_ManagerFrame<span style="color: #008080;">::</span><span style="color: #007788;">handle_plugin</span><span style="color: #008000;">&#40;</span>wxCommandEvent <span style="color: #000040;">&amp;</span>e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>plugins<span style="color: #008000;">&#91;</span>e.<span style="color: #007788;">GetId</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> wxID_PLUGIN<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#40;</span>this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>ui<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>uid.<span style="color: #007788;">mb_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, fill_p_snip<span style="color: #008000;">&#40;</span>this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>ui<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>get_active_stc<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>snip, this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>ui<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>get_active_stc<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetText<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Of course, some of the code here doesn&#8217;t really mean jack all, since there are custom methods and stuff used in it. But the base of it is clear. I&#8217;ll try to write another (small) application and a tutorial covering the steps over the weekend.</p>
<p>Until then, have fun! :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gabehabe.com/blog/wxwidgets-loading-symbols-from-a-dll-and-using-connect/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>wxWidgets: Threading, and using the Clipboard</title>
		<link>http://www.gabehabe.com/blog/wxwidgets-threading-and-using-the-clipboard/</link>
		<comments>http://www.gabehabe.com/blog/wxwidgets-threading-and-using-the-clipboard/#comments</comments>
		<pubDate>Sat, 31 Oct 2009 17:11:12 +0000</pubDate>
		<dc:creator>Danny</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[wxWidgets]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[clipboard]]></category>
		<category><![CDATA[gui design]]></category>
		<category><![CDATA[threading]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.gabehabe.com/blog/wxwidgets-threading-and-using-the-clipboard/</guid>
		<description><![CDATA[wxWidgets: Threading, and using the Clipboard Two for one in this tutorial. We're going to create a thread using wxWidgets, and make it monitor the clipboard for text. Then, if we detect a change, we're going to add the new contents to a list. It's a relatively simple process, but unfortunately wxWidgets isn't the most [...]]]></description>
			<content:encoded><![CDATA[<h3>wxWidgets: Threading, and using the Clipboard</h3>

Two for one in this tutorial. We're going to create a thread using wxWidgets, and make it monitor the clipboard for text. Then, if we detect a change, we're going to add the new contents to a list.

It's a relatively simple process, but unfortunately wxWidgets isn't the most documented GUI toolkit out there.

So, let's get started. I'm gonna pile all the code into a single file instead of breaking my classes up into seperate files, just for ease of navigation in this tutorial. The code is relatively short anyway, with only 62 lines.

First off, as with any other program, we're going to want to get our includes done. We'll be needing three: The standard wx header, the clipboard header, and the thread header.


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;wx/wx.h&gt; // standard wx header</span>
<span style="color: #339900;">#include &lt;wx/clipbrd.h&gt; // clipboard - we'll monitor the clipboard for text</span>
<span style="color: #339900;">#include &lt;wx/thread.h&gt; // include threads!</span></pre></div></div>



The next thing we need to do is declare our <code>ClipLogger</code> class - this will inherit wxThread, and have two variables: One to hold the most recent text that we got from the clipboard, and one which is a pointer to a <code>wxListBox</code> object -- the object on the main window which we'll be updating.


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> ClipLogger <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> wxThread <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
        ClipLogger<span style="color: #008000;">&#40;</span>wxListBox<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
        <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> Entry<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// the entry point to the thread</span>
&nbsp;
        wxString LatestText<span style="color: #008080;">;</span> <span style="color: #666666;">// store the last text so we can check for changes</span>
        wxListBox<span style="color: #000040;">*</span> list<span style="color: #008080;">;</span> <span style="color: #666666;">// the list to update on a text change</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>



The constructor is <em>very</em> simple. All it does is take a wxListBox*, and assign it to a variable stored by the class - this is the list that we want to update on clipboard text changes.


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">ClipLogger<span style="color: #008080;">::</span><span style="color: #007788;">ClipLogger</span><span style="color: #008000;">&#40;</span>wxListBox<span style="color: #000040;">*</span> l<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>list <span style="color: #000080;">=</span> l<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>



Next up is the main part of this tutorial. When we create a thread, we need to override the <code>Entry()</code> method of the class, like so:


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> ClipLogger<span style="color: #008080;">::</span><span style="color: #007788;">Entry</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span></pre></div></div>



To save time and memory, rather than creating a variable every time we loop, we'll create it before and simply overwrite it inside the loop.


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">wxTextDataObject temp<span style="color: #008080;">;</span></pre></div></div>



wxTextDataObject is the type of object that the clipboard will store.
Next, since our thread is a constant "monitor" for the clipboard, we want it to loop.


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span></pre></div></div>



Then, we need to think about how we can help our application not be CPU-hungry. The simple solution is to make the thread sleep each time it loops. We can do this with <code>wxSleep(int time)</code>, where time is the length of time to sleep in <strong>seconds</strong>.


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">wxSleep<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>



The rest of the loop is the clipboard. It's very simple, so rather than break it up line-by-line, I've added comments along the way. We basically need to do the following:
 - Open the clipboard
 - If the clipboard is text, get it into our <code>temp</code> variable
 - Update the list and remember this is the most recent (so as not to constantly add the same data to the list)
 - Close the clipboard


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>wxTheClipboard<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Open<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// try to open the clipboard</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>wxTheClipboard<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>IsSupported<span style="color: #008000;">&#40;</span>wxDF_TEXT<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// if the clipboard contains text</span>
                wxTheClipboard<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetData<span style="color: #008000;">&#40;</span>temp<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// get the data from the clipboard</span>
                <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>LatestText <span style="color: #000040;">!</span><span style="color: #000080;">=</span> temp.<span style="color: #007788;">GetText</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// if it's changed, we want to update</span>
                    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>temp.<span style="color: #007788;">GetText</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> wxT<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// if it's not an empty string</span>
                        this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>LatestText <span style="color: #000080;">=</span> temp.<span style="color: #007788;">GetText</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  <span style="color: #666666;">// update the &quot;LatestText&quot;</span>
                        this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>list<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Append<span style="color: #008000;">&#40;</span>temp.<span style="color: #007788;">GetText</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// append to the list</span>
                    <span style="color: #008000;">&#125;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        wxTheClipboard<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Close<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// close the clipboard</span></pre></div></div>



The last thing left to do in the thread is simply close it off, and close off the loop.


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



Simple, huh? :)

And that's our thread defined. Now all we need to do is create the app itself, which is a simple process. I hope that you already know how to do it, so we can blitz through the majority of it.

Create the app:


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> threaded_app <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> wxApp <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
        <span style="color: #0000ff;">bool</span> OnInit<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>



The beginning of the <code>OnInit()</code> should be nothing new at this point either.


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">bool</span> threaded_app<span style="color: #008080;">::</span><span style="color: #007788;">OnInit</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// quickly create a wxFrame to display a window</span>
    wxFrame<span style="color: #000040;">*</span> f <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> wxFrame<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span>, wxID_ANY, wxT<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Threaded App!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// add a list to the frame we created</span>
    wxListBox<span style="color: #000040;">*</span> list <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> wxListBox<span style="color: #008000;">&#40;</span>f, wxID_ANY<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// display the frame</span>
    f<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Show<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>



The last part of <code>OnInit()</code> that we need to do is actually create an instance of our thread and run it, like so:


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    <span style="color: #666666;">// pass the list to the clipboard monitor so it knows what to update</span>
    ClipLogger<span style="color: #000040;">*</span> cl <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> ClipLogger<span style="color: #008000;">&#40;</span>list<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// construct our thread</span>
    cl<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Create<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// we have to create a thread before we can run it</span>
    cl<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Run<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// run our thread</span></pre></div></div>



And we can simply finish off the <code>OnInit()</code> and IMPLEMENT_APP:


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
IMPLEMENT_APP<span style="color: #008000;">&#40;</span>threaded_app<span style="color: #008000;">&#41;</span></pre></div></div>




And that's all there is to threading and using the clipboard in wxWidgets!

Here's the complete code:


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;wx/wx.h&gt; // standard wx header</span>
<span style="color: #339900;">#include &lt;wx/clipbrd.h&gt; // clipboard - we'll monitor the clipboard for text</span>
<span style="color: #339900;">#include &lt;wx/thread.h&gt; // include threads!</span>
&nbsp;
<span style="color: #0000ff;">class</span> ClipLogger <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> wxThread <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
        ClipLogger<span style="color: #008000;">&#40;</span>wxListBox<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
        <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> Entry<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// the entry point to the thread</span>
&nbsp;
        wxString LatestText<span style="color: #008080;">;</span> <span style="color: #666666;">// store the last text so we can check for changes</span>
        wxListBox<span style="color: #000040;">*</span> list<span style="color: #008080;">;</span> <span style="color: #666666;">// the list to update on a text change</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
ClipLogger<span style="color: #008080;">::</span><span style="color: #007788;">ClipLogger</span><span style="color: #008000;">&#40;</span>wxListBox<span style="color: #000040;">*</span> l<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>list <span style="color: #000080;">=</span> l<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> ClipLogger<span style="color: #008080;">::</span><span style="color: #007788;">Entry</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    wxTextDataObject temp<span style="color: #008080;">;</span> <span style="color: #666666;">// create a &quot;wxTextDataObject&quot; to get the info from the clipboard</span>
    <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// our thread will loop</span>
        wxSleep<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// sleep for 1 second, make the thread less cpu-hungry</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>wxTheClipboard<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Open<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// try to open the clipboard</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>wxTheClipboard<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>IsSupported<span style="color: #008000;">&#40;</span>wxDF_TEXT<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// if the clipboard contains text</span>
                wxTheClipboard<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetData<span style="color: #008000;">&#40;</span>temp<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// get the data from the clipboard</span>
                <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>LatestText <span style="color: #000040;">!</span><span style="color: #000080;">=</span> temp.<span style="color: #007788;">GetText</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// if it's changed, we want to update</span>
                    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>temp.<span style="color: #007788;">GetText</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> wxT<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// if it's not an empty string</span>
                        this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>LatestText <span style="color: #000080;">=</span> temp.<span style="color: #007788;">GetText</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  <span style="color: #666666;">// update the &quot;LatestText&quot;</span>
                        this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>list<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Append<span style="color: #008000;">&#40;</span>temp.<span style="color: #007788;">GetText</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// append to the list</span>
                    <span style="color: #008000;">&#125;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        wxTheClipboard<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Close<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// close the clipboard</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">class</span> threaded_app <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> wxApp <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
        <span style="color: #0000ff;">bool</span> OnInit<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">bool</span> threaded_app<span style="color: #008080;">::</span><span style="color: #007788;">OnInit</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// quickly create a wxFrame to display a window</span>
    wxFrame<span style="color: #000040;">*</span> f <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> wxFrame<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span>, wxID_ANY, wxT<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Threaded App!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// add a list to the frame we created</span>
    wxListBox<span style="color: #000040;">*</span> list <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> wxListBox<span style="color: #008000;">&#40;</span>f, wxID_ANY<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// display the frame</span>
    f<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Show<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// pass the list to the clipboard monitor so it knows what to update</span>
    ClipLogger<span style="color: #000040;">*</span> cl <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> ClipLogger<span style="color: #008000;">&#40;</span>list<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// construct our thread</span>
    cl<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Create<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// we have to create a thread before we can run it</span>
    cl<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Run<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// run our thread</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
IMPLEMENT_APP<span style="color: #008000;">&#40;</span>threaded_app<span style="color: #008000;">&#41;</span></pre></div></div>



Happy coding! :)

]]></content:encoded>
			<wfw:commentRss>http://www.gabehabe.com/blog/wxwidgets-threading-and-using-the-clipboard/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<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>
		<item>
		<title>My First Android App: Brainfuck</title>
		<link>http://www.gabehabe.com/blog/my-first-android-app-brainfuck/</link>
		<comments>http://www.gabehabe.com/blog/my-first-android-app-brainfuck/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 19:34:05 +0000</pubDate>
		<dc:creator>Danny</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[android development]]></category>
		<category><![CDATA[brainfuck]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.gabehabe.com/blog/my-first-android-app-brainfuck/</guid>
		<description><![CDATA[My first app! ** EDIT: Just realised input doesn&#8217;t work: It brings up the input dialog, but I forgot to save it anywhere! Guess I was a little too excited to get publishing ;). I&#8217;ll try to find time to fix it tomorrow! Apologies! ** Download: Okay, so it&#8217;s not gonna get a very good [...]]]></description>
			<content:encoded><![CDATA[<p>My first app!</p>
<p>** EDIT: Just realised input doesn&#8217;t work: It brings up the input dialog, but I forgot to save it anywhere! Guess I was a little too excited to get publishing ;). I&#8217;ll try to find time to fix it tomorrow! Apologies! **</p>
<p>Download:<br />
<a href="market://search/?q=pname:gabehabe.brainfuck"><img src="http://chart.apis.google.com/chart?cht=qr&#038;chs=100x100&#038;chl=market://search?q=pname:gabehabe.brainfuck" alt="Download on android!" /></a></p>
<p>Okay, so it&#8217;s not gonna get a very good rating&#8230; it&#8217;s not very attractive, and I really don&#8217;t think many people are going to understand <a href="http://en.wikipedia.org/wiki/Brainfuck" target="_blank">Brainfuck</a>.</p>
<p>That said, it was a fun little app to write&#8230; and I must admit, I really didn&#8217;t give the android SDK a fair chance. (Mind, it&#8217;s changed a <strong>hell of a lot</strong> since I first tried it back when android was first released to the general public!)</p>
<p>Code for the app, broken into two parts: (the activity and the layout)</p>
<p>First off, the main code for the Java activity:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">gabehabe.brainfuck</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.TabActivity</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.TabHost</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.TextView</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.text.InputFilter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.AlertDialog</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.DialogInterface</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.TabHost.OnTabChangeListener</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> Brainfuck <span style="color: #000000; font-weight: bold;">extends</span> TabActivity <span style="color: #000000; font-weight: bold;">implements</span> OnTabChangeListener <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">String</span> input<span style="color: #339933;">;</span>
	TabHost tabs<span style="color: #339933;">;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">tabs</span> <span style="color: #339933;">=</span> getTabHost<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	    tabs.<span style="color: #006633;">addTab</span><span style="color: #009900;">&#40;</span>tabs.<span style="color: #006633;">newTabSpec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tab_code&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setIndicator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Code&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setContent</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">code</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    tabs.<span style="color: #006633;">addTab</span><span style="color: #009900;">&#40;</span>tabs.<span style="color: #006633;">newTabSpec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tab_run&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setIndicator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Run&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setContent</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">interpreted</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    tabs.<span style="color: #006633;">addTab</span><span style="color: #009900;">&#40;</span>tabs.<span style="color: #006633;">newTabSpec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tab_help&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setIndicator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Help&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setContent</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">help</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    tabs.<span style="color: #006633;">setOnTabChangedListener</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>
&nbsp;
&nbsp;
	    tabs.<span style="color: #006633;">setCurrentTab</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</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> onTabChanged<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> label<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>label <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;tab_run&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">final</span> Brainfuck bf <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">final</span> TextView interpreted <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;">interpreted</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			interpreted.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</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;">final</span> EditText edt <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;">code</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">String</span> t <span style="color: #339933;">=</span> edt.<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: #000066; font-weight: bold;">int</span> bytes_pos <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">int</span> loop_pos <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">char</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> bytes <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">300</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">// initialise and make sure everything's 0</span>
			<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;">300</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				bytes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<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> t.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>t.<span style="color: #006633;">charAt</span><span style="color: #009900;">&#40;</span>i<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> <span style="color: #0000ff;">'+'</span><span style="color: #339933;">:</span>
						<span style="color: #339933;">++</span>bytes<span style="color: #009900;">&#91;</span>bytes_pos<span style="color: #009900;">&#93;</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> <span style="color: #0000ff;">'-'</span><span style="color: #339933;">:</span>
						<span style="color: #339933;">--</span>bytes<span style="color: #009900;">&#91;</span>bytes_pos<span style="color: #009900;">&#93;</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> <span style="color: #0000ff;">'&gt;'</span><span style="color: #339933;">:</span>
						<span style="color: #339933;">++</span>bytes_pos<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> <span style="color: #0000ff;">'&lt; '</span><span style="color: #339933;">:</span>
						<span style="color: #339933;">--</span>bytes_pos<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> <span style="color: #0000ff;">'.'</span><span style="color: #339933;">:</span>
						<span style="color: #003399;">StringBuffer</span> b <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						b.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>interpreted.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>bytes<span style="color: #009900;">&#91;</span>bytes_pos<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						interpreted.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>b.<span style="color: #006633;">toString</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: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
					<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">','</span><span style="color: #339933;">:</span>
					    AlertDialog.<span style="color: #006633;">Builder</span> alert <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AlertDialog.<span style="color: #006633;">Builder</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					    alert.<span style="color: #006633;">setTitle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Enter Character&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					    alert.<span style="color: #006633;">setMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Program is requesting input!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					    <span style="color: #666666; font-style: italic;">// Set an EditText view to get user input </span>
					    <span style="color: #000000; font-weight: bold;">final</span> EditText input <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>
					    InputFilter<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> FilterArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> InputFilter<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
					    FilterArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> InputFilter.<span style="color: #006633;">LengthFilter</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					    input.<span style="color: #006633;">setFilters</span><span style="color: #009900;">&#40;</span>FilterArray<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					    alert.<span style="color: #006633;">setView</span><span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					    alert.<span style="color: #006633;">setPositiveButton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;OK&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> DialogInterface.<span style="color: #006633;">OnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span>DialogInterface dialog, <span style="color: #000066; font-weight: bold;">int</span> whichButton<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					    	bf.<span style="color: #006633;">input</span> <span style="color: #339933;">=</span> input.<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: #009900;">&#125;</span>
					    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					    alert.<span style="color: #006633;">setNegativeButton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cancel&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> DialogInterface.<span style="color: #006633;">OnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span>DialogInterface dialog, <span style="color: #000066; font-weight: bold;">int</span> whichButton<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					    	 bf.<span style="color: #006633;">input</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
					      <span style="color: #009900;">&#125;</span>
					    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					    alert.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					    <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
					<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">'['</span><span style="color: #339933;">:</span>
						loop_pos <span style="color: #339933;">=</span> i<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> <span style="color: #0000ff;">']'</span><span style="color: #339933;">:</span>
						<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>loop_pos <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
							<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>bytes<span style="color: #009900;">&#91;</span>bytes_pos<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// stupid java... ¬_¬</span>
								bytes<span style="color: #009900;">&#91;</span>bytes_pos<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
							<span style="color: #009900;">&#125;</span>
							<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>bytes<span style="color: #009900;">&#91;</span>bytes_pos<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
								i <span style="color: #339933;">=</span> loop_pos<span style="color: #339933;">;</span>
							<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
								loop_pos <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
							<span style="color: #009900;">&#125;</span>
						<span style="color: #009900;">&#125;</span>
						<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And now the code for the actual layout (XML format)
</pre>

<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;tabhost</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:id</span>=<span style="color: #ff0000;">&quot;@android:id/tabhost&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;linearlayout</span> <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 style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tabwidget</span> <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@android:id/tabs&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;framelayout</span> <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@android:id/tabcontent&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
            /* tab 1 -- code entry */
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;edittext</span> <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/code&quot;</span> </span>
<span style="color: #009900;">              <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span>
<span style="color: #009900;">              <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span>
<span style="color: #009900;">              <span style="color: #000066;">android:background</span>=<span style="color: #ff0000;">&quot;@android:drawable/editbox_background&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
            /* tab 2 -- run */
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scrollview</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:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;textview</span> <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/interpreted&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;/scrollview<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            /* tab 3 -- help */
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scrollview</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:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;textview</span> <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/help&quot;</span></span>
<span style="color: #009900;">      	          <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">             	  <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span>
<span style="color: #009900;">                  <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;@string/help&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;/scrollview<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/framelayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/linearlayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tabhost<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>I'll no doubt be adding stuff, a menu for snippets, perhaps saving files and stuff... all good fun. Watch this space! :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gabehabe.com/blog/my-first-android-app-brainfuck/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>wxWidgets: Keyboard Shortcuts without a Menu</title>
		<link>http://www.gabehabe.com/blog/wxwidgets-keyboard-shortcuts-without-a-menu/</link>
		<comments>http://www.gabehabe.com/blog/wxwidgets-keyboard-shortcuts-without-a-menu/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 22:25:08 +0000</pubDate>
		<dc:creator>Danny</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[wxWidgets]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[gui design]]></category>

		<guid isPermaLink="false">http://www.gabehabe.com/blog/?p=44</guid>
		<description><![CDATA[Quick entry. Sort of a preview. I&#8217;m working on a tutorial for dream.in.code about creating keyboard shortcuts in a wxWidgets application (particularly using C++), and figured I&#8217;d share the code here, too. (After I&#8217;ve finally gotten around to installing this syntax highlighting plugin for WordPress) Aaaaannddd&#8230; Done! Right. Back onto the original topic. Damn sidetracked [...]]]></description>
			<content:encoded><![CDATA[<p>Quick entry. Sort of a preview. I&#8217;m working on a tutorial for <a href="http://dreamincode.net" target="_blank">dream.in.code</a> about creating keyboard shortcuts in a wxWidgets application (particularly using C++), and figured I&#8217;d share the code here, too.</p>
<p>(After I&#8217;ve finally gotten around to installing <a href="http://wordpress.org/extend/plugins/wp-syntax" target="_blank">this syntax highlighting plugin</a> for WordPress)</p>
<p>Aaaaannddd&#8230; Done!</p>
<p>Right. Back onto the original topic. Damn sidetracked mind. So, when I write a tutorial, I write a whole chunk of code first, and then add the tutorial content around it. Here&#8217;s the code for allocating keyboard shortcuts to do different stuff in wxWidgets <strong>without</strong> using menu items.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// basic setup code, I'll fly past this. If you're not familiar, check out this tutorial:</span>
<span style="color: #666666;">// http://www.dreamincode.net/forums/showtopic66948.htm</span>
&nbsp;
<span style="color: #339900;">#include &lt;wx/wx.h&gt;</span>
&nbsp;
<span style="color: #0000ff;">class</span> app <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> wxApp <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
        <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">bool</span> OnInit<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
        wxTextCtrl<span style="color: #000040;">*</span> txt<span style="color: #008080;">;</span>
        wxCheckBox<span style="color: #000040;">*</span> chk<span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #0000ff;">void</span> key_shortcut<span style="color: #008000;">&#40;</span>wxKeyEvent<span style="color: #000040;">&amp;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        DECLARE_EVENT_TABLE<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// if you're not familiar with events, check out this tutorial:</span>
<span style="color: #666666;">// http://www.dreamincode.net/forums/showtopic67058.htm</span>
&nbsp;
BEGIN_EVENT_TABLE<span style="color: #008000;">&#40;</span>app, wxApp<span style="color: #008000;">&#41;</span>
    EVT_KEY_DOWN<span style="color: #008000;">&#40;</span>app<span style="color: #008080;">::</span><span style="color: #007788;">key_shortcut</span><span style="color: #008000;">&#41;</span>
END_EVENT_TABLE<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
&nbsp;
<span style="color: #0000ff;">bool</span> app<span style="color: #008080;">::</span><span style="color: #007788;">OnInit</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    wxFrame<span style="color: #000040;">*</span> win <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> wxFrame<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span>, wxID_ANY, wxT<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Keyboard Shortcuts, No Menu!&quot;</span><span style="color: #008000;">&#41;</span>, wxDefaultPosition, wxSize<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">250</span>, <span style="color: #0000dd;">125</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    win<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Show<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>txt <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> wxTextCtrl<span style="color: #008000;">&#40;</span>win, wxID_ANY, wxT<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Press Ctrl+G to append text to me&quot;</span><span style="color: #008000;">&#41;</span>, wxPoint<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>,<span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>, wxSize<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">250</span>, <span style="color: #0000dd;">50</span><span style="color: #008000;">&#41;</span>, wxTE_MULTILINE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>chk <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> wxCheckBox<span style="color: #008000;">&#40;</span>win, wxID_ANY, wxT<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Press Ctrl+Left to toggle me.&quot;</span><span style="color: #008000;">&#41;</span>, wxPoint<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">55</span><span style="color: #008000;">&#41;</span>, wxDefaultSize<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// this is the method I'll be focusing on. The key_shortcut event which we create</span>
<span style="color: #666666;">// to do fancy stuff without adding billions of menu items</span>
&nbsp;
<span style="color: #0000ff;">void</span> app<span style="color: #008080;">::</span><span style="color: #007788;">key_shortcut</span><span style="color: #008000;">&#40;</span>wxKeyEvent<span style="color: #000040;">&amp;</span> e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// of course, it doesn't have to be the control key. You can use others:</span>
    <span style="color: #666666;">// http://docs.wxwidgets.org/stable/wx_wxkeyevent.html</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>e.<span style="color: #007788;">GetModifiers</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> wxMOD_CONTROL<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span>e.<span style="color: #007788;">GetKeyCode</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">case</span> <span style="color: #FF0000;">'G'</span><span style="color: #008080;">:</span> <span style="color: #666666;">// can return the upper ASCII value of a key</span>
                <span style="color: #666666;">// do whatever you like for a Ctrl+G event here!</span>
                this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>txt<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>AppendText<span style="color: #008000;">&#40;</span>wxT<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot; gabehabe ftw!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">case</span> WXK_LEFT<span style="color: #008080;">:</span> <span style="color: #666666;">// we also have special keycodes for non-ascii values.</span>
                <span style="color: #666666;">// get a full list of special keycodes here:</span>
                <span style="color: #666666;">// http://docs.wxwidgets.org/stable/wx_keycodes.html</span>
                this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>chk<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>SetValue<span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>chk<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetValue<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">default</span><span style="color: #008080;">:</span> <span style="color: #666666;">// do nothing</span>
                <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
IMPLEMENT_APP<span style="color: #008000;">&#40;</span>app<span style="color: #008000;">&#41;</span></pre></div></div>

<p>Link to follow when the tutorial is completed and approved.</p>
<p>As promised, here&#8217;s the tutorial in all it&#8217;s glory:<br />
<a href="http://www.dreamincode.net/forums/showtopic125264.htm" target="_blank">wxWidgets keyboard shortcuts with no menu</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gabehabe.com/blog/wxwidgets-keyboard-shortcuts-without-a-menu/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

