Scala, Java, Unix, MacOS tutorials (page 333)

I began an experiment with the Amazon KDP Select free promotion program today. I just released my new eBook, How I Sold My Business: A Personal Diary, on Monday of this past week, and when I learned about the KDP Select program, I thought I'd give it a try.

Drupal FAQ: How do I remove the H1 title tag from the front page of a Drupal website?

I just ran into this problem on my new website, How I Sold My Business: A Personal Diary. Whenever you create a Drupal page you have to give it a title, but for the front page of a website that doesn't really make sense.

There are at least two ways to do this:

Drupal content type FAQ: How do I modify the node title for the "create" form when I add a new content type?

For my How I Sold My Business website, I added a new, custom content type named "Charity". This allows people who have bought my book to suggest a charity. The problem with this is that I want the Charity node title (also referred to as a "page title") to say "Suggest a Charity", not the default "Create Charity".

Drupal form FAQ: How do I redirect a user when they submit a form, and I'm writing a hook_form_alter function to alter that form?

In Drupal 6 you set the "$form['#redirect']" property in your hook_form_alter function when you alter the form, like this:

$form['#redirect'] = 'thank-you';

That syntax tells Drupal to redirect the user to the "/thank-you" URI on your website.

If you're ever creating a Drupal form using the CCK module and need to show a list of states (the United States) in a combo box (also known as a "select list" or "drop down" field), you'll want to have that list of states in the right format.

Fortunately (for you) I just ran into this problem, and created two different versions of CCK form state fields. This first one displays the full name of the state, and stores the two-digit state code in the database:

This is a little different from my usual posts, but while working on the cover design for my new book, How I Sold My Business: A Personal Diary, I wanted to look at a large number of fonts, so I could see which font I liked the most. This was at least the third time this need had come up, so I decided to take a little time to try to create a Mac font tester.

March 5, 2012: I just released my first book, an eBook named “How I Sold My Business: A Personal Diary.” As the name implies, the book tells the story of how I sold my small computer programming company, in a diary format.

The book is currently available for the Kindle through Amazon.com. If all goes well it will soon be available at Barnes & Noble and the Apple iBookstore.

Scala/XPath FAQ: How do I get the first element of an array in an XML document using Scala and XPath?

I ran into the problem of needing to get the first array element from an XML document using Scala and XPath recently, and in short, I ended up writing some Scala/XPath code that looked like this:

I upgraded one of my computers to Mac OS X Lion recently, and because I'm working with images a lot right now, I quickly ran into a weird quirk: The Preview app on Lion reopens all your previous documents whenever you start it.

If you haven't seen it before, here's how this Lion Preview history feature works:

Android error logging FAQ: How do I log error messages in Android?

To log Android errors, just use the Log class, as shown in this example:

If you want to enable the Android activity icon (app icon) in your application's ActionBar so users can tap the icon to go to the home screen, here are my notes on how to do this.

Enabling the ActionBar activity icon to handle the home button tap is a simple, three step process:

Step 1

In Step 1, add a line like this to your app's AndroidManifest.xml file:

Android FAQ: How do I start a new Activity in Android?

Typically I start a new Activity in Android like this:

Android ListActivity FAQ: How do I create a class that extends the Android ListActivity class?

The easiest way to show a ListActivity example is to share some source code with the non-essential code stripped out. To that end, here's an example ListActivity class:

I just ran across a nice example of a Java static initializer block example in an Android book, and thought it might be helpful to share that example here.

First, here's the static initialization code:

I've been using Mac OS X since 2005, and the things I've always loved about it were:

  • It looked great.
  • It was simple to use.
  • It didn't crash.
  • It had the power of Unix under the hood.

Flash forward to Mac OS X Lion, and the simplicity and quality are gone. Beyond that, it doesn't look as good as previous versions of OS X (but at least in that case, you can argue that beauty is in the eye of the beholder).

Android Button FAQ: How do I add a listener to a Button in Android?

To add a click (tap) listener to a button in Android, just get a reference to the button (typically using findViewById), then call the setOnClickListener method of the button, like this:

Android Toast FAQ: How do I create a Toast message in Android? (Or, Can you share some Android Toast message syntax examples?)

Here's one example of the Android Toast syntax:

Toast.makeText(ProjectActivity.this, "Your message here" , Toast.LENGTH_SHORT).show();

and here's a second example, this time referring to the Android application context as the first method parameter:

Android error FAQ: Help, I'm developing an Android app, and I'm getting a "Unfortunately your Android app has stopped error message"; how do I fix it?

If you're getting an Android error message that looks like this:

"Unfortunately your Android app has stopped error message"

It's probably because you forgot to put a new Activity in the AndroidManifest.xml file (or at least that's been the #1 cause of this error message in my experience).

In this short article, I’ll demonstrate the typical workflow for using a Git topic branch. If you’ve never heard of a topic branch, here’s a description from the excellent book, Pro Git:

“A topic branch is a short-lived branch that you create and use for a single particular feature or related work.

This is something you’ve likely never done with a VCS before because it’s generally too expensive to create and merge branches. But in Git it’s common to create, work on, merge, and delete branches several times a day.”

The basic Git topic branch workflow pattern looks like this:

create a new branch (our 'topic branch')
$ git branch bug1945

switch to the new branch
$ git checkout bug1945

do your work on the branch ...

commit your changes
$ git commit -a -m 'fixed bug 1945'

merge the changes back to the master
$ git checkout master
$ get merge bug1945

delete your topic branch
$ git branch -d bug1945

create another topic branch ...

That’s all there is to the basic Git topic branch workflow/pattern. Here’s a quick review:

  1. Create a new topic branch to work on your next feature
  2. Make your changes to the code
  3. Merge the changes back to the master
  4. Delete your branch

Related Git branch commands

Here’s a short list of Git commands related to the concept of topic branches:

create a branch and check it out in one step
$ git checkout -b bug1945

list your current branches
$ git branch

a little more information about the current branches
$ git branch -v

use the gui merge tool to see your merges
$ git mergetool

Git topic branch workflow: Summary

In summary, I hope this short tutorial on the concept of the Git topic branch workflow has been helpful.

Reporting live from Boulder, Colorado, this is Alvin Alexander.

If you need an example of how to use the Java Robot class in a Scala program, here's a quick one. As unusual as it might seem, this example lets your Scala program log back into a Mac OS X computer after you've been logged out, typically by a screensaver.

First, here's the Scala source code to demonstrate the Robot class: