Short source code examples

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:

This code shows how to create a Java FlowLayout that flows left and has horizontal spacing of ten pixels and vertical spacing of five pixels:

FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT, 10, 5);
jPanel.setLayout(flowLayout);

Here’s what the FlowLayout constructor arguments look like:

FlowLayout(int align, int horizontalSpacing, int verticalSpacing);

FlowLayout 'align' property definitions

The align property can be any of these:

If you want to set your Java JScrollPane scrollbars to always scroll, both horizontally and vertically, this code shows the solution:

These are some simple Java/Scala JOptionPane examples in the form of a Scala utility class (an object, actually).

The following Java source code comes from Sun Microsystems, and shows a JTable example, including the use of an AbstractTableModel, a JScrollPane, and methods like:

  • isCellEditable
  • getColumnName
  • getValueAt
  • getRowCount
  • setPreferredScrollableViewportSize

Here’s the example Java JTable source code:

The following source code comes from Sun Microsystems, and shows a JComboBox, ActionListener, and methods like addActionListener, actionPerformed, getSelectedItem, and more.

Here’s the example Java source code:

The following source code comes from Sun Microsystems, and shows how to implement a MouseListener, including the following MouseListener methods, which all take a MouseEvent:

  • addMouseListener
  • mousePressed
  • mouseReleased
  • mouseEntered
  • mouseExited
  • mouseClicked

Here’s the example Java source code:

The following source code example comes from Sun Microsystems, and shows various things related to showing and clicking a JButton, including using the JButton class, ActionListener, ActionEvent, ImageIcon, setEnabled, setMnemonic, setToolTipText, and actionPerformed.

jbutton, actionperformed, actionlistener, actionevent, setmnemonic, java, swing, jframe, jpanel, tool tip, tooltip, image, imageicon, event, click

The following source code comes from Sun Microsystems, and shows various things related to showing and clicking a JButton, including using the JButton class, ActionListener, ActionEvent, setMnemonic, and actionPerformed.

jbutton, actionperformed, actionlistener, actionevent, setmnemonic, java, swing, jframe, jpanel

The following source code comes from Sun Microsystems, and shows various things related to handling mouse events, including MouseEvent, MouseMotionListener, and the mouseDragged and mouseMoved methods that accept MouseEvent objects.