Advanced ListView: populating a list with images and text

The Android documentation says that the ListView widget is easy to use. It is true if you just want to display a simple list of strings but as soon as you want to customize your list things become more complicated.

The following is a tutorial on how to write a ListView that displays a static list of images and strings, similar to the settings list on your phone.

For the impatients, here's the Eclipse project code and a picture of the final result:

How to detect motion on an Android device

For learning purposes I've written PicInMotion, a small Android application that automatically takes a picture when it detects motion in the camera preview.

This article explains how the simple motion detection mechanism works.

Update: source code is available at github, be sure to clone the standalone repository.

Strategy

Dead simple. I store a frame and compare it to the next, if the differences in the two frames are above a certain threshold then motion is detected. Common image detection techniques are far more complicated than what I'm using here and image processing operations (like erode, dilate and some other form of noise reduction) are normally applied to frames in order to reduce digital noise.

Adding control to the OpenGL spinning cube

This demo extends on the Create a spinning cube OpenGL to show how to control the cube using a D-pad. We're going to increment the speed rotation along the X and Y axes using the directional keys of the D-pad.

The biggest change is that we now have our custom class that extends the SurfaceView. We do this so we can override the onKeyUp method and be notified when the user uses the D-pad.

The onCreate of our Activity looks like this:

{% highlight java %} public class SpinningCubeActivity2 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // go fullscreen requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // create our custom view GLSurfaceView view = new OpenGLSurfaceView(this); view.setRenderer((Renderer)view); setContentView(view); } } {% endhighlight %}

"touch" a file on Android

This snippet mimics the behaviour of the unix touch command on the Android platform:

{% highlight java %} public static void touch(File file) throws IOException { if(!file.exists()) { File parent = file.getParentFile(); if(parent != null) if(!parent.exists()) if(!parent.mkdirs()) throw new IOException("Cannot create parent directories for file: " + file); file.createNewFile(); } boolean success = file.setLastModified(System.currentTimeMillis()); if (!success) throw new IOException("Unable to set the last modification time for " + file); } {% endhighlight %}

Taking a picture with android.media.Camera

Sometimes you may want more control over the stages involved when taking a picture or you may want to access and modify the raw image data acquired by the camera. In these cases, using a simple Intent to take a picture is not enough.

We're going to create a new Activity and customize the view to make it full screen inside the onCreate method.

{% highlight java %} public class TakePictureActivity extends Activity { private Preview mCameraView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Force screen in landscape mode as showing a video in // potrait mode is not easily doable on all devices setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // Hide window title and go fullscreen requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); mCameraView= new Preview(this); setContentView(mCameraView); } } {% endhighlight %}