Short source code examples

If you’re working with an Android application, this source code seems to work to load an image from a file:

Bitmap bitmap = BitmapFactory.decodeFile(pathToPicture);

The Bitmap and BitmapFactory classes are located in the package:


Assuming that your pathToPicture is correct, you can then add this bitmap image to an ImageView like this:

When you want to provide Up/Back/Home navigation in an Android sub-activity, this Java code doesn’t seem to be needed:


The “Add Up Action” docs on this page state that is needed, but as of some (unknown) Android version it is not needed. All you need to do is specify the “Up/Parent” activity in your AndroidManifest.xml file, like this:

As a quick Android “centering” tip, I found that by adding this snippet to the ImageButton below:


I was able to get the ImageButton to center itself in the Toolbar. Here’s the layout code for the Toolbar and the ImageButton:

As another quick Android example, this Java source code shows how to add a “click listener” (on click/tap event) to an Android Button:

To set an image on an Android ImageButton, you basically just need to put your image in the “res/drawable” directories, and then add this tag to your ImageButton XML definition:


That assumes the image file is named my_image.png.

Here’s a full ImageButton example to demonstrate this:

To access a String resource/value that you’ve defined in an XML file from your Android/Java code, use the Android getString method, like this:

String prefsKeyTimeBetweenNotifications = getString(R.string.prefsKeyTimeBetweenNotifications);

In this example I’ve defined prefsKeyTimeBetweenNotifications as a string resource key in a res/values/strings.xml file, like this:

Android SQLite FAQ: I’m trying to catch an exception when my SQLiteOpenHelper/SQLiteDatabase insert method fails (typically because of a unique/duplicate constraint); why can’t I catch the exception?

I’m working on a very small Android “notifications” app where I a) display a notification using a background service, b) the user taps the notification, which c) takes them to a view that shows the full text of the notification. To get this to work, I need to send the full text along with the notification. However, this didn’t work easily. The text that was shown by my full view would be updated once, then never again.

After a lot of googling and trial and error, I finally got this approach in my sendNotification method working:

Not much to see here, just a couple of Android permissions that an Android app may need to access the network/internet:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

As the names imply, the first permission requests access to the internet, and the second permission is needed to check the internet status/state. These permissions need to be put in an Android app’s AndroidManifest.xml file.

The following code shows how to create a CDATA element (string resource) in a strings.xml file in an Android application:

The Google/Android SlidingTabsColor project has a little example of how to use a ViewAnimator widget. Here’s the XML layout portion of the ViewAnimator:

I found that I can control the scrolling speed of an Android ListView using code like this in a Fragment:

Here’s some source code for a simple Android checkbox setOnCheckedChangeListener/OnCheckedChangeListener example:

The following code shows a basic Android notifications example. The source code is not mine; you can find it at the URL I’ve linked to. I just wanted a copy of it here so I can find it easily in the future.

Here’s the code:

I’ve been working through the examples in the excellent Android book, Android Programming: The Big Nerd Ranch Guide, and was trying to figure out how the example in Chapter 10 worked, specifically how the crime data was being saved without the use of a “save” button.

The following source code shows how to declare that an Android activity is the launcher activity for your application:

When you need to determine which item in a ListFragment has been tapped/selected in an Android application, this code shows how to do that:

This is how you can show an Android Toast message from a Fragment:

Toast.makeText(getActivity(), "Click!", Toast.LENGTH_SHORT).show();

As shown, don’t forget to call .show() at the end of the makeText method. Forgetting to call show() is a common mistake.

If it helps to see this in more context, this is a complete onListItemClick method from a ListFragment subclass I’m currently working on:

Here’s a quick example of how to use variable names with the Scala foldLeft method:

val pSum = movies.foldLeft(0.0)((accum, element) => accum + p1Movies(element) * p2Movies(element))

In this example the variable movies is a Seq, and the variables p1Movies and p2Movies are Map objects.

I’ve been working on putting together a Raspberry Pi “video streaming with camera module” system, and these are the links (and a few notes) that were helpful in the process. At the moment I’m using the “Stream video with MJPEG-streamer” approach, but it only shares images of snapshots that are taken a few times a second, so it’s not exactly what I had in mind. If I try anything next, I’ll go with the “Using nginx-rtmp” approach, which seems like it might serve up a real, live video stream.

Here are the links: