<?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>Mike Plate &#187; Android</title>
	<atom:link href="http://www.mikeplate.com/category/android/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mikeplate.com</link>
	<description>Freelance web and mobile developer</description>
	<lastBuildDate>Tue, 17 Aug 2010 20:12:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Show a context menu for long-clicks in an Android ListView</title>
		<link>http://www.mikeplate.com/show-a-context-menu-for-long-clicks-in-an-android-listview/</link>
		<comments>http://www.mikeplate.com/show-a-context-menu-for-long-clicks-in-an-android-listview/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 11:26:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[ContextMenu]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[ListView]]></category>
		<category><![CDATA[ListViewDemo]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=246</guid>
		<description><![CDATA[Coming from a Windows and .NET background, I had some trouble understanding how to interact with the ListView control and context menu creation in Android. Context menus are supposed to be shown on your mobile device when you touch/click the screen and hold on for a longer time. So here is how to determine which [...]]]></description>
			<content:encoded><![CDATA[<p>Coming from a Windows and .NET background, I had some trouble understanding how to interact with the ListView control and context menu creation in Android. Context menus are supposed to be shown on your mobile device when you touch/click the screen and hold on for a longer time. So here is how to determine which item is long-clicked and how to show a context menu for it.<span id="more-246"></span></p>
<p>Source code for this blog post is available as a complete Eclipse project at<a title="Link to source code on Github" href="http://github.com/mikeplate/ListViewDemo"> http://github.com/mikeplate/ListViewDemo</a> (zip download link in upper right corner).</p>
<h2>An Activity with an expanding ListView and a docked TextView</h2>
<p>If you have an activity that will only contain a single ListView control, you can derive your activity from the ListActivity instead of Activity. However, I think I might like to show some extra info below my ListView so I chose to have a separate ListView object. My activity layout looks like this:</p>
<pre class="brush:xml;gutter:false">&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  &gt;
  &lt;ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="0px"
    android:layout_weight="1"
    /&gt;
  &lt;TextView
    android:id="@+id/footer"
    android:layout_width="fill_parent"
    android:layout_height="60dip"
    android:text="@string/footer"
    android:padding="4dip"
    android:background="#FF666666"
    /&gt;
&lt;/LinearLayout&gt;</pre>
<p>And I need the layout for items in the ListView (listitem.xml):</p>
<pre class="brush:xml;gutter:false">&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:textSize="24dip"
  android:padding="8dip"
  /&gt;</pre>
<p>Note a nice trick that I&#8217;ve used to get the TextView to &#8220;dock&#8221; at the bottom with a definied height, and have the ListView automatically fill out the rest of the height. This kind of thinking is important since Android devices can have different resolutions. The trick is to set the layout_height to zero pixels and the layout_weight to one (default is zero). Not sure about the logic behind that, but it works!</p>
<p><a href="http://www.mikeplate.com/wp-content/uploads/2010/01/ListViewDemo01.png"><img class="alignnone size-full wp-image-260" title="ListViewDemo01" src="http://www.mikeplate.com/wp-content/uploads/2010/01/ListViewDemo01.png" alt="Screen capture of a selected=" height="480" /></a></p>
<p>In order to have something to put into my ListView, I created a few country names in a string array as a resource and I sort that array before adding it to the ListView with the ArrayAdapter object. (Check out source code link above for this content.)</p>
<pre class="brush:java">public class ListViewDemoActivity extends Activity {
  private String[] Countries;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Countries = getResources().getStringArray(R.array.countries);
    Arrays.sort(Countries);

    ListView list = (ListView)findViewById(R.id.list);
    ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, R.layout.listitem, Countries);
    list.setAdapter(adapter);
    registerForContextMenu(list);
  }
}</pre>
<h2>Creating a ContextMenu in Android</h2>
<p>When the user long-clicks, the event onCreateContextMenu is fired for the control that the user is clicking. For me, that is the ListView control. But since I don&#8217;t want to write a custom ListView-derived class, I want to catch that event in my activity. There does not seem to be any bubbling going on. Events fired in a child control does not bubble up to the parent if they are unhandled.</p>
<p>But obviously, the api designers have thought of this since there is a special method for this situation. Call the registerForContextMenu in your activity for this! This will actually make sure your overridden methods for both onCreateContextMenu and onContextItemSelected is called for the ListView-events as we&#8217;ll see soon.</p>
<p>Next, we&#8217;ll provide the implementation of onCreateContextMenu. Here I want to ensure that the event comes from the ListView and if so, I want to determine on which item in the ListView the user long-clicked.</p>
<pre class="brush:java">@Override
public void onCreateContextMenu(ContextMenu menu, View v,
    ContextMenuInfo menuInfo) {
  if (v.getId()==R.id.list) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
    menu.setHeaderTitle(Countries[info.position]);
    String[] menuItems = getResources().getStringArray(R.array.menu);
    for (int i = 0; i&lt;menuItems.length; i++) {
      menu.add(Menu.NONE, i, i, menuItems[i]);
    }
  }
}</pre>
<p>As you can see, the argument of type ContextMenuInfo can actually change depending on what type of control is sending the event. For ListViews, the class you need to type cast into is AdapterView.AdapterContextMenuInfo. From there I used the position, which in my case corresponds to the index into the string-array. From the array I retrieve the string for that particular item and use as title for the menu. Then you can of course add all the menu commands you like. For the demo, I defined another string array as a resource with the commands I want to add.</p>
<p>When creating the menu items with the add-call, I specify that I don&#8217;t want any grouping of the items (Menu.NONE) and that the order and id of the item is the same (i). The last argument to add is the text to display for the item.</p>
<p><a href="http://www.mikeplate.com/wp-content/uploads/2010/01/ListViewDemo02.png"><img class="alignnone size-full wp-image-262" title="ListViewDemo02" src="http://www.mikeplate.com/wp-content/uploads/2010/01/ListViewDemo02.png" alt="Screen capture of the context menu with commands" width="320" height="480" /></a></p>
<h2>Responding to selected MenuItem</h2>
<p>If the user dismisses the context menu (for instance, by back button) you don&#8217;t need to do anything. But for catching the actual selection of one of the items, you need to override onContextItemSelected.</p>
<pre class="brush:java">@Override
public boolean onContextItemSelected(MenuItem item) {
  AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
  int menuItemIndex = item.getItemId();
  String[] menuItems = getResources().getStringArray(R.array.menu);
  String menuItemName = menuItems[menuItemIndex];
  String listItemName = Countries[info.position];

  TextView text = (TextView)findViewById(R.id.footer);
  text.setText(String.format("Selected %s for item %s", menuItemName, listItemName));
  return true;
}</pre>
<p>The MenuItem argument holds all information that you need. The ContextMenuInfo object that got sent to onCreateContextMenu is still there and still needs type casting. Or I guess you could have saved that info in the activity between the calls, but I didn&#8217;t.</p>
<p>The id of the menu item selected is the same as the index into the string array of menu item texts for me. Instead of just outputting the menu command name and the list item text in a TextView, you would most likely have a big switch statement on menuItemIndex.</p>
<p>This was my first blog post and code demo for the Android platform. I hope it won&#8217;t be the last! The goal is to build upon this demo and/or other demos in my investigations of the Android platform. Please let me know in the comments if you have even better methods or code patterns that solves problems like this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/show-a-context-menu-for-long-clicks-in-an-android-listview/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Getting ready for Android development</title>
		<link>http://www.mikeplate.com/getting-ready-for-android-development/</link>
		<comments>http://www.mikeplate.com/getting-ready-for-android-development/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 20:44:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Android]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=44</guid>
		<description><![CDATA[Here are the steps I took to get my Android development environment set up on my Windows 7 64-bit box. Download and install Java Development Kit SE: JDK 6 Update 16. File name I ended up downloading was jdk-6u16-windows-x64. Download and unpack Eclipse IDE for Java Developers. File name I ended up downloading was eclipse-SDK-3.5.1-win32-x86_64.zip. [...]]]></description>
			<content:encoded><![CDATA[<p>Here are the steps I took to get my Android development environment set up on my Windows 7 64-bit box.<span id="more-44"></span></p>
<ul>
<li>Download and install <a href="http://java.sun.com/javase/downloads/index.jsp">Java Development Kit SE: JDK 6 Update 16</a>.<br />
File name I ended up downloading was jdk-6u16-windows-x64.</li>
<li>Download and unpack <a href="http://download.eclipse.org/eclipse/downloads/">Eclipse IDE for Java Developers</a>.<br />
File name I ended up downloading was eclipse-SDK-3.5.1-win32-x86_64.zip.</li>
<li>Download and unpack <a href="http://developer.android.com/sdk/index.html">Android SDK</a>.<br />
File name I ended up downloading was android-sdk_r3-windows.zip.</li>
<li>Changed android.bat inside Android SDK according to <a href="http://code.google.com/p/android/issues/detail?id=3917">this tip</a>.</li>
<li>Start a command prompt as administrator and run the following command from the tools folder of the Android SDK:<br />
android update sdk</li>
<li>Fixed error &#8220;Failed to fetch URL https://dl-ssl.google.com/android/repository/repository.xml, reason: HTTPS SSL error. You might want to force download through HTTP in the settings.&#8221; by doing as it says and then clicking Update all button under Installed packages. Not sure why this was needed because the https link loaded fine in my browser.</li>
<li>Select &#8220;SDK Platform Android 1.5&#8243;, Accept and then Install Accepted. Actually I ended up downloading all platforms (not sure why, but I have room so what the heck). This means 1.1, 1.5, 1.6, 2.0, documentation</li>
<li>Start Eclipse and accept the default workspace location (or change &#8211; it is just the place to put your projects and files). Choose Help, Install new software. Input the following link and click Add button.
<p>https://dl-ssl.google.com/android/eclipse/</p>
<p>Type a name for the link/source and ok. Check &#8220;Developer Tools&#8221; once it appears in the list and click Next, Next, I Accept and Finish. I got a warning about unsigned content, but accepted with OK. Also, a Yes for restarting Eclipse.</li>
<li>Choose Window, Preferences inside Eclipse. Under Android, Browse for the location where you unpacked the Android SDK. I clicked Apply, but it seemed to take a while and maybe another click and/or another Browse attempt before the list filled up with my targets &#8220;Android 1.1&#8243;, &#8220;Android 1.5&#8243; etc. OK.</li>
<li>Choose Window, Android SDK and AVD Manager inside Eclipse. Click New under Virtual Devices. Type a name for your phone emulator and select your target platform, Android 1.5 for me. Type a size for your emulated SD card, I chose 200 MiB. I did not create any additional hardware abstractions (yet, will probably come back later). Then Create AVD and close the dialog box.</li>
<li>Choose File, New, Project inside Eclipse. Select Android Project and Next. Type a name for your project and select your target platform. I&#8217;m going for Android 1.5 which is on my HTC Hero at the time of this writing. Also input values for the following fields, for example:<br />
Application name: My App<br />
Package name: com.mycompany.myapp<br />
Create Activity: MainActivity<br />
And complete with a click on Finish. Now, you may think nothing has happed, but it has. Just click icon on right which saids &#8220;Workbench&#8221; when you hover on it. There you have your project and source files to work with!</li>
<li>Click Debug button in toolbar in order to test the application in the emulator. Select Android Application and off you go. Even on my Intel Core i7 processor it takes some time to get the app running inside the emulator, about 1 minute! Note however, that if you keep the emulator running, it only takes about 5 seconds to launch a new version of my application from within Eclipse.</li>
</ul>
<p>That&#8217;s it. Now off for some educational reading in how to write Android applications. I hope to get back on that matter.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/getting-ready-for-android-development/feed/</wfw:commentRss>
		<slash:comments>41</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)

Served from: www.mikeplate.com @ 2010-09-07 11:30:09 -->