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.

Bluetooth Service Connector

Bluetooth is still a relatively new technology when it comes to Windows. Although Windows 7 now has a reasonably decent bluetooth stack baked-in, it’s certainly not bug-free. It is not straightforward to control connection of bluetooth services, such as AudioSink, HeadSet, RemoteControl etc. And depending on how ‘dumb’ your bluetooth device is, your only option to get it connected to Windows in some cases (e.g. your device was last connected to a different host, such as your phone) is to completely uninstall and re-discover the device, as discussed on this thread. Weeeak.

I’ve written a small utility that may help with issue (it works for me, but I’ve only tested on my own bluetooth headphones – Sony DR-BT50). It works in conjunction with another utility I recently posted (that allows a program to set the default audio device), and provides control over connecting / disconnecting the available bluetooth services for all your paired bluetooth devices in Windows. It also provides a shortcut specifically for bluetooth audio devices which automates the process of getting a frustratingly silent pair of headphones / speaker to spring into life with a single click.

Download the utility here. I hope it works for you!

Setting default audio device in Windows

Since the audio revamp in Windows Vista, many things that were possible to do in code in the past (i.e. XP) are now difficult or impossible. One of those tasks that was easy in XP was setting the default audio device for playback. In XP this just required a registry change, but in Vista & Windows 7, Microsoft decided that developers should not have access to change the default audio device, so they locked down the registry and provided no documented API to achieve this. Their reasoning was that if two programs both wanted to set the default audio device, they would end up fighting each other for it, which of course is bad. But I believe this is a very short-sighted decision, since there are plenty of legitimate cases where a program needs to do this. These are discussed on this amazingly long-living thread (started in 2006 and still going).

The end result is that, no thanks to Microsoft, we finally have a way to do this, and I’ve created a console application in case others would like to control this as well:

Usage: SetDefaultAudioDevice.exe [deviceID] [role]
Where:
[deviceID] is a GUID including braces
[role] is either ‘console’, ‘multimedia’ or ‘communications’.

Example: SetDefaultAudioDevice {24dfc80a-680f-4748-8627-c340cb14f187} multimedia

Your audio device IDs can be found in the registry under HKLMSOFTWAREMicrosoftWindowsCurrentVersionMMDevicesAudioRender.

Feel free to download this utility here, source code is in my SVN repo.

Thank-you to EreTIk and Jonny Best and everyone else on the above forum post!