<?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>Public Static Droid Main</title>
	<atom:link href="http://publicstaticdroidmain.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://publicstaticdroidmain.com</link>
	<description>Expert Android development from San Francisco</description>
	<lastBuildDate>Mon, 19 Mar 2012 00:38:42 +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>Example Robolectric and Ant Eclipse Project</title>
		<link>http://publicstaticdroidmain.com/2012/03/robolectric-ant-project-eclipse/</link>
		<comments>http://publicstaticdroidmain.com/2012/03/robolectric-ant-project-eclipse/#comments</comments>
		<pubDate>Mon, 19 Mar 2012 00:38:42 +0000</pubDate>
		<dc:creator>Justin Schultz</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[ant]]></category>
		<category><![CDATA[build path]]></category>
		<category><![CDATA[build server]]></category>
		<category><![CDATA[ci]]></category>
		<category><![CDATA[continuous integration]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jenkins]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[robolectric]]></category>
		<category><![CDATA[sample]]></category>
		<category><![CDATA[test]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[unit]]></category>
		<category><![CDATA[unit test]]></category>
		<category><![CDATA[unit tests]]></category>

		<guid isPermaLink="false">http://publicstaticdroidmain.com/?p=629</guid>
		<description><![CDATA[Until recently, I&#8217;ve been unable to configure a Robolectric project that can easily be shared across a team and integrated with Jenkins CI without the use of Maven. I mentioned this in a previous post, and have asked for assistance on Pivotal&#8217;s Robolectric Google Group a handful of times. There are several jar files that [...]]]></description>
			<content:encoded><![CDATA[<p>Until recently, I&#8217;ve been unable to configure a Robolectric project that can easily be shared across a team and integrated with <a href="http://jenkins-ci.org/" title="Jenkins CI Server Robolectric Example" target="_blank">Jenkins CI</a> without the use of Maven. I mentioned this in a <a href="http://publicstaticdroidmain.com/2012/01/unit-testing-android-applications-with-robolectric/" title="Unit Testing Android Applications With Robolectric" target="_blank">previous post</a>, and have asked for assistance on Pivotal&#8217;s <a href="http://groups.google.com/group/robolectric?pli=1" title="Robolectric Google Group" target="_blank">Robolectric Google Group</a> a handful of times. There are several jar files that need to be referenced, and dependencies between the test and main Android projects and (without the use of Maven) this is difficult to configure and move around between teammates. </p>
<p>Thankfully, a brilliant Android engineer named <a href="http://digitaldumptruck.jotabout.com/" title="Michael Portuesi's Blog" target="_blank">Michael Portuesi</a> came to my aid and helped me sort out the missing pieces, and I decided to create an <a href="https://github.com/jmschultz/Eclipse-Robolectric-Example" title="Eclipse Robolectric Example" target="_blank">example Robolectric project</a> to share his wisdom and help other people get started. This project also includes an Ant script that you can easily plug into Jenkins or any other Ant-based CI process.</p>
<h1 style="padding-top: 14px">How does it work?</h1>
<p>A Robolectric test project requires dependencies on:</p>
<ul>
<li>android.jar</li>
<li>maps.jar</li>
<li>junit.jar</li>
<li>hamcrest.jar</li>
<li>robolectric-with-dependencies.jar</li>
<li>The Android project being tested</li>
</ul>
<p>There are several ways you can set this up, but for simplicity I&#8217;ve pinned these jar files into the test project&#8217;s libs folder. Some people prefer linking the <a href="https://github.com/pivotal/robolectric/" title="Robolectric Source via GitHub" target="_blank">Robolectric source</a> into their test projects, so they can modify the Robolectric code as necessary. You can also use environment variables in place of the Maps and Android jars, and add references to JUnit and Hamcrest to your test project&#8217;s build path via Eclipse.</p>
<p>After cloning this repository, add the android-project and android-project-test projects into an Eclipse workspace. The android-project is an Android project (obviously), and the android-project-test project is a plain old Java project. You should be able to right-click on the android-project-test project, and choose &#8220;Run As -> JUnit Test&#8221;, and the single test should run via Robolectric and JUnit. Alternatively, you should be able to right-click on the build.xml file and choose &#8220;Run As -> Ant Build&#8221;. This ant script can be used to run your Robolectric tests via a CI server like Jenkins.</p>
<p>Take note of <a href="https://github.com/jmschultz/Eclipse-Robolectric-Example/blob/master/android-project-test/src/com/justinschultz/tests/runner/SampleTestRunner.java" title="SampleTestRunner.java" target="_blank">SampleTestRunner.java</a>. This class inherits from RobolectricTestRunner, and tells Robolectric how to resolve references to your Android project&#8217;s manifest and resources:</p>
<pre class="brush: java; title: ; notranslate">
package com.justinschultz.tests.runner;

import java.io.File;

import org.junit.runners.model.InitializationError;

import com.xtremelabs.robolectric.RobolectricConfig;
import com.xtremelabs.robolectric.RobolectricTestRunner;

public class SampleTestRunner extends RobolectricTestRunner {
	/**
	 * Call this constructor to specify the location of resources and AndroidManifest.xml.
	 *
	 * @param testClass
	 * @throws InitializationError
	 */
	public SampleTestRunner(@SuppressWarnings(&quot;rawtypes&quot;) Class testClass) throws InitializationError {
		super(testClass, new RobolectricConfig(new File(&quot;../android-project/AndroidManifest.xml&quot;), new File(&quot;../android-project/res&quot;)));
	}
}
</pre>
<p>When authoring tests, make sure your class names end with the word &#8220;Test&#8221;, as the ant script requires this. Also make sure that their <a href="http://junit.sourceforge.net/javadoc/org/junit/runner/RunWith.html" title="@RunWith Annotation" target="_blank">@RunWith annotations</a> use the SampleTestRunner.class, and NOT the RobolectricTestRunner.class:</p>
<pre class="brush: java; title: ; notranslate">
@RunWith(SampleTestRunner.class)
public class SampleTest {
    @Test
    public void testBasicResourceValue() throws Exception {
        String helloFromActivity = new MainActivity().getResources().getString(R.string.hello);
        assertThat(helloFromActivity, equalTo(&quot;Hello World, MainActivity!&quot;));
    }
}
</pre>
<p>Finally, make sure junit.jar is defined in your Ant script&#8217;s classpath BEFORE android.jar, otherwise <a href="http://digitaldumptruck.jotabout.com/?p=773" title="Ant Robolectric Configuration" target="_blank">Ant will throw an error</a>:</p>
<pre class="brush: xml; title: ; notranslate">
	&lt;path id=&quot;junit_classpath&quot;&gt;
		&lt;pathelement path=&quot;${build.dir}&quot;/&gt;
		&lt;pathelement path=&quot;${android.project.classpath}&quot;/&gt;
		&lt;!-- NOTE: junit.jar must come before android.jar! --&gt;
		&lt;filelist refid=&quot;libs_jars&quot;/&gt;
		&lt;filelist refid=&quot;android_jars&quot;/&gt;
	&lt;/path&gt;
</pre>
<p>And that&#8217;s it. You should be able to clone the <a href="https://github.com/jmschultz/Eclipse-Robolectric-Example" title="Android Eclipse Robolectric Example" target="_blank">Android Eclipse Robolectric Example</a> from GitHub and immediately run the sample test via Ant or JUnit in Eclipse.</p>
]]></content:encoded>
			<wfw:commentRss>http://publicstaticdroidmain.com/2012/03/robolectric-ant-project-eclipse/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Double-Tap to Zoom in Android Maps</title>
		<link>http://publicstaticdroidmain.com/2012/03/double-tap-zoom-android-mapactivity-mapview/</link>
		<comments>http://publicstaticdroidmain.com/2012/03/double-tap-zoom-android-mapactivity-mapview/#comments</comments>
		<pubDate>Sun, 04 Mar 2012 02:04:05 +0000</pubDate>
		<dc:creator>Justin Schultz</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[double tap]]></category>
		<category><![CDATA[double tap to zoom]]></category>
		<category><![CDATA[gesture]]></category>
		<category><![CDATA[gesturelistener]]></category>
		<category><![CDATA[listener]]></category>
		<category><![CDATA[maps]]></category>
		<category><![CDATA[onDoubleTap]]></category>
		<category><![CDATA[zoom]]></category>

		<guid isPermaLink="false">http://publicstaticdroidmain.com/?p=583</guid>
		<description><![CDATA[Double-Tap to zoom is a typical feature for mapped interfaces on Android. Sometimes it&#8217;s just annoying trying to reverse-pinch to zoom with one hand while holding a device. This is something that&#8217;s not technically difficult to do, but it&#8217;s not documented very well. Here&#8217;s how to do it: Write a class that inherits from MapView, [...]]]></description>
			<content:encoded><![CDATA[<p>Double-Tap to zoom is a typical feature for mapped interfaces on Android. Sometimes it&#8217;s just annoying trying to reverse-pinch to zoom with one hand while holding a device. </p>
<p>This is something that&#8217;s not technically difficult to do, but it&#8217;s not documented very well. </p>
<p>Here&#8217;s how to do it:</p>
<ol>
<li style="margin-top: 1em">Write a class that inherits from MapView, and add a GestureDetector to the child class. Then, override the onTouchEvent(MotionEvent event) method and pass the MotionEvent along to your GestureDetector:
</ol>
<pre class="brush: java; title: ; notranslate">
package com.justinschultz.android.controls;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import com.google.android.maps.MapView;

public class MyMapView extends MapView
{
	private GestureDetector gestureDetector;

	public MyMapView(Context context, AttributeSet attrs)
	{
		super(context, attrs);

		gestureDetector = new GestureDetector((OnGestureListener) context);
		gestureDetector.setOnDoubleTapListener((OnDoubleTapListener) context);
	}

    @Override
	public boolean onTouchEvent(MotionEvent ev)
	{
		return this.gestureDetector.onTouchEvent(ev);
	}
}
</pre>
<ol start="2">
<li style="margin-top: 1em">Refer to this child class in your layout XML file, instead of the regular MapView:</li>
</ol>
<pre class="brush: xml; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
	android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot;&gt;
	&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
	    android:orientation=&quot;vertical&quot;
	    android:layout_width=&quot;fill_parent&quot;
	    android:layout_height=&quot;wrap_content&quot;&gt;

		&lt;com.justinschultz.android.controls.MyMapView
	    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
	    android:id=&quot;@+id/mapview&quot;
	    android:layout_width=&quot;fill_parent&quot;
	    android:layout_height=&quot;wrap_content&quot;
	    android:clickable=&quot;true&quot;
	    android:apiKey=&quot;YOUR_API_KEY&quot; /&gt;

	&lt;/RelativeLayout&gt;
&lt;/LinearLayout&gt;
</pre>
<ol start="3">
<li style="margin-top: 1em">Implement the <a href="http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html" target="_blank">OnGestureListener</a> and <a href="http://developer.android.com/reference/android/view/GestureDetector.OnDoubleTapListener.html" target="_blank">OnDoubleTapListener</a> interfaces in your MapActivity class, and write some code to zoom-in the map in the onDoubleTap(MotionEvent e) method:</li>
</ol>
<pre class="brush: java; title: ; notranslate">
public class Map extends MapActivity
    implements OnGestureListener, OnDoubleTapListener {
 // More map stuff here, and additional forced overrides from the interfaces
 @Override
 public boolean onDoubleTap(MotionEvent e) {
      // Assuming your MapView is called mapView
     mapView.getController().zoomIn();
     return true;
 }
</pre>
<p>And that&#8217;s it! Your map will now zoom-in when the user double-taps. </p>
]]></content:encoded>
			<wfw:commentRss>http://publicstaticdroidmain.com/2012/03/double-tap-zoom-android-mapactivity-mapview/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Inspecting and Improving UI Layouts With HierarchyViewer</title>
		<link>http://publicstaticdroidmain.com/2012/02/running-hierarchyviewer-android-device/</link>
		<comments>http://publicstaticdroidmain.com/2012/02/running-hierarchyviewer-android-device/#comments</comments>
		<pubDate>Sat, 18 Feb 2012 04:56:26 +0000</pubDate>
		<dc:creator>Justin Schultz</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[adt]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[hierarchy]]></category>
		<category><![CDATA[hierarchyviewer]]></category>
		<category><![CDATA[improve layout]]></category>
		<category><![CDATA[inspect]]></category>
		<category><![CDATA[layout]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[speed]]></category>
		<category><![CDATA[tree]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://publicstaticdroidmain.com/?p=539</guid>
		<description><![CDATA[HierarchyViewer is a visual tool that can be used to inspect your Android user interfaces and help you improve your layout designs and performance. It&#8217;s packaged with the Android SDK, and lives in the &#60;android-sdk&#62;/tools folder. To launch hierarchyviewer, simply open a terminal or command window and type hierarchyviewer. To open hierarchyviewer via Eclipse, select [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://developer.android.com/guide/developing/tools/hierarchy-viewer.html" target="_blank">HierarchyViewer</a> is a visual tool that can be used to inspect your Android user interfaces and help you improve your layout designs and performance. It&#8217;s packaged with the Android SDK, and lives in the &lt;android-sdk&gt;/tools folder.</p>
<p>To launch hierarchyviewer, simply open a terminal or command window and type <strong>hierarchyviewer</strong>. To open hierarchyviewer via Eclipse, select Window -> Open Perspective -> Hierarchy View.</p>
<p>For security reasons you can&#8217;t run it on-device, so you&#8217;ll need to have an application running on an emulator. Also, make sure Eclipse isn’t trying to debug your application when using HierarchyViewer or it won’t work.</p>
<p>As its name suggests, HierarchyViewer will present you with a hierarchy of the view objects for the layout you select. After you&#8217;ve launched the tool, select the Activity you&#8217;d like to profile and click the button labeled &#8220;Load View Hierarchy.&#8221; For my simple demo, the hierarchy looks like this:<br />
<div id="attachment_557" class="wp-caption aligncenter" style="width: 610px"><a href="http://publicstaticdroidmain.com/wp-content/uploads/2012/02/hierarchyviewer.png" target="_blank"><img src="http://publicstaticdroidmain.com/wp-content/uploads/2012/02/hierarchyviewer-1024x380.png" alt="" title="HierarchyViewer" width="600" height="222" class="size-large wp-image-557" /></a><p class="wp-caption-text">Hierarchy Viewer</p></div></p>
<h1 style="padding-top: 14px">UI Performance</h1>
<p>In addition to providing information about the view hierarchy, the HierarchyViewer tool also provides 3 important metrics about the rendering performance for each View object on the selected Layout&#8217;s UI:</p>
<ul>
<li>The time it takes to Measure</li>
<li>The time it takes to Layout</li>
<li>The time it takes to Draw</li>
</ul>
<p>Here&#8217;s a great Google IO presentation on using hierarchyviewer, and understanding these metrics:</p>
<p><iframe width="600" height="450" src="http://www.youtube.com/embed/PAgE7saQUUY?fs=1&#038;feature=oembed" frameborder="0" allowfullscreen></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://publicstaticdroidmain.com/2012/02/running-hierarchyviewer-android-device/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit Testing Android Applications With Robolectric</title>
		<link>http://publicstaticdroidmain.com/2012/01/unit-testing-android-applications-with-robolectric/</link>
		<comments>http://publicstaticdroidmain.com/2012/01/unit-testing-android-applications-with-robolectric/#comments</comments>
		<pubDate>Thu, 26 Jan 2012 04:28:46 +0000</pubDate>
		<dc:creator>Justin Schultz</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[build process]]></category>
		<category><![CDATA[build server]]></category>
		<category><![CDATA[ci]]></category>
		<category><![CDATA[continuous integration]]></category>
		<category><![CDATA[robolectric]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[tests]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[unit tests]]></category>

		<guid isPermaLink="false">http://publicstaticdroidmain.com/?p=42</guid>
		<description><![CDATA[Running unit tests on an Android emulator or device is slow! Building, deploying, and launching the application in an emulator often takes several minutes, and that&#8217;s painful. It&#8217;s downright unacceptable if you&#8217;re planning on running your unit-tests as part of a continuous-integration build process, because the build server will need to launch an emulator every [...]]]></description>
			<content:encoded><![CDATA[<p>Running unit tests on an Android emulator or device is slow! Building, deploying, and launching the application in an emulator often takes several minutes, and that&#8217;s painful. It&#8217;s downright unacceptable if you&#8217;re planning on running your unit-tests as part of a continuous-integration build process, because the build server will need to launch an emulator every time you build and test. </p>
<p>To make matters worse, Google prevents you from using the Android SDK if it&#8217;s not running on a device or emulator. If you do, you&#8217;ll be thwarted by a RuntimeException and message: </p>
<pre class="brush: java; title: ; notranslate">
java.lang.RuntimeException: Stub!
</pre>
<p>Pivotal Labs&#8217; <a href="http://pivotal.github.com/robolectric/" target="_blank">Robolectric</a> is a unit test framework that de-couples the Android SDK jar so you can test-drive the development of your Android app. Tests run inside the JVM on your workstation in seconds. </p>
<p>With Robolectric, you can write tests like this:</p>
<pre class="brush: java; title: ; notranslate">
// Test class for MyActivity
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {
    private Activity activity;
    private Button pressMeButton;
    private TextView results;

    @Before
    public void setUp() throws Exception {
        activity = new MyActivity();
        activity.onCreate(null);
        pressMeButton = (Button) activity.findViewById(R.id.press_me_button);
        results = (TextView) activity.findViewById(R.id.results_text_view);
    }

    @Test
    public void shouldUpdateResultsWhenButtonIsClicked() throws Exception {
        pressMeButton.performClick();
        String resultsText = results.getText().toString();
        assertThat(resultsText, equalTo(&quot;Robolectric Rules.&quot;));
    }
}
</pre>
<p>Robolectric makes this possible by intercepting the loading of the Android classes and rewriting the method bodies. Robolectric re-defines Android methods so they return null (or 0, false, etc.), or if provided Robolectric will forward method calls to shadow Android objects giving the Android SDK behavior. Robolectric provides a large number of shadow objects covering much of what a typical application would need to test-drive the business logic and functionality of your application. Coverage of the SDK is improving every day.</p>
<h1 style="padding-top: 14px">Resource Support</h1>
<p>Robolectric handles inflation of views, string resource lookups, etc. Some view attributes (id, visibility enabled, text, checked, and src) are parsed and applied to inflated views. Activity and View #findViewById() methods return Android view objects. Support exists for include and merge tags. These features allow tests access resources and to assert on view state.</p>
<h1 style="padding-top: 14px">Run Tests Outside of the Emulator</h1>
<p>Run your tests on your workstation, or on your Continuous Integration environment. Because tests run on your workstation in a JVM and not in the emulator Android runtime, the dexing, packaging, and installation on the emulator steps are not necessary, allowing you to iterate quickly and refactor your code with confidence.</p>
<p><a href="http://pivotal.github.com/robolectric/eclipse-quick-start.html" target="_blank">Here</a> is a link with step-by-step instructions on getting Robolectric set up in Eclipse.</p>
<h1 style="padding-top: 14px">Potential Robolectric Pitfalls</h1>
<p>I ran into a few issues using Robolectric at <a href="http://www.breezy.com/" target="_blank">Breezy</a>:</p>
<ul>
<li>Robolectric doesn&#8217;t support all Android SDK APIs</li>
<li>Running Robolectric in a team environment is difficult without something like <a href="http://maven.apache.org/" target="_blank">Maven</a>. Eclipse doesn&#8217;t allow you to set relative directory paths for libraries by default, so if your Robolectric, Android SDK, and JUnit libraries live in different paths on different developer&#8217;s machines, you&#8217;ll have a tough time sharing a configured test project.</li>
<li>Robolectric caches its shadow classes, which can produce unexpected test results. <a href="http://digitaldumptruck.jotabout.com/?p=773" target="_new">Here&#8217;s</a> a blog post about mitigating this.
</ul>
<p>Despite these few nuances, I love Robolectric. Here&#8217;s a great how-to <a href="http://www.youtube.com/watch?v=T6FWL877txw" target="_blank">video</a> on Robolectric by Pivotal Labs, and here&#8217;s a link to the <a href="http://groups.google.com/group/robolectric/topics?pli=1" target="_blank">Robolectric Google Group</a> if you run into problems. </p>
]]></content:encoded>
			<wfw:commentRss>http://publicstaticdroidmain.com/2012/01/unit-testing-android-applications-with-robolectric/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Android Threading: Part 1</title>
		<link>http://publicstaticdroidmain.com/2012/01/android-threading-part-1/</link>
		<comments>http://publicstaticdroidmain.com/2012/01/android-threading-part-1/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 05:00:24 +0000</pubDate>
		<dc:creator>Justin Schultz</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[asynctask]]></category>
		<category><![CDATA[handler]]></category>
		<category><![CDATA[looper]]></category>
		<category><![CDATA[multi-threaded]]></category>
		<category><![CDATA[threading-series]]></category>
		<category><![CDATA[threads]]></category>
		<category><![CDATA[ui]]></category>
		<category><![CDATA[ui thread]]></category>

		<guid isPermaLink="false">http://publicstaticdroidmain.com/?p=447</guid>
		<description><![CDATA[The UI layer of all Android applications runs on a single thread called the UI thread. Performing long-running operations such as network access or database queries on this thread will block the user interface, and eventually crash your application with an Application Not Responding (ANR) error. In this tutorial I&#8217;ll show you a simple way [...]]]></description>
			<content:encoded><![CDATA[<p>The UI layer of all Android applications runs on a single thread called the UI thread. </p>
<p>Performing long-running operations such as network access or database queries on this thread will block the user interface, and eventually crash your application with an Application Not Responding (ANR) error. </p>
<p>In this tutorial I&#8217;ll show you a simple way to perform a long-running task in the background using the <a href="http://developer.android.com/reference/android/os/AsyncTask.html" target="_blank">AsyncTask</a> class to download an image.</p>
<p>To start, create a new application and update the main.xml layout file with this:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;fill_parent&quot;
    android:layout_height=&quot;fill_parent&quot;
    android:orientation=&quot;vertical&quot; &gt;

    &lt;Button
        android:id=&quot;@+id/btnDownloadImages&quot;
        android:layout_width=&quot;fill_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@+id/helloText&quot;
        android:onClick=&quot;downloadImage&quot;
        android:text=&quot;Tap to Download Image&quot; /&gt;

    &lt;ImageView
        android:id=&quot;@+id/imgDownload&quot;
        android:layout_width=&quot;fill_parent&quot;
        android:layout_height=&quot;fill_parent&quot;
        android:layout_below=&quot;@+id/helloText&quot; /&gt;

&lt;/LinearLayout&gt;
</pre>
<p>When a user taps the button, we&#8217;ll: </p>
<ol>
<li>Display a busy dialog</li>
<li>Invoke an AsyncTask to download an image from the internet</li>
<li>Display the downloaded image in the ImageView control</li>
<li>Hide the busy dialog</li>
</ol>
<p>Here&#8217;s the Activity class to achieve this (note the comments):</p>
<pre class="brush: java; title: ; notranslate">
package com.justinschultz.asynctaskexample;

import java.io.InputStream;
import java.net.URL;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class AsyncTaskExampleActivity extends Activity {
	ImageView _imgView = null;
	ProgressDialog _busyDialog = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// Need an Activity-level reference to the ImageView
		_imgView = (ImageView) findViewById(R.id.imgDownload);
	}

	public void downloadImage(View v) {
		// Need to show the busy dialog here, since we can't do it in
		// DownloadImageTask's doInBackground() method
		showBusyDialog();
		new DownloadImageTask()
				.execute(&quot;http://code.google.com/android/goodies/wallpaper/android-wallpaper5_1024x768.jpg&quot;);
	}

	private class DownloadImageTask extends AsyncTask&lt;String, Void, Bitmap&gt; {
		@Override
		protected Bitmap doInBackground(String... url) {
			// You might be tempted to display the busy dialog here, but you
			// CAN'T update the UI from this method!
			return loadImageFromNetwork(url[0]);
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			// Our long-running process is done, and we can safely access the UI thread
			dismissBusyDialog();
			_imgView.setImageBitmap(result);
		}
	}

	private Bitmap loadImageFromNetwork(String url) {
		Bitmap bitmap = null;

		try {
			bitmap = BitmapFactory.decodeStream((InputStream) new URL(url).getContent());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bitmap;
	}

	public void showBusyDialog() {
		_busyDialog = ProgressDialog.show(this, &quot;&quot;, &quot;Downloading Image...&quot;, true);
	}

	public void dismissBusyDialog() {
		if (_busyDialog != null) {
			_busyDialog.dismiss();
		}
	}
}
</pre>
<p>The AsyncTask class must be subclassed, and it&#8217;s 3 generic types are:</p>
<ol>
<li><strong>Params</strong>: the type of the parameters sent to the task upon execution.
<li><strong>Progress</strong>: the type of the progress units published during the background computation.
<li><strong>Result</strong>: the type of the result of the background computation.
</ol>
<p>Not all types are always used by an asynchronous task. To mark a type as unused, simply use the type Void:</p>
<pre class="brush: java; title: ; notranslate">
private class MyTask extends AsyncTask&lt;Void, Void, Void&gt; { ... }
</pre>
<p>I don&#8217;t recommend using AsyncTask for things like long-running API calls, but it&#8217;s fine for short tasks within the activity life (between onResume and onPause).  I&#8217;ll cover approaches to long-running API calls in a future post. </p>
<p>Download the complete code for this example <a href="http://publicstaticdroidmain.com/wp-content/uploads/2012/01/AsyncTaskExample.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://publicstaticdroidmain.com/2012/01/android-threading-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Real-Time Android Messaging With Pusher</title>
		<link>http://publicstaticdroidmain.com/2012/01/real-time-android-messaging-with-pusher/</link>
		<comments>http://publicstaticdroidmain.com/2012/01/real-time-android-messaging-with-pusher/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 02:00:22 +0000</pubDate>
		<dc:creator>Justin Schultz</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[android game development]]></category>
		<category><![CDATA[channels]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[messaging]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[pusher]]></category>
		<category><![CDATA[real time]]></category>
		<category><![CDATA[web sockets]]></category>
		<category><![CDATA[websockets]]></category>

		<guid isPermaLink="false">http://publicstaticdroidmain.com/?p=264</guid>
		<description><![CDATA[Pusher is a hosted API for real-time JSON messaging in web and mobile applications via WebSockets. Examples Pusher applications: Updating a website or mobile application with sports scores or stock prices in real-time Sending real-time messages to Arduino devices from a web or mobile application Building a real-time donation thermometer on a website Tracking multi-player [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://pusher.com/" target="_blank">Pusher</a> is a hosted API for real-time JSON messaging in web and mobile applications via <a href="http://en.wikipedia.org/wiki/WebSocket" target="_blank">WebSockets</a>. </p>
<p>Examples Pusher applications:</p>
<ul>
<li> Updating a website or mobile application with sports scores or stock prices in real-time
<li> <a href="https://kevinrohling.wordpress.com/2011/09/14/world-domination-using-arduinos-and-websockets/" target="_blank">Sending real-time messages to Arduino devices</a> from a web or mobile application
<li> Building a real-time donation thermometer on a website
<li> Tracking multi-player game moves&#8230; in real-time
<li> <a href="http://pusher.com/showcase" target="_blank">You get the idea&#8230;</a>
</ul>
<p>I recently used Pusher at <a href="http://www.breezy.com/" target="_blank">Breezy</a> to notify user&#8217;s printers when documents sent from their mobile devices are ready to be printed. It&#8217;s very fast (real-time!) and powerful stuff, and I immediately started thinking about ways to implement it in Android applications. </p>
<p>Surprisingly, nobody had built a full-featured Java / Android client for Pusher, so I decided to <a href="https://github.com/jmschultz/JavaPusherClient">build one</a>. </p>
<p>Pusher is event-driven, so the first thing to do is create an event listener:</p>
<pre class="brush: java; title: ; notranslate">
PusherListener eventListener = new PusherListener() {
	@Override
	public void onConnect(String socketId) {
		System.out.println(&quot;Pusher connected. Socket Id is: &quot; + socketId);
	}

	@Override
	public void onMessage(String message) {
		System.out.println(&quot;Received message from Pusher: &quot; + message);
	}

	@Override
	public void onDisconnect() {
		System.out.println(&quot;Pusher disconnected.&quot;);
	}
};
</pre>
<p>Next, create an instance of the Pusher class and pass it the API key you were assigned while signing up for Pusher. Then set the event listener from the previous step and connect:</p>
<pre class="brush: java; title: ; notranslate">
Pusher pusher = new Pusher(YOUR_API_KEY);
pusher.setPusherListener(eventListener);
pusher.connect();
</pre>
<p>Pusher&#8217;s communication happens over public, private or presence <a href="http://pusher.com/docs/client_api_guide/client_channels" target="_blank">Channels</a>, and you can bind to or trigger events on any of these channel types:</p>
<pre class="brush: java; title: ; notranslate">
// Public Channel
channel = pusher.subscribe(PUSHER_CHANNEL);
// Private Channel
channel = pusher.subscribe(PUSHER_CHANNEL, AUTH_TOKEN);
// Presence Channel
channel = pusher.subscribe(PUSHER_CHANNEL, AUTH_TOKEN, USER_ID);
// Trigger an event
channel.send(&quot;trigger-event&quot;, new JSONObject());
// Bind to the &quot;price-updated&quot; event with a ChannelListener
channel.bind(&quot;price-updated&quot;, new ChannelListener() {
  @Override
  public void onMessage(String message) {
    System.out.println(&quot;Received bound channel message: &quot; + message);
  }
});
</pre>
<p>The Java / Android Pusher library can be found on GitHub <a href="https://github.com/jmschultz/JavaPusherClient" target="_blank">here</a>.</p>
<p>Please let me know if you have any feedback or questions.</p>
]]></content:encoded>
			<wfw:commentRss>http://publicstaticdroidmain.com/2012/01/real-time-android-messaging-with-pusher/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Avoiding Android Memory Leaks: Part 1</title>
		<link>http://publicstaticdroidmain.com/2012/01/avoiding-android-memory-leaks-part-1/</link>
		<comments>http://publicstaticdroidmain.com/2012/01/avoiding-android-memory-leaks-part-1/#comments</comments>
		<pubDate>Wed, 11 Jan 2012 06:00:17 +0000</pubDate>
		<dc:creator>Justin Schultz</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[activity]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[avoiding memory leaks]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[dialog]]></category>
		<category><![CDATA[leaks]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[memory-leak-series]]></category>
		<category><![CDATA[onCreate]]></category>
		<category><![CDATA[onPause]]></category>

		<guid isPermaLink="false">http://publicstaticdroidmain.com/?p=160</guid>
		<description><![CDATA[I recently discovered how easy it is to accidentally &#8220;leak&#8221; memory and resources in an Android application. In this tutorial, I&#8217;ll illustrate a memory leak with a simple dialog box example, and then show you how to fix it. Let&#8217;s start by creating a simple Android application that displays a dialog in its onCreate event: [...]]]></description>
			<content:encoded><![CDATA[<p>I recently discovered how easy it is to accidentally &#8220;leak&#8221; memory and resources in an Android application.</p>
<p>In this tutorial, I&#8217;ll illustrate a memory leak with a simple dialog box example, and then show you how to fix it.</p>
<p>Let&#8217;s start by creating a simple Android application that displays a dialog in its onCreate event:</p>
<pre class="brush: java; title: ; notranslate">
package com.justinschultz.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class LeakedDialogActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setMessage(&quot;This dialog leaks!&quot;).setTitle(&quot;Leaky Dialog&quot;).setCancelable(false).setPositiveButton(&quot;Ok&quot;, new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which) {}
		});

		AlertDialog alert = builder.create();
		alert.show();
    }
}
</pre>
<p>If you&#8217;ve copied this correctly, the app should look like this:</p>
<div id="attachment_163" class="wp-caption aligncenter" style="width: 490px"><a href="http://publicstaticdroidmain.com/wp-content/uploads/2012/01/device-2012-01-10-195010.png"><img class="size-full wp-image-163" title="Leaky Android Dialog" src="http://publicstaticdroidmain.com/wp-content/uploads/2012/01/device-2012-01-10-195010.png" alt="" width="480" height="800" /></a><p class="wp-caption-text">Leaky Android Dialog</p></div>
<p>While this application is running, take a look in <a href="http://developer.android.com/guide/developing/tools/logcat.html" target="_new">LogCat</a>. (In Eclipse, LogCat can be displayed via Window -&gt; Show View -&gt; LogCat.)</p>
<p>While the dialog is displayed, rotate the device (or the emulator by pressing CTRL+F11).</p>
<p>You should see a lot of bright red error text in LogCat that looks something like this:</p>
<p><div id="attachment_173" class="wp-caption aligncenter" style="width: 610px"><a href="http://publicstaticdroidmain.com/wp-content/uploads/2012/01/Screenshot-at-2012-01-10-200010.png" target="_blank"><img class="size-large wp-image-173" title="LogCat Leaked Window Error" src="http://publicstaticdroidmain.com/wp-content/uploads/2012/01/Screenshot-at-2012-01-10-200010-1024x575.png" alt="" width="600" height="336" /></a><p class="wp-caption-text">LogCat Leaked Window Error</p></div><br />
Even though it looks like everything is working correctly on the device, LogCat displays this error message:</p>
<pre class="brush: java; title: ; wrap-lines: true; notranslate">
Activity com.justinschultz.android.LeakedDialogActivity has leaked
window com.android.internal.policy.impl.PhoneWindow$DecorView@40543ec0
that was originally added here
</pre>
<h1 style="padding-top: 16px">So&#8230; what happened?</h1>
<p>When the device rotates, the onPause and onStop Activity events are invoked. Then a new landscape Activity is created, and its onCreate method is invoked.</p>
<p>Since the dialog is displayed and has a reference to the Activity, it can&#8217;t be garbage collected, and subsequently is leaked. That is, <em>you&#8217;ve created an object that can&#8217;t be garbage collected</em>. </p>
<p>To fix this, let&#8217;s make the dialog an Activity member variable and override the onPause method to dismiss it:</p>
<pre class="brush: java; title: ; notranslate">
  AlertDialog _alert;

    @Override
    public void onPause() {
    	super.onPause();

    	if(_alert != null)
    		_alert.dismiss();
    }
</pre>
<p>This will dismiss the dialog each time the Activity is destroyed and recreated and the error will magically disappear from LogCat. </p>
<p>Here&#8217;s the <a href='http://publicstaticdroidmain.com/wp-content/uploads/2012/01/LeakedDialog.zip'>source</a> for this example. </p>
]]></content:encoded>
			<wfw:commentRss>http://publicstaticdroidmain.com/2012/01/avoiding-android-memory-leaks-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Application Crash Reporting for Android</title>
		<link>http://publicstaticdroidmain.com/2012/01/application-crash-reporting-for-android/</link>
		<comments>http://publicstaticdroidmain.com/2012/01/application-crash-reporting-for-android/#comments</comments>
		<pubDate>Sat, 07 Jan 2012 05:00:10 +0000</pubDate>
		<dc:creator>Justin Schultz</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[acra]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[crash]]></category>
		<category><![CDATA[crash reporting]]></category>
		<category><![CDATA[reporting]]></category>

		<guid isPermaLink="false">http://publicstaticdroidmain.com/?p=140</guid>
		<description><![CDATA[ACRA is a Google library that allows your Android applications to automatically log unhandled exceptions or application crashes to a Google document and/or display Toast notifications. To use it, you&#8217;ll need to create an Application class at the root of your Android application. This class will be used to log any uncaught or unhandled Exceptions, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://code.google.com/p/acra/" target="_new">ACRA</a> is a Google library that allows your Android applications to automatically log unhandled exceptions or application crashes to a Google document and/or display Toast notifications. </p>
<p>To use it, you&#8217;ll need to create an Application class at the root of your Android application. This class will be used to log any uncaught or unhandled Exceptions, and will contain ACRA&#8217;s configuration. </p>
<p>Here&#8217;s a sample Application class:</p>
<pre class="brush: java; title: ; notranslate">
package com.justinschultz.android;

import java.lang.Thread.UncaughtExceptionHandler;

import org.acra.ACRA;
import org.acra.annotation.ReportsCrashes;

import android.app.Application;
import android.util.Log;

@ReportsCrashes(formKey = &quot;YOURFORMKEY&quot;)
public class MyApplication extends Application implements UncaughtExceptionHandler
{
	private static MyApplication instance;

	public MyApplication()
	{
		instance = this;
		Thread.setDefaultUncaughtExceptionHandler(this);
	}

	@Override
	public void onCreate()
	{
		ACRA.init(this);
		super.onCreate();
	}

	public static MyApplication getInstance()
	{
		return instance;
	}

	@Override
	public void uncaughtException(Thread thread, Throwable ex)
	{
		Log.i(&quot;Error: &quot;, (ex == null || ex.getMessage() == null) ? &quot;Unknown&quot; : ex.getMessage());
	}

}
</pre>
<p><a href="http://code.google.com/p/acra/wiki/BasicSetup#Setting-up_your_project" target="_new">Here&#8217;s</a> how to generate a form key and complete the configuration.</p>
]]></content:encoded>
			<wfw:commentRss>http://publicstaticdroidmain.com/2012/01/application-crash-reporting-for-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a Custom Multi-Line ListView in Android</title>
		<link>http://publicstaticdroidmain.com/2011/12/building-a-custom-multi-line-listview-in-android/</link>
		<comments>http://publicstaticdroidmain.com/2011/12/building-a-custom-multi-line-listview-in-android/#comments</comments>
		<pubDate>Wed, 28 Dec 2011 17:30:31 +0000</pubDate>
		<dc:creator>Justin Schultz</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[adapter]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[array adapter]]></category>
		<category><![CDATA[base adapter]]></category>
		<category><![CDATA[custom]]></category>
		<category><![CDATA[listview]]></category>
		<category><![CDATA[multi line]]></category>
		<category><![CDATA[ui]]></category>

		<guid isPermaLink="false">http://publicstaticdroidmain.com/?p=96</guid>
		<description><![CDATA[This tutorial will walk you through building a custom multi-line ListView in Android. We&#8217;ll achieve this by extending BaseAdapter (or concrete ArrayAdapter) to turn a collection of objects into individual ListView items. First, let&#8217;s define the layout for our scrollable ListView by placing the following in main.xml: Next, let&#8217;s define a layout for individual ListView [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will walk you through building a custom multi-line ListView in Android. </p>
<p>We&#8217;ll achieve this by extending <a href="http://developer.android.com/reference/android/widget/BaseAdapter.html" target="_new">BaseAdapter</a> (or concrete <a href="http://developer.android.com/reference/android/widget/ArrayAdapter.html" target="_new">ArrayAdapter</a>) to turn a collection of objects into individual ListView items. </p>
<p>First, let&#8217;s define the layout for our scrollable ListView by placing the following in main.xml:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;fill_parent&quot;
    android:layout_height=&quot;fill_parent&quot;
    android:orientation=&quot;vertical&quot; &gt;

    &lt;TextView
        android:layout_width=&quot;fill_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:gravity=&quot;center_vertical|center_horizontal&quot;
        android:text=&quot;Custom ListView Example&quot; /&gt;

    &lt;ListView
        android:id=&quot;@+id/srListView&quot;
        android:layout_width=&quot;fill_parent&quot;
        android:layout_height=&quot;fill_parent&quot; /&gt;

&lt;/LinearLayout&gt;
</pre>
<p>Next, let&#8217;s define a layout for individual ListView items. Paste this layout code in a new layout file called custom_row_view.xml:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;fill_parent&quot;
    android:layout_height=&quot;fill_parent&quot;
    android:orientation=&quot;vertical&quot;
    android:paddingBottom=&quot;10dip&quot;
    android:paddingLeft=&quot;10dip&quot;
    android:paddingTop=&quot;10dip&quot; &gt;

    &lt;TextView
        android:id=&quot;@+id/name&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:textColor=&quot;#C20000&quot;
        android:textSize=&quot;14sp&quot;
        android:textStyle=&quot;bold&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/cityState&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/phone&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot; /&gt;

&lt;/LinearLayout&gt;
</pre>
<p>Next, let&#8217;s create a POJO called SearchResults.java to represent contact search results. Paste this boilerplate code into it:</p>
<pre class="brush: java; title: ; notranslate">
package com.publicstaticdroidmain.android;

public class SearchResults {
	 private String name = &quot;&quot;;
	 private String cityState = &quot;&quot;;
	 private String phone = &quot;&quot;;

	 public void setName(String name) {
	  this.name = name;
	 }

	 public String getName() {
	  return name;
	 }

	 public void setCityState(String cityState) {
	  this.cityState = cityState;
	 }

	 public String getCityState() {
	  return cityState;
	 }
	 public void setPhone(String phone) {
	  this.phone = phone;
	 }

	 public String getPhone() {
	  return phone;
	 }
}
</pre>
<p>Now we need to build an <a href="http://developer.android.com/reference/android/widget/BaseAdapter.html" target="_new">Adapter</a>. The Adapter will turn our collection of SearchResults into individual ListView items. Create a file called MyCustomBaseAdapter.java with these contents:</p>
<pre class="brush: java; title: ; notranslate">
package com.publicstaticdroidmain.android;

import java.util.ArrayList;

import com.publicstaticdroidmain.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyCustomBaseAdapter extends BaseAdapter {
	private static ArrayList&lt;SearchResults&gt; searchArrayList;

	private LayoutInflater mInflater;

	public MyCustomBaseAdapter(Context context, ArrayList&lt;SearchResults&gt; results) {
		searchArrayList = results;
		mInflater = LayoutInflater.from(context);
	}

	public int getCount() {
		return searchArrayList.size();
	}

	public Object getItem(int position) {
		return searchArrayList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.custom_row_view, null);
			holder = new ViewHolder();
			holder.txtName = (TextView) convertView.findViewById(R.id.name);
			holder.txtCityState = (TextView) convertView
					.findViewById(R.id.cityState);
			holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.txtName.setText(searchArrayList.get(position).getName());
		holder.txtCityState.setText(searchArrayList.get(position)
				.getCityState());
		holder.txtPhone.setText(searchArrayList.get(position).getPhone());

		return convertView;
	}

	static class ViewHolder {
		TextView txtName;
		TextView txtCityState;
		TextView txtPhone;
	}
}
</pre>
<p>And finally, let&#8217;s update our main Activity class (mine is called CustomListViewActivity) to tie everything together:</p>
<pre class="brush: java; title: ; notranslate">
package com.publicstaticdroidmain.android;

import java.util.ArrayList;

import com.publicstaticdroidmain.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class CustomListViewActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ArrayList&lt;SearchResults&gt; searchResults = GetSearchResults();

        final ListView lv = (ListView) findViewById(R.id.srListView);
        lv.setAdapter(new MyCustomBaseAdapter(this, searchResults));

        lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView&lt;?&gt; a, View v, int position, long id) {
				Object o = lv.getItemAtPosition(position);
		        SearchResults fullObject = (SearchResults)o;
		        Toast.makeText(CustomListViewActivity.this, &quot;You have chosen: &quot; + &quot; &quot; + fullObject.getName(), Toast.LENGTH_LONG).show();
			}
        });
    }

    private ArrayList&lt;SearchResults&gt; GetSearchResults(){
     ArrayList&lt;SearchResults&gt; results = new ArrayList&lt;SearchResults&gt;();

     SearchResults sr = new SearchResults();
     sr.setName(&quot;Justin Schultz&quot;);
     sr.setCityState(&quot;San Francisco, CA&quot;);
     sr.setPhone(&quot;415-555-1234&quot;);
     results.add(sr);

     sr = new SearchResults();
     sr.setName(&quot;Jane Doe&quot;);
     sr.setCityState(&quot;Las Vegas, NV&quot;);
     sr.setPhone(&quot;702-555-1234&quot;);
     results.add(sr);

     sr = new SearchResults();
     sr.setName(&quot;Lauren Sherman&quot;);
     sr.setCityState(&quot;San Francisco, CA&quot;);
     sr.setPhone(&quot;415-555-1234&quot;);
     results.add(sr);

     sr = new SearchResults();
     sr.setName(&quot;Fred Jones&quot;);
     sr.setCityState(&quot;Minneapolis, MN&quot;);
     sr.setPhone(&quot;612-555-8214&quot;);
     results.add(sr);

     sr = new SearchResults();
     sr.setName(&quot;Bill Withers&quot;);
     sr.setCityState(&quot;Los Angeles, CA&quot;);
     sr.setPhone(&quot;424-555-8214&quot;);
     results.add(sr);

     sr = new SearchResults();
     sr.setName(&quot;Donald Fagen&quot;);
     sr.setCityState(&quot;Los Angeles, CA&quot;);
     sr.setPhone(&quot;424-555-1234&quot;);
     results.add(sr);

     sr = new SearchResults();
     sr.setName(&quot;Steve Rude&quot;);
     sr.setCityState(&quot;Oakland, CA&quot;);
     sr.setPhone(&quot;515-555-2222&quot;);
     results.add(sr);

     sr = new SearchResults();
     sr.setName(&quot;Roland Bloom&quot;);
     sr.setCityState(&quot;Chelmsford, MA&quot;);
     sr.setPhone(&quot;978-555-1111&quot;);
     results.add(sr);

     sr = new SearchResults();
     sr.setName(&quot;Sandy Baguskas&quot;);
     sr.setCityState(&quot;Chelmsford, MA&quot;);
     sr.setPhone(&quot;978-555-2222&quot;);
     results.add(sr);

     sr = new SearchResults();
     sr.setName(&quot;Scott Taylor&quot;);
     sr.setCityState(&quot;Austin, TX&quot;);
     sr.setPhone(&quot;512-555-2222&quot;);
     results.add(sr);

     return results;
    }
}
</pre>
<p>And that&#8217;s it. If you&#8217;ve done everything correctly, you should wind up with this:<br />
<div id="attachment_127" class="wp-caption aligncenter" style="width: 490px"><a href="http://publicstaticdroidmain.com/wp-content/uploads/2011/12/device-2011-12-26-160505.png"><img src="http://publicstaticdroidmain.com/wp-content/uploads/2011/12/device-2011-12-26-160505.png" alt="Android Custom Multi-Line ListView Screenshot" title="Android Custom Multi-Line ListView Screenshot" width="480" height="800" class="size-full wp-image-127" /></a><p class="wp-caption-text">Android Custom Multi-Line ListView Screenshot</p></div></p>
<p>Here&#8217;s the zipped <a href='http://publicstaticdroidmain.com/wp-content/uploads/2011/12/CustomListView.zip'>source code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://publicstaticdroidmain.com/2011/12/building-a-custom-multi-line-listview-in-android/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Android USB Debugging on Ubuntu Linux 11.10</title>
		<link>http://publicstaticdroidmain.com/2011/12/android-usb-debugging-on-ubuntu-linux-11-10/</link>
		<comments>http://publicstaticdroidmain.com/2011/12/android-usb-debugging-on-ubuntu-linux-11-10/#comments</comments>
		<pubDate>Mon, 26 Dec 2011 02:00:38 +0000</pubDate>
		<dc:creator>Justin Schultz</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[adb]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://publicstaticdroidmain.com/?p=73</guid>
		<description><![CDATA[I recently set up a new development machine with Ubuntu Linux 11.10, but ran into this problem trying to list connected Android devices with the adb command: To fix this, just run these commands from your SDK/platform-tools folder to restart the ADB server with root permissions: Now your devices should appear: Also, make sure to [...]]]></description>
			<content:encoded><![CDATA[<p>I recently set up a new development machine with Ubuntu Linux 11.10, but ran into this problem trying to list connected Android devices with the <a href="http://developer.android.com/guide/developing/tools/adb.html" target="_new">adb</a> command:</p>
<pre class="brush: plain; title: ; notranslate">
./adb devices
List of devices attached
???????????? no permissions
</pre>
<p>To fix this, just run these commands from your SDK/platform-tools folder to restart the ADB server with root permissions:</p>
<pre class="brush: plain; title: ; notranslate">
./adb kill-server
sudo ./adb start-server
</pre>
<p>Now your devices should appear:</p>
<pre class="brush: plain; title: ; notranslate">
./adb devices
List of devices attached
HT09PR201016	device
</pre>
<p>Also, make sure to enable USB debugging on your device in Settings -> Applications -> Development. </p>
]]></content:encoded>
			<wfw:commentRss>http://publicstaticdroidmain.com/2011/12/android-usb-debugging-on-ubuntu-linux-11-10/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

