<?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>Another Android Blog</title>
	<atom:link href="http://www.anotherandroidblog.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.anotherandroidblog.com</link>
	<description>Yet another Android developer&#039;s blog</description>
	<lastBuildDate>Fri, 30 Dec 2011 21:28:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Passing Toast Message to a New Activity</title>
		<link>http://www.anotherandroidblog.com/2011/12/30/passing-toast-message-to-a-new-activity/</link>
		<comments>http://www.anotherandroidblog.com/2011/12/30/passing-toast-message-to-a-new-activity/#comments</comments>
		<pubDate>Fri, 30 Dec 2011 21:25:46 +0000</pubDate>
		<dc:creator>Randall Mitchell</dc:creator>
				<category><![CDATA[Activity]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[android development]]></category>
		<category><![CDATA[custom application]]></category>
		<category><![CDATA[toast]]></category>

		<guid isPermaLink="false">http://www.anotherandroidblog.com/?p=329</guid>
		<description><![CDATA[Greetings readers. I have implemented a &#8220;very basic&#8221; messaging system within one of my applications. An example use case is as follows: 1. User arrives at an activity. 2. The activity realizes that the user needs to be in another activity. 3. The activity posts a user message to the application and sends the user [...]]]></description>
			<content:encoded><![CDATA[<p>Greetings readers.  I have implemented a &#8220;very basic&#8221; messaging system within one of my applications.  An example use case is as follows:</p>
<p>1. User arrives at an activity.<br />
2. The activity realizes that the user needs to be in another activity.<br />
3. The activity posts a user message to the application and sends the user on their way to the other activity.</p>
<p>Here is why I made it for my application: In my Pitching Coach application, the user arrives at the pitch chart when a game is opened.  If a pitcher has not yet been created for the game, the pitch chart sees this and sends the user to the create pitcher activity.  The pitch chart posts a message similar to &#8220;you need to create a pitcher in order to start the game.&#8221; to the system and now the create pitcher activity tells the user why they are on that activity. Lets look at how I did this&#8230;</p>
<p><script type="text/javascript"><!--
google_ad_client = "ca-pub-6156690421222683";
/* Standard Ad Unit */
google_ad_slot = "7299265357";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>I create a custom application class by extending android.app.Application.  This class will be live and accessible any time an activity for the app is visible.  It&#8217;s as simple as creating an object that extends Application and putting the code inside of the root package directory for the app.  Inside of the AndroidManifest I edit the application tag to contain the name of the new custom class.  This hooks up your application to the new application class.  Here is the snippet from the Android Manifest.</p>
<h2>The <em>AndroidManifest.xml</em> file</h2>

<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;manifest</span></span>
<span style="color: #009900;">	...</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;application</span></span>
<span style="color: #009900;">		<span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;CustomApplication&quot;</span></span>
<span style="color: #009900;">		...</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;/application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;manifest<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Inside of the new application class, I create a couple of methods.  One for posting a message and one for displaying and deleting the message.  I also create a member of type string named applicationMessage.  Here is the code:</p>
<p>(Note: Since the work of posting a toast message is done here already, I also add the showToastMessage() method so I can quickly post a toast message to the user at any time.  Extra little bonus <img src='http://www.anotherandroidblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> )</p>
<h2>The <em>CustomApplication.java</em> class</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.mywebsite.myapplication</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.app.Application</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.LayoutInflater</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.ViewGroup</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.TextView</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.Toast</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CustomApplication <span style="color: #000000; font-weight: bold;">extends</span> Application <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> CustomApplication<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/** ----------------------- MESSAGE HANDLING ----------------------- */</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> message <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setApplicationMessage<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> messageText<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		message <span style="color: #339933;">=</span> messageText<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> showAndDeleteApplicationMessage<span style="color: #009900;">&#40;</span>Activity activity<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>message.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			LayoutInflater inflater <span style="color: #339933;">=</span> activity.<span style="color: #006633;">getLayoutInflater</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">View</span> toastLayout <span style="color: #339933;">=</span> inflater.<span style="color: #006633;">inflate</span><span style="color: #009900;">&#40;</span>
					R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">toast_layout</span>,
					<span style="color: #009900;">&#40;</span>ViewGroup<span style="color: #009900;">&#41;</span> activity.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">toast_layout_root</span><span style="color: #009900;">&#41;</span>
					<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			TextView text <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> toastLayout.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">toastlayout_text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			text.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			message <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
			Toast toast <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Toast<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>
			toast.<span style="color: #006633;">setDuration</span><span style="color: #009900;">&#40;</span>Toast.<span style="color: #006633;">LENGTH_LONG</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			toast.<span style="color: #006633;">setView</span><span style="color: #009900;">&#40;</span>toastLayout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			toast.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> showToastMessage<span style="color: #009900;">&#40;</span>Activity activity, Message messageText<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		message <span style="color: #339933;">=</span> messageText<span style="color: #339933;">;</span>
		showAndDeleteApplicationMessage<span style="color: #009900;">&#40;</span>activity<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><script type="text/javascript"><!--
google_ad_client = "ca-pub-6156690421222683";
/* Standard Ad Unit */
google_ad_slot = "7299265357";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>Now when I want to post a message for an activity I am going to launch.  I simply call a reference to my application inside of the first activity and send it a message.  Then, in an activity that may be receiving a message, I make a call to showAndDeleteApplicationMessage() inside of onResume() &#8211; or onCreate().  If the message string is empty, it does nothing.  Here is some brief code on embedding the application class and posting the message:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.mymojosport.pitchingcoach</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.mywebsite.myapplication.R</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.mywebsite.myapplication.CustomApplication</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CustomActivity <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/** ------------------------ INITIALIZATION ------------------------- */</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> CustomApplication app<span style="color: #339933;">;</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>
		<span style="color: #666666; font-style: italic;">// create a reference to the custom activity</span>
		app <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>CustomApplication<span style="color: #009900;">&#41;</span> getApplication<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// show any messages posted to the application</span>
		app.<span style="color: #006633;">showAndDeleteApplicationMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now calling the activity and sending it a message is as simple as (from the calling activity):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">app.<span style="color: #006633;">setApplicationMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;You need to create a pitcher in order to start the game.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Intent intent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, CustomActivity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
startActivity<span style="color: #009900;">&#40;</span>intent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Hope this helps the world out there!<br />
Randall</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anotherandroidblog.com/2011/12/30/passing-toast-message-to-a-new-activity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Are you an unemployed Junior Java Type?</title>
		<link>http://www.anotherandroidblog.com/2011/10/08/are-you-an-unemployed-junior-java-type/</link>
		<comments>http://www.anotherandroidblog.com/2011/10/08/are-you-an-unemployed-junior-java-type/#comments</comments>
		<pubDate>Sat, 08 Oct 2011 06:50:31 +0000</pubDate>
		<dc:creator>Randall Mitchell</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Fresher]]></category>
		<category><![CDATA[Junior Java Developer]]></category>

		<guid isPermaLink="false">http://www.anotherandroidblog.com/?p=320</guid>
		<description><![CDATA[I chanced upon this thread that might help to lift your spirits: http://lnkd.in/a5Y9tV It&#8217;s about looking for your first job. Cheers, Randall]]></description>
			<content:encoded><![CDATA[<h3>I chanced upon this thread that might help to lift your spirits:</h3>
<p><a href="http://lnkd.in/a5Y9tV">http://lnkd.in/a5Y9tV</a></p>
<h3>It&#8217;s about looking for your first job.</h3>
<h3>Cheers,<br />
Randall</h3>
]]></content:encoded>
			<wfw:commentRss>http://www.anotherandroidblog.com/2011/10/08/are-you-an-unemployed-junior-java-type/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Shared Preferences Explained</title>
		<link>http://www.anotherandroidblog.com/2011/07/14/using-shared-preferences-explained/</link>
		<comments>http://www.anotherandroidblog.com/2011/07/14/using-shared-preferences-explained/#comments</comments>
		<pubDate>Thu, 14 Jul 2011 17:09:18 +0000</pubDate>
		<dc:creator>Randall Mitchell</dc:creator>
				<category><![CDATA[Data Storage]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[android development]]></category>
		<category><![CDATA[data storage]]></category>
		<category><![CDATA[saving settings]]></category>
		<category><![CDATA[settings]]></category>
		<category><![CDATA[SharedPreferences]]></category>

		<guid isPermaLink="false">http://www.anotherandroidblog.com/?p=312</guid>
		<description><![CDATA[This post explains further the example code on the Android Developer site: http://developer.android.com/guide/topics/data/data-storage.html I am trying to dumb this down as best as I can, so some useful options may be left out. Here is the code found on that page as of July 14, 2011: public class Calc extends Activity &#123; public static final [...]]]></description>
			<content:encoded><![CDATA[<p>This post explains further the example code on the Android Developer site:</p>
<p>http://developer.android.com/guide/topics/data/data-storage.html</p>
<p>I am trying to dumb this down as best as I can, so some useful options may be left out.  Here is the code found on that page as of July 14, 2011:</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> Calc <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> PREFS_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;MyPrefsFile&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle state<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>state<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       . . .
&nbsp;
       <span style="color: #666666; font-style: italic;">// Restore preferences</span>
       SharedPreferences settings <span style="color: #339933;">=</span> getSharedPreferences<span style="color: #009900;">&#40;</span>PREFS_NAME, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #000066; font-weight: bold;">boolean</span> silent <span style="color: #339933;">=</span> settings.<span style="color: #006633;">getBoolean</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;silentMode&quot;</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       setSilent<span style="color: #009900;">&#40;</span>silent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onStop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onStop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// We need an Editor object to make preference changes.</span>
      <span style="color: #666666; font-style: italic;">// All objects are from android.context.Context</span>
      SharedPreferences settings <span style="color: #339933;">=</span> getSharedPreferences<span style="color: #009900;">&#40;</span>PREFS_NAME, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      SharedPreferences.<span style="color: #006633;">Editor</span> editor <span style="color: #339933;">=</span> settings.<span style="color: #006633;">edit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      editor.<span style="color: #006633;">putBoolean</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;silentMode&quot;</span>, mSilentMode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Commit the edits!</span>
      editor.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><script type="text/javascript"><!--
google_ad_client = "ca-pub-6156690421222683";
/* Standard Ad Unit */
google_ad_slot = "7299265357";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>Android has made storing application settings as simple as possible.  Using the SharedPreferences object and a few methods build into the Activity class, we can quickly handle application settings.</p>
<p>First, we need to give our settings a name.  In the example, Google names them MyPrefsFile.  Changing the name will result in not finding your existing settings, so don&#8217;t change this ever.</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;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> PREFS_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;MyPrefsFile&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Let&#8217;s look at the following line of code&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">SharedPreferences settings <span style="color: #339933;">=</span> getSharedPreferences<span style="color: #009900;">&#40;</span>PREFS_NAME, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We need to create a SharedPreferences object.  This is basically just a list of key-value pairs similar to a java HashTable.  Think of it as a named array list.  Our Activity has a built in method called getSharedPreferences() that will retrieve our existing settings for us.  If they are not there already, Android will create one for us.  All the heavy lifting is done without our help.  We pass it the name of the preferences &#8220;file&#8221; we chose earlier.  Remember, Android handles the &#8220;file&#8221;, all we do is pretend it&#8217;s there and Android will do the rest. The getSharedPreferences() method returns our key-value pairs object so calling&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">SharedPreferences settings <span style="color: #339933;">=</span> getSharedPreferences<span style="color: #009900;">&#40;</span>PREFS_NAME, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>.. creates a SharedPreferences object based on the settings named &#8220;MyPrefsFile&#8221;.  We now have a list of all our existing settings.  Lets get a setting.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">boolean</span> silent <span style="color: #339933;">=</span> settings.<span style="color: #006633;">getBoolean</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;silentMode&quot;</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
setSilent<span style="color: #009900;">&#40;</span>silent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We need the setting &#8220;silentMode&#8221;.  We create a boolean object named &#8220;silent&#8221; and set it equal to the setting &#8220;silentMode&#8221; in our SharedPreferences we created earlier.  The &#8220;false&#8221; is the default value&#8230;meaning if &#8220;silentMode&#8221; has not already been set, &#8220;silent&#8221; will be set to false.</p>
<p>The line &#8220;setSilent(silent)&#8221; is a call to a method we create in the Activity that tells our application to be silent.  It is just an example of using the setting that we retrieved.</p>
<p>Now Lets look at how to store settings.  In the example given by the Android team, settings are stored in onClose().  This means that any time an activity is closed the settings are stored.  When we collected settings last time, we used SharedPreferences.  That is a key/value pair holder.  We need to do things a little differently to store/update new data&#8230; but we need to get the current settings and add them to any settings we are going to add or change.  Lets grab the current application settings.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">SharedPreferences settings <span style="color: #339933;">=</span> getSharedPreferences<span style="color: #009900;">&#40;</span>PREFS_NAME, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><script type="text/javascript"><!--
google_ad_client = "ca-pub-6156690421222683";
/* Standard Ad Unit */
google_ad_slot = "7299265357";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>Now we need to create an object that handles storing and changing of settings.  We create this &#8220;settings editor&#8221; using our existing settings.  Let&#8217;s look at the code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">SharedPreferences.<span style="color: #006633;">Editor</span> editor <span style="color: #339933;">=</span> settings.<span style="color: #006633;">edit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Our SharedPreferences has built in functionality to create a &#8220;settings editor&#8221;.  Explaining this line thoroughly would require a discussion on builders.  Just know that we are creating an object of the form &#8220;SharedPreferences.Editor&#8221;.  We name it &#8220;editor&#8221; and we use the edit() method of &#8220;settings&#8221; to create it with all of our current application settings ready to go.  We now have a &#8220;settings editor&#8221;.  Let&#8217;s use it.</p>
<p>Sometime during the life of our program, we set our &#8220;silentMode&#8221; setting to true.  We want it to stay that way the next time the application opens.  We call &#8220;putBoolean()&#8221; in our editor, passing in the name of our setting, &#8220;silentMode&#8221;.  The mSilentMode is the boolean value that we need our setting set to, in this case it is equal to &#8220;true&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">editor.<span style="color: #006633;">putBoolean</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;silentMode&quot;</span>, mSilentMode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>With our new value stored in our settings editor, lets commit the changes.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">editor.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We write that one line of code, Android does the rest.  Our settings have been stored. The next time this Activity runs, it will go to the top of the code discussed in the post, only this time it will find the value of silentMode is set to true and calling setSilent(silent) will be passing in true.</p>
<h3>Let me know if there is any part of this post you don&#8217;t understand!<br/><br />
Randall</h3>
]]></content:encoded>
			<wfw:commentRss>http://www.anotherandroidblog.com/2011/07/14/using-shared-preferences-explained/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Protecting Your Android Application</title>
		<link>http://www.anotherandroidblog.com/2011/07/08/protecting-your-android-application/</link>
		<comments>http://www.anotherandroidblog.com/2011/07/08/protecting-your-android-application/#comments</comments>
		<pubDate>Fri, 08 Jul 2011 15:39:53 +0000</pubDate>
		<dc:creator>Randall Mitchell</dc:creator>
				<category><![CDATA[Android Development]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[android development]]></category>
		<category><![CDATA[obfuscate]]></category>
		<category><![CDATA[ssl]]></category>

		<guid isPermaLink="false">http://www.anotherandroidblog.com/?p=310</guid>
		<description><![CDATA[I was just thinking to myself, I need to make my published apps more secure. I found this post enlightening and a good start. It&#8217;s from the guy that wrote Android Wireless Application Development and covers obfuscation, using the Android License Verification Library, and SSL encryption for connected apps. Good stuff! http://www.informit.com/articles/article.aspx?p=1725260]]></description>
			<content:encoded><![CDATA[<p>I was just thinking to myself, I need to make my published apps more secure.  I found this post enlightening and a good start.  It&#8217;s from the guy that wrote Android Wireless Application Development and covers obfuscation, using the Android License Verification Library, and SSL encryption for connected apps.  Good stuff!</p>
<p>http://www.informit.com/articles/article.aspx?p=1725260</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anotherandroidblog.com/2011/07/08/protecting-your-android-application/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Custom Spinner Style</title>
		<link>http://www.anotherandroidblog.com/2011/07/01/custom-spinner-style/</link>
		<comments>http://www.anotherandroidblog.com/2011/07/01/custom-spinner-style/#comments</comments>
		<pubDate>Fri, 01 Jul 2011 14:35:28 +0000</pubDate>
		<dc:creator>Randall Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.anotherandroidblog.com/?p=307</guid>
		<description><![CDATA[I&#8217;ll have to do this later on, so this post is just a &#8220;bookmark&#8221;. It seems to be the better one that I found on the subject. http://www.mokasocial.com/2011/03/easily-create-a-default-custom-styled-spinner-android/]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll have to do this later on, so this post is just a &#8220;bookmark&#8221;.  It seems to be the better one that I found on the subject.</p>
<p>http://www.mokasocial.com/2011/03/easily-create-a-default-custom-styled-spinner-android/</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anotherandroidblog.com/2011/07/01/custom-spinner-style/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Button Hit Area for Custom Graphics</title>
		<link>http://www.anotherandroidblog.com/2011/07/01/button-hit-area-for-custom-graphics/</link>
		<comments>http://www.anotherandroidblog.com/2011/07/01/button-hit-area-for-custom-graphics/#comments</comments>
		<pubDate>Fri, 01 Jul 2011 14:13:01 +0000</pubDate>
		<dc:creator>Randall Mitchell</dc:creator>
				<category><![CDATA[Components]]></category>
		<category><![CDATA[UI Design]]></category>
		<category><![CDATA[android development]]></category>
		<category><![CDATA[android ui design]]></category>
		<category><![CDATA[bounding box]]></category>
		<category><![CDATA[button]]></category>
		<category><![CDATA[drawable]]></category>
		<category><![CDATA[hit area]]></category>
		<category><![CDATA[inset]]></category>

		<guid isPermaLink="false">http://www.anotherandroidblog.com/?p=293</guid>
		<description><![CDATA[I decided to create an &#8220;irregularly shaped&#8221; and small arrow image to be used as a button. The problem was, when I set it as a background image, the hit area only reported when the actual image was being hit. The quick fix is to use a drawable inset to create a bounding box around [...]]]></description>
			<content:encoded><![CDATA[<p>I decided to create an &#8220;irregularly shaped&#8221; and small arrow image to be used as a button.  The problem was, when I set it as a background image, the hit area only reported when the actual image was being hit.  The quick fix is to use a drawable inset to create a bounding box around the image.  Here&#8217;s my setup and the example image.  This post is short and sweet.  Leave a comment if you need further help &#8211; I&#8217;ll try to answer.</p>
<h3>Button XML code:</h3>
<p>/res/layout/batting_lineup_list_item.xml</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>
<span style="color: #009900;">    <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/battinglineuplistitem_upbutton&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_weight</span>=<span style="color: #ff0000;">&quot;40&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;0dip&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;40dip&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:background</span>=<span style="color: #ff0000;">&quot;@drawable/arrow_up_inset_drawable&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 style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...</pre></div></div>

<h3>The Drawable Inset</h3>
<p>/res/drawable/arrow_up_inset_drawable.xml</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;inset</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:drawable</span>=<span style="color: #ff0000;">&quot;@drawable/arrow_up&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:insetTop</span>=<span style="color: #ff0000;">&quot;16dp&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:insetBottom</span>=<span style="color: #ff0000;">&quot;12dp&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:insetLeft</span>=<span style="color: #ff0000;">&quot;8dp&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:insetRight</span>=<span style="color: #ff0000;">&quot;8dp&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;/inset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3>The Button Graphic PNG Image</h3>
<p>The <em>@drawable/arrow_up</em> reference is pointing to the drawable png file inside of the resources directory&#8230;</p>
<p>/res/drawable-hdpi/arrow_up.png<br />
/res/drawable-ldpi/arrow_up.png<br />
/res/drawable-mdpi/arrow_up.png</p>
<p><h3>End Result</h3>
<p><a href="http://www.anotherandroidblog.com/wp-content/uploads/2011/07/inset_drawable_example.png"><img src="http://www.anotherandroidblog.com/wp-content/uploads/2011/07/inset_drawable_example.png" alt="" title="inset_drawable_example" width="384" height="384" class="aligncenter size-full wp-image-294" /></a></p>
<h3>Have a great one!<br />
Randall</h3>
]]></content:encoded>
			<wfw:commentRss>http://www.anotherandroidblog.com/2011/07/01/button-hit-area-for-custom-graphics/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Drop Shadow LinearLayout</title>
		<link>http://www.anotherandroidblog.com/2011/06/29/drop-shadow-linearlayout/</link>
		<comments>http://www.anotherandroidblog.com/2011/06/29/drop-shadow-linearlayout/#comments</comments>
		<pubDate>Wed, 29 Jun 2011 09:23:22 +0000</pubDate>
		<dc:creator>Randall Mitchell</dc:creator>
				<category><![CDATA[Components]]></category>
		<category><![CDATA[UI Design]]></category>
		<category><![CDATA[android development]]></category>
		<category><![CDATA[android ui design]]></category>
		<category><![CDATA[custom component]]></category>
		<category><![CDATA[dropshadow]]></category>

		<guid isPermaLink="false">http://www.anotherandroidblog.com/?p=283</guid>
		<description><![CDATA[I thought I would share this quick tip I just cooked up. To create a drop shadow below a layout component, you can add a &#8220;view&#8221; of the same width immediately below that component and set the background of that view to a drawable xml file containing a gradient. Here is an example layout file [...]]]></description>
			<content:encoded><![CDATA[<p>I thought I would share this quick tip I just cooked up.  To create a drop shadow below a layout component, you can add a &#8220;view&#8221; of the same width immediately below that component and set the background of that view to a drawable xml file containing a gradient.</p>
<p>Here is an example layout file containing the item in need of a drop shadow.  We want to place a drop shadow below the LinearLayout with id &#8220;some_layout_item&#8221; (see below).  I place a generic view below <em>some_layout_item</em> and specify what ever height I would like.  In this case, I set the height for 5dip.  That is how high my drop shadow will be.  I then set the view&#8217;s background to the drawable <em>drop_shadow</em>.</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;RelativeLayout</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:background</span>=<span style="color: #ff0000;">&quot;@color/cream&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_width</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;LinearLayout</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/some_layout_item&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000000; font-weight: bold;">&gt;</span></span>
        ...
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/LinearLayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;View</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:layout_below</span>=<span style="color: #ff0000;">&quot;@id/some_layout_item&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;5dip&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:background</span>=<span style="color: #ff0000;">&quot;@drawable/drop_shadow&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;/View<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    ...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/RelativeLayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now lets create that drop shadow drawable.  We do this by creating the file &#8220;res/drawable/drop_shadow.xml&#8221; as coded below (if you don&#8217;t have a folder named drawable in the res directory, just right click res and create it).  All that is in the file is a specification for a <em>shape</em> with a gradient.  The <em>startColor</em> is the darker part of the shadow that sits below and touching the LinearLayout.  All I did was make a darker color similar to the background color specified in the parent container.  The <em>endColor</em> is the color of the parent container&#8217;s background.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;shape</span> <span style="color: #000066;">xmlns:android</span>=”http://schemas.android.com/apk/res/android”<span style="color: #000000; font-weight: bold;">&gt;</span></span>    
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;gradient</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:startColor</span>=<span style="color: #ff0000;">&quot;@color/cream_dark&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:endColor</span>=<span style="color: #ff0000;">&quot;@color/cream&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:angle</span>=<span style="color: #ff0000;">&quot;270&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;/gradient<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/shape<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Suddenly, we have a drop shadow that just works.  The beauty of using this inside of a relative layout is you can insert the drop shadow without changing a anything else inside of layout file.</p>
<p>Cheers,<br />
Randall</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anotherandroidblog.com/2011/06/29/drop-shadow-linearlayout/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Calling Activity for Result</title>
		<link>http://www.anotherandroidblog.com/2011/06/09/calling-activity-for-result/</link>
		<comments>http://www.anotherandroidblog.com/2011/06/09/calling-activity-for-result/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 14:40:44 +0000</pubDate>
		<dc:creator>Randall Mitchell</dc:creator>
				<category><![CDATA[Activity]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[android development]]></category>
		<category><![CDATA[Intent]]></category>

		<guid isPermaLink="false">http://www.anotherandroidblog.com/?p=276</guid>
		<description><![CDATA[I was going to write one up but this site did the work as well as I could have: See the tutorial here It&#8217;s really a relatively simple problem with a relatively simple solution. Like I prefer to do in my posts, however, the solution has been completely explained. Ps. Nice blog template Sai]]></description>
			<content:encoded><![CDATA[<p>I was going to write one up but this site did the work as well as I could have: <a href="http://saigeethamn.blogspot.com/2009/08/android-developer-tutorial-for_31.html">See the tutorial here</a></p>
<p>It&#8217;s really a relatively simple problem with a relatively simple solution.  Like I prefer to do in my posts, however, the solution has been completely explained.</p>
<p>Ps.  Nice blog template Sai <img src='http://www.anotherandroidblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.anotherandroidblog.com/2011/06/09/calling-activity-for-result/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Passing Data With an Intent</title>
		<link>http://www.anotherandroidblog.com/2011/05/27/passing-data-with-an-intent/</link>
		<comments>http://www.anotherandroidblog.com/2011/05/27/passing-data-with-an-intent/#comments</comments>
		<pubDate>Fri, 27 May 2011 01:47:10 +0000</pubDate>
		<dc:creator>Randall Mitchell</dc:creator>
				<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://www.anotherandroidblog.com/?p=264</guid>
		<description><![CDATA[This is the code to pass data from one activity to another through an Intent. This is the code used to create and pass data into an Intent. String dataToPass = &#34;The next activity needs this sentence.&#34;; ... Intent intent = new Intent&#40;this, NextActivity.class&#41;; intent.putExtras&#40;&#34;KeyToAccessData&#34;, dataToPass&#41;; startActivity&#40;intent&#41;; And this is how you catch that data [...]]]></description>
			<content:encoded><![CDATA[<p>This is the code to pass data from one activity to another through an Intent.</p>
<p>This is the code used to create and pass data into an Intent.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> dataToPass <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;The next activity needs this sentence.&quot;</span><span style="color: #339933;">;</span>
...
<span style="color: #006633;">Intent</span> intent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, NextActivity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
intent.<span style="color: #006633;">putExtras</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;KeyToAccessData&quot;</span>, dataToPass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
startActivity<span style="color: #009900;">&#40;</span>intent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And this is how you catch that data from the receiving activity:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> dataToCollect<span style="color: #339933;">;</span>
...
<span style="color: #006633;">Intent</span> intent <span style="color: #339933;">=</span> getIntent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
dataToCollect <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getStringExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;KeyToAccessData&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note that the getIntent() method is part of the Android Activity class.  Every Activity knows it&#8217;s own intent.  We are just creating a way to reference it easily.  You can also collect Integers (getIntExtra()) and other various data types using this method.</p>
<hr />
This post is part of a new snippet series I will be adding to my blog. Basically, it’s a way for me to quickly look up code when my brain decides to bury it. If I have to look it up more than once, I’ll try to add it here as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anotherandroidblog.com/2011/05/27/passing-data-with-an-intent/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom Composite Android Component</title>
		<link>http://www.anotherandroidblog.com/2011/05/26/custom-composite-android-component/</link>
		<comments>http://www.anotherandroidblog.com/2011/05/26/custom-composite-android-component/#comments</comments>
		<pubDate>Thu, 26 May 2011 20:58:32 +0000</pubDate>
		<dc:creator>Randall Mitchell</dc:creator>
				<category><![CDATA[Components]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[android development]]></category>
		<category><![CDATA[composite component]]></category>
		<category><![CDATA[custom component]]></category>

		<guid isPermaLink="false">http://www.anotherandroidblog.com/?p=250</guid>
		<description><![CDATA[This example puts a couple of TextViews into a LinearLayout.  The combination of components is then treated as one component with the added functionality of programmatically changing the text of the two TextViews.]]></description>
			<content:encoded><![CDATA[<p>This example puts a couple of TextViews into a LinearLayout.  The combination of components is then treated as one component with the added functionality of programmatically changing the text of the two TextViews.  I pretty much modified this off of something I learned and forgot.  It took me a good bit of digging to find it again.  Check maohao.com for the original source/alternate version.  I&#8217;m putting it here so I don&#8217;t loose it (again). I&#8217;m sure it&#8217;s somewhere on developer.google.com but I just couldn&#8217;t find a specific example not bogged down by other code (i.e., ListView).  Also, I&#8217;m not even sure this works well for ListView.  That said, it does have practical applications.  For example, a list that will always be small.  For further explanation, please visit maohao at:<br />
<span id="more-250"></span><br />
<a href="http://maohao.wordpress.com/2009/08/27/building-mix-up-custom-component-android/">http://maohao.wordpress.com/2009/08/27/building-mix-up-custom-component-android/</a></p>
<p>Here is a working example of my version.  The first code below,<b>two_text_components.xml</b>, has root LinearLayout and two TextView components with id <b>text_one</b> and <b>text_two</b>.  This is the XML layout of the the composite component we want to create.</p>
<h2>two_text_components.xml</h2>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;LinearLayout</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">android:orientation</span>=<span style="color: #ff0000;">&quot;horizontal&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>
<span style="color: #009900;">		<span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/text_one&quot;</span></span>
<span style="color: #009900;">		<span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">		<span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">		<span style="color: #000066;">android:layout_weight</span>=<span style="color: #ff0000;">&quot;1&quot;</span></span>
<span style="color: #009900;">		<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>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextView</span></span>
<span style="color: #009900;">		<span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/text_two&quot;</span></span>
<span style="color: #009900;">		<span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">		<span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">		<span style="color: #000066;">android:layout_weight</span>=<span style="color: #ff0000;">&quot;9&quot;</span></span>
<span style="color: #009900;">		<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>
<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>We are going to programmatically set the text for <b>text_one</b> and <b>text_two</b>.  In the Maohao post, he specifies a namespace and is able to insert variables directly into the xml of the composite component &#8211; definitely worth taking a look at.  The most important aspect of this example is connecting the XML to the Java.  That&#8217;s done on a single line below that point&#8217;s the Activity&#8217;s layout inflator to the appropriate xml file.  Also make sure most of your work is done in onFinishInflate() or later; otherwise, you&#8217;ll end up with null pointer exceptions. Take a look at the Java code.</p>
<h2>TwoTextWidget.java</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.anotherandroidblog.snippets.CustomCompositeViewExample.customcomponents</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.anotherandroidblog.snippets.CustomCompositeViewExample.R</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Context</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.util.AttributeSet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.LinearLayout</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.TextView</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TwoTextWidget <span style="color: #000000; font-weight: bold;">extends</span> LinearLayout <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> TextView textOne<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> TextView textTwo<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> TwoTextWidget<span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span> context, <span style="color: #003399;">AttributeSet</span> attrs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>context, attrs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onFinishInflate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onFinishInflate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>Activity<span style="color: #009900;">&#41;</span>getContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getLayoutInflater</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">inflate</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">two_text_component</span>, <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setupViewItems<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;">private</span> <span style="color: #000066; font-weight: bold;">void</span> setupViewItems<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		textOne <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">text_one</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		textTwo <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">text_two</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTextOne<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> text<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		textOne.<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>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTextTwo<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> text<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		textTwo.<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>Now let&#8217;s see how we add the new composite widget into the Activity&#8217;s XML.  You must use the full package/class name of the new composite widget.  You can use any of the standard LinearLayout properties in our composite widget (which extends from LinearLayout).</p>
<h2>main.xml</h2>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;LinearLayout</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:orientation</span>=<span style="color: #ff0000;">&quot;vertical&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android: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>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;@string/title&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;com.anotherandroidblog.snippets.CustomCompositeViewExample.customcomponents.TwoTextWidget</span></span>
<span style="color: #009900;">    	<span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/list_item_one&quot;</span></span>
<span style="color: #009900;">    	<span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    	<span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">    	<span style="color: #000066;">android:orientation</span>=<span style="color: #ff0000;">&quot;horizontal&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;com.anotherandroidblog.snippets.CustomCompositeViewExample.customcomponents.TwoTextWidget</span></span>
<span style="color: #009900;">    	<span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/list_item_two&quot;</span></span>
<span style="color: #009900;">    	<span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    	<span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">    	<span style="color: #000066;">android:orientation</span>=<span style="color: #ff0000;">&quot;horizontal&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>Here is our last file.  It is just the Activity class that runs the application.  I&#8217;ve called the two methods created in the composite component for each instance we use them.</p>
<h2>Home.java</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.anotherandroidblog.snippets.CustomCompositeViewExample</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.anotherandroidblog.snippets.CustomCompositeViewExample.customcomponents.TwoTextWidget</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.util.Log</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> Home <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> TwoTextWidget itemOne<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> TwoTextWidget itemTwo<span style="color: #339933;">;</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>
        setupViews<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;">private</span> <span style="color: #000066; font-weight: bold;">void</span> setupViews<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	itemOne <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TwoTextWidget<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">list_item_one</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	itemOne.<span style="color: #006633;">setTextOne</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	itemOne.<span style="color: #006633;">setTextTwo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	itemTwo <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TwoTextWidget<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">list_item_two</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	itemTwo.<span style="color: #006633;">setTextOne</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	itemTwo.<span style="color: #006633;">setTextTwo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Goodbye&quot;</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>

<h2>Project Files</h2>
<p><a href="www.anotherandroidblog.com/files/CustomCompositeViewExample.zip">Here is the project file.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.anotherandroidblog.com/2011/05/26/custom-composite-android-component/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

