Android header preferences on small screen / phone

Short version: If your android preference headers are displaying on a small screen, but not opening the associated preference fragment when clicked, make sure you don’t have launchMode attribute set on your preferences activity in your AndroidManifest.xml file.

Long version: The concept of two-pane preferences of course applies to large screen (tablet) devices, where the left hand-side is a list of ‘headers’ and the right hand side is an associated preference fragment. Google’s built-in template for ‘SettingsActivity’ shows how this can be implemented such that when the activity is displayed on a small screen (e.g. phone) the preference headers are not displayed and instead all preferences are shown on the one screen, separated by categories. To do this they force you to use a deprecated API, which is pretty ugly. Additionally, this only makes sense when you don’t have many preferences. Once you have a large number of preferences, it may make sense to use headers on a small screen, which was the case for me, however it wasn’t working. The headers were displaying fine, but whenever I tapped one, nothing would happen, the onCreate() method of the associated fragment would not be called. By trial and error I finally found that I had

android:launchMode=”singleTask”

in my manifest file. As soon as I removed that attribute, preference headers worked on a small screen fine.

“No Permissions” when listing android devices

When plugging in a new Android device to your computer for debugging, you might see this when executing adb devices:

List of devices attached
???????????? no permissions
13716365315 device

This shows that 2 devices are attached, but only 1 of the two are working / recognised. To solve this you can try killing and restarting the adb server. To do this on linux, change to your android-sdk-linux/platform-tools directory in a terminal shell, then type:

sudo su
adb kill-server
adb start-server

Now try adb devices again and hopefully you see your device ok.

Find was stopped in progress in VS

There has been a bug in Visual Studio since the 90s where you are searching in files, but the find operation stops very quickly after starting (and hence gives no results). The output window shows ‘Find was stopped in progress’. This is caused by pressing Ctrl+ScrollLock (usually done inadvertently when Ctrl+Break). The solution is to press Ctrl+ScrollLock again, then you should be able to ‘Find in Files’ again.

Setting a startup project in Eclipse

Coming from Visual Studio, it is not immediately obvious how to set a specific project as a ‘startup’ or launch project (i.e. the project that will be run by default). In VS, you just right click the project and choose ‘Set As Startup Project’. Here’s how to do it in eclipse:

  1. Go to Window -> Preferences -> Run / Debug -> Launching
  2. Set the ‘Launch Operation’ as appropriate.

Magic command to install Asp.Net 4 for IIS 7.5

After creating an ASP.NET web application in Visual Studio, to host it in a freshly installed IIS 7.5, you need to do two things that are not immediately obvious:

  1. Create a new app pool or change the default app pool to target .NET 4 (see IIS->Application Pools).
  2. Run this from the command line:
    %windir%Microsoft.NETFrameworkv4.0.30319aspnet_regiis.exe -i

MSDN has more info on aspnet_regiis command line options here.
Just putting this on my blog so I can remember this magic command line.

On-screen performance monitoring for acceptance tests

If you record videos of your automated acceptance tests (we are using CamStudio), an excellent addition is to install the free Prism HUD utility. This will give you on-screen (but transparent to mouse actions) CPU / disk / network and memory statistics which can be very useful when reviewing these videos. It appears to be the only application that allows reporting of metrics of any process that exceeds a certain (configurable) delta, or is over a certain (configurable) threshold. This means you get much more useful information than just a summary metric (e.g. total CPU utilization), but you don’t clutter your screen with the metrics for all processes, just those ‘that matter’ at any point in time, automatically!!

Go get it here.

Random Programming Quote

I’ve hand-collected > 300 great programming quotes and stuffed them in a tiny console application, which, when run, will randomly print one to the console. Use it as a footer for your nightly build email, or anywhere you have some drabby generated document / email.

Also can be used if you are just bored on a Friday afternoon. 😉

Download it here.

Instantiating a custom view in Android

Creating a custom view/control in Android is fairly simple – here are the steps I used:

  1. Create a new class which extends from the ‘View’ abstract base class. E.g.
    public class MyCustomControl extends View
  2. Provide constructor overloads for ALL of the following prototypes, where initView() is a method in which you perform initialization of things such as Paint objects:
    public MyCustomControl(Context context)
    	{
    		super(context);
    		initView();
    	}
    	
    	public MyCustomControl(Context context, AttributeSet attrs)
    	{
    		super(context, attrs);
    		initView();
    	}
    
    	public MyCustomControl(Context context, AttributeSet attrs, int defStyle)
    	{
    		super(context, attrs, defStyle);
    		initView();
    	}
    
  3. Override onMeasure() in order to scale your control to the parent drawing area, such as shown in this good example.
  4. Do your canvas drawing operations in onDraw on the supplied Canvas object.
  5. Instantiate your custom view in a layout xml file (e.g. main.xml), for example:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    	<com.android.samples.MyCustomControl
    		android:id="@+id/myCustomControl"
        	android:layout_width="fill_parent" 
        	android:layout_height="fill_parent" 
        />
    </LinearLayout>

I found that if I didn’t add the 2nd and 3rd constructors, I got an exception when calling setContentView(R.layout.main) from onCreate() of the main Activity. You can instead construct an instance of the view manually in onCreate() of the main Activity, and then call setContentView(yourCustomViewInstance) if you don’t want to use XML layout.

Fastest way to delete a directory tree in Windows

Our nightly build was taking > 30 minutes to delete our source tree before getting latest (Windows is not quick to delete a large number of small files), which was comparable to the time taken to actually compile the source. There are stackoverflow and superuser articles that discuss the fastest way to do deletes, however there’s no magic solution. If you find you are regularly needing to delete large amounts of files (e.g. as part of the nightly build process), your best option is to create a new partition on your HDD (or install a new HDD) and store all the files you will be deleting on there. Then you can erase them all in about 2 seconds by doing a quick format of the drive! The only trick is how to programatically format a drive, since there is no (documented) API for this in Windows. There’s Win32_Volume.Format (WMI) but it’s only available in server OSs, and then there’s SHFormatDrive, which shows a dialog.

However, provided your partition (in this case Z:) has no label, you can do this:

echo Y | format Z: /FS:NTFS /X /Q
An inferior alternative to this is to use a VBScript that uses SendKeys like this:

set WshShell = CreateObject("WScript.Shell")
wshShell.run "c:windowssystem32format.com Z: /FS:NTFS /V:QuickWipeDrive /X /Q"
wscript.sleep 1000
wshshell.sendkeys "QuickWipeDrive" & "{ENTER}"
wscript.sleep 1000
wshshell.sendkeys "Y" & "{ENTER}"
wscript.sleep 5000

The downside of this script is that it will only work when the computer is not locked since SendKeys requires a console session that is logged in an active.