Adding click handler for RecyclerView List Entries

If you’re migrating from the ListView to RecyclerView in API Level 21 support library (i.e. Android 5.0 / Lollipop), you may be wondering how to add a click listener for entries in the list. With the ListView you would provide a single listener for all list entries using the ListView.setOnItemClickListener() method, which takes an implementation of the AdapterView.OnItemClickListener interface as its parameter. In your implementation of this interface you would override the onItemClick() method where you are given the ListView instance, the View for the entry in the list, its position, and ID as method parameters.

The stumbling block with the RecyclerView is that it does not have this setOnItemClickListener() method. The solution is actually quite simple, you can use the standard setOnClickListener() method provided by the View object. You can do this for the View objects corresponding to each list entry.

You would typically inflate the required View object from the respective resource definition in your override of the RecyclerView.Adapter.onCreateViewHolder() method. You would also construct your version of the RecyclerView.ViewHolder class there and pass the inflated View object as a constructor parameter.

The View.setOnClickListener() accepts a View.OnClickListener implementation as its parameter, and your ViewHolder is a good place to implement this. This is because the ViewHolder class has access to the list entry View object, its parent, position, and ID which are exactly the same as the parameters you were provided in the old ListView onItemClick() method.

Your ViewHolder class could look something like the following:

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    protected ViewHolder(View v)
        // Inflate all your UI elements for the list entry ..
        // and ...

    public void onClick(View v)
        int pos = this.getPosition();
        long id = this.getItemId();
        ViewParent parent = v.getParent();

And that’s really it! The code above should provide a good start for your ViewHolder class. Hopefully this has been helpful.

Constrained Fluid Layout

How to have a fluid web layout, and still have control over the layout width.

Users of the web all have different screen sizes, and not everyone uses the browser in the maximised state, especially those with large screen sizes. There are also a wide variety of portable devices with web browsing capabilities e.g. the Nokia n800, n810 and of course the iphone. Even many of the Portable Continue reading “Constrained Fluid Layout”

Selecting fonts for the Web

While it would be nice if there was a cross-browser cross-platform font downloading system for the web, the current system of Cascading Style Sheets isn’t too bad. It does allow you to select a number of fonts that the browser can cascade through.

This gives you the option to select fonts that fit the look you’re after, but may not be widely available. You can cascade down to fonts that are more widely available fonts.

Or you may wish to use a font that is available on only one platform. In such a case you could check to see if there are similar fonts available in other systems which can be used as a fallback. You can check the commonly available fonts on various systems in this font list. This will also flag the fonts not available on your computer.

If you want to check the similarity between a number of fonts to narrow your choices or to determine the order of the cascade, this font comparer will help your decision.