Android: using a TextView to show rich text in an AlertDialog

If you want to display a link or basic formatting in an AlertDialog on Android, you can do it by providing HTML.

The key parts you need are Html.fromHtml and TextView.setMovementMethod.

Make sure you pass the dialog’s context in to the constructor of the TextView, not the context of the current activity. Otherwise the colours in your TextView will be wrong and you may well end up with black text on a dark grey background.

AlertDialog dialog = new AlertDialog.Builder( activity )
    .setTitle( t( ) )
    .setPositiveButton( "Yes!" )
    .setNeutralButton( "Maybe?" )

TextView view = new TextView( dialog.getContext() );
view.setText( Html.fromHtml( "<b>foo</b> <a href='#'>bar</a>" ) );
view.setMovementMethod( LinkMovementMethod.getInstance() );
view.setPadding( 10, 10, 10, 10 );

dialog.setView( view );;

If you are on API level 11+, you can use AlertDialog.Builder’s getContext() method, so you don’t have to create the dialog until the end.

Assassination of individuals by the state

I wrote to my MP (via about the British government assassinating people they suspected of planning terrorist attacks. He replied saying that the government believes the action was legal, proportional and necessary. Part of this justification was based on the fact that circumstances in Syria make it difficult to disrupt attacks in other ways. He stated the people killed were recruiting ISIL members and planning specific attacks in Britain. He also stated that this action was unconnected to general military action against ISIL in Syria.

My reply is below. Feel free to re-use and adapt it if you want to send a similar letter.

Dear Philip Hammond,

Thank you for your reply to my letter about the assassination of Junaid Hussain and Reyaad Khan.

I have 4 questions:

1. When did the British government start assassinating individuals suspected of planning terrorist attacks?

2. What standard of evidence is required to assassinate someone?

3. How imminent must a suspected attack be to warrant assassination?

4. What oversight is there of each decision to perform an assassination?

I continue to believe this practice is in conflict with British values and international law. You mention the UN Charter: I assume you are referring to Article 51 of the UN Charter, which refers to “armed attack” against a country. I do not accept that an individual suspected of planning a terrorist attack falls under the definition of an “armed attack” which was surely a reference to warfare by state-like entities, rather than plans being made by individuals to commit acts well-covered by UK criminal law.

We celebrated the 800th anniversary of Magna Carta in your constituency this year. Our reasons for celebration included the limitation of the state’s power over the individual. When the state executes individuals without trial or even publication of evidence, is there any practical limit to its power?

Yours sincerely,

Andy Balaam

Difficult merges in Git – don’t panic!

A video in which I try to explain what merging and rebasing really are, to help you understand what is going on when Git presents you with scary-looking conflict messages. I also explain why you shouldn’t panic because it’s hard to lose your work, and how to get you work back if you really mess up:

Slides here: Difficult Merges in Git.

A commit represents the state of the world (and the history leading up to that state). A commit is not a diff.

Merging means making a new commit with two (or more) “parents” (previous commits) that represents the result of merging the changes from two different threads of development that happened separately. None of the already-committed commits are modified – you just get a new commit on top. History is more complicated, but true.

Rebasing means modifying the history of one thread of development so it looks like it happened after the other one. This involves modifying all the commits in that thread. There is no extra merge commit, so you lose the history of the merge that happened. History is simple, but it’s a lie, and if you messed up the rebasing process, you can’t get back to where you were (once your old commits have been garbage-collected).

ZX Spectrum BASIC Web Server

Finally, you can write your web sites in ZX Spectrum BASIC, using my new project, ZX Spectrum BASIC CGI server .

How it works

Here’s what happens when a request comes in:

  • Apache HTTPD receives the request, and runs the CGI script that does the magic.
  • The CGI script (written in Bash) generates some BASIC code that provides the HTTP meta-vars (e.g. PATH_INFO for the path of the request) as DATA statements and wraps some other boilerplate around the code of the .basic file asked for in the request, and writes out another .basic file which is ready to be executed.
  • Then it uses BAS2TAP (my mirror here) to create an audio tape file representing the program.
  • Next it launches the Fuse spectrum emulator (inside an Xvfb wrapper to make it think it has a display), telling it to load from the audio tape and run the program.
  • The program runs, and writes its HTTP responses to the Spectrum printer using LPRINT statements.
  • Fuse uses very basic OCR to understand what characters are printed, and writes them into a text file.
  • The CGI script monitors the output file, waiting for a line containing an end marker (“ENDSPECTRUMBASIC”). When it finds it, it kills the emulator (since it can’t made to auto-exit, I think).
  • The CGI script sends the contents of the output file (minus the end marker) back to Apache, which returns it as a response.



Originally designed to demonstrate how Docker can help isolate other services from “interesting” dependencies, this became a bit of a labour of love.

I’ll never stop being grateful to the designers of the ZX Spectrum, or the authors of “Further Programming for the ZX Spectrum”.

Letter to my MP on UK government assassination of British Citizens in Syria

Below is a copy of the letter I sent to my MP this morning (via Feel free to re-use and adapt it if you want to send a similar letter.

Update: follow-up letter.

Dear Philip Hammond,

I was extremely concerned to hear of the assassination of two British citizens by the UK armed forces [1].

The Prime Minister and Defence Secretary justified the attack on the basis of self-defence, which I believe is a reference to Article 51 of the UN Charter [2]. I consider the use of this article to cover action against individuals who are suspected of planning domestic terrorism to be wholly inappropriate. I support those who are calling for a legal challenge against this action.

Please pass on my concerns to the Prime Minister and Defence Secretary, and use your considerable influence in this area to make government policy focus on the use of criminal law to prevent domestic terrorism.

Please work to ensure the government abides by the decision of the UK parliament not to participate in military action in Syria, and thus not to treat this situation as a war in which the UK is participating. As you said on 2nd September 2013, “The House of Commons has ruled out military participation in any such response” (referring to our response to the use of chemical weapons).

Yours sincerely,

Andy Balaam