How to catch any tap on the screen in an Android app

29 Dec

We recently had to implement a session timeout functionality in an Android app. We decided to expire the session (log the user out form the app) after 30 minutes of inactivity. The first thing to do here is the define what “inactivity” means. Our business analyst decided that not touching the app for 30 minutes meant the user was not interacting with the app. For most of the apps this is an acceptable criteria, but that are other cases were it might not make sense (for example trading apps, where the user can leave the app open for longer periods to monitor his positions).

Since in our case inactivity meant no touch, we needed a way to trap a touch in an Android Activity (before it gets dispatched to a UI control). Luckily for us, that’s really easy to do in Android: the Activity class has the method dispatchTouchEvent(MotionEvent event). We implemented that in our superclass and now we’re notified every time there’s a touch on the UI). Don’t forget to return false in that method to allow the event to travel up the hierarchy of UI controls. Otherwise, no UI controls will respond to the events that you want to catch.

Here’s the full method:

Steve Jobs: The Exclusive Biography

16 Dec

I’ve just finished listening to the Steve Jobs: The Exclusive Biography audiobook.  Steve Jobs is portrayed with all his good and bad parts, but what struck me the most was the number of mistakes he made (or how big some of them were). We usually see only the successes of someone and we think they’ve succeeded so much because they’ve never made any mistakes. Quite the opposite is true though. Steve Jobs was no exception. Until he returned to Apple he made a lot of mistakes, both in business and in his personal life. But all those events helped him learn something, helped him to become a better leader.

Landscape orientation for views in UITabBar

19 Nov

Enabling the landscape orientation for a view in an iOS app is pretty easy: just implement the shouldAutorotateToInterfaceOrientation: method and return YES.

If you’ve done that for a UIView that’s part of a UITabBar and the view is not rotated (in fact none of the views from that UITabBar are rotated) then it’s a good chance you’re not complying with this requirement:

All child view controllers in your UITabBarController or UINavigationController do not agree on a common orientation set.
To make sure that all your child view controllers rotate correctly, you must implement shouldAutorotateToInterfaceOrientation for each view controller representing each tab or navigation level. Each must agree on the same orientation for that rotate to occur. That is, they all should return YES for the same orientation positions.”

The information above is extracted from:
http://developer.apple.com/library/ios/#qa/qa1688/_index.html

World MoneyShow in London

17 Nov

Last weekend (11-12 Nov 2011) took place The World MoneyShow, in London, at the Queen Elizabeth II Conference Center. A lot of trading companies (focused on Forex and Spread Betting mainly) presented their offer. The event was addressed to retail users and the average age of the participants was over 35.

Among the main themes that everyone was talking about were volatility and algorithmic trading (or how to compete against that). Having so much volatility in the past 2-3 years, the definition of medium and long term investing has basically changed. Most people think of just a few months when talking about medium term strategies.

The reality of trading is that 80% of traders are losing money. This seems to be caused by:

1) unrealistic expectations: a statistic of the average return for hedge funds over a 30 years period showed a return of 12% per year. A lot of new traders have higher expectations, hoping to do that in a month. This is fueled by advertising and by the stories of someone turned an account of a few thousands pounds into millions of pounds in just a few months.

2) lack of a trading plan: instead of having a very clear trading strategy, most people end up gambling basically (just hitting buy/sell without following a set of rules)

3) complexity of technical analysis: successful trading can be defined by the 3Ms, Mind (60%), Money (30%), Method (10%). Most people focus mostly on the method (charts & indicators) in the search for an “always right” strategy, which obviously doesn’t exist. They should focus instead on the psychology of trading (or the discipline of how to follow a trading plan) and money management (to make sure you don’t lose too much in a trade).

Maybe the most important message (especially these days when everything changes so fast) was: get rich slow (it’s a marathon, not a sprint).

Android build failed: cannot find ant/pre_setup.xml

29 Aug

If you’re trying to run an ant task in your Android project and you get this error “Cannot find /${sdk.dir}/tools/ant/pre_setup.xml” it means that you have to define the sdk.dir variable and point it to your Android SDK directory.

You should create the local.properties file (in your project’s root directory) with the following content:

sdk.dir=~/android-sdk-mac_x86

Cannot find symbol: method getPdfObject(java.lang.Object)

20 Aug

I was working recently with iText library (http://itextpdf.com/) to read annotations from PDF files. I found the following code example on the web:

At compilation I was getting the following error:

Comments.java:28: cannot find symbol
symbol  : method getPdfObject(java.lang.Object)
location: class com.itextpdf.text.pdf.PdfReader
          PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject( iter.next());
                                                         ^
1 error

The fix is pretty easy and there are 2 options:

  1. use ListIterator<PdfObject>
  2. typecast iter.next(): (PdfObject) iter.next()

Get UITextField text as you type

4 May

If you need as-you-type validation on a UITextField, then you need to be able to get the complete text as soon as the user inputs a character. You can use -textField:shouldChangeCharactersInRange:replacementString: as a way to get notified when the user taps a character, but you need to do a small computation in order to get the final text of the text field:

You can now add as-you-type validation if you need it.

Memory upgrade limit on your laptop: SSD to the rescue

17 Apr

I have a pretty old, 2007, white 13” Macbook. 3 years and a half is a long time for a laptop :) . But it is working great. The main problem I had was the memory upgrade limitation: this model (September 2007) supports up to only 2GB of RAM and I was needing more. Freezes of 1-2 seconds would happen frequently throught the day and they were really annoying.

I was considering buying a new laptop, but SSDs started to come to my attention. They are quite expensive (yet) but I bought the OCZ Vertex 2 240GB SATA II 2.5-inch Internal Solid State Drive
(for £350) and it is worth every penny. My computer is much faster now, no more freezes and I don’t need to buy a new computer anymore.

When your operating system needs more memory than what’s physically available as RAM (2GB is my case) it starts writing that information to the disk. With a classic disk (HDD) it takes a lot of time to write and read that information, but with an SSD it is many times faster (almost as fast as using the RAM, since it’s basically the same technology).

Xcode 4 Warning: The Selected Run Destination Is Not Valid For This Action

22 Mar

After updating to the latest Xcode 4 release (9 Mar 2011 release) I was getting this warning message “The selected run destination is not valid for this action” when I was trying to run my application on my jailbroken 3G iPhone (version 4.2.1).

This seems to be related to the IPHONEOS_DEPLOYMENT_TARGET constant missing from project.pbxproj. I was able to fix the problem by changing the iOS Deployment Target in the project settings:

The problem was fixed regardless of the value I selected in the above list (it worked with 3.0, 4.0, 4.2 or any other value). When I changed the value in that list though, the IPHONEOS_DEPLOYMENT_TARGET setting was added to a section in project.pbxproj and I was able to run the app on the device.

How to Plot a UK Post Code on Google Maps

19 Mar

Google Maps is easy to integrate into any website. In the following code snippet you can see how to plot a UK post code on Google Maps:

The above script is a simplified version of this code snippet: http://stackoverflow.com/questions/3495006/google-maps-postcode-plotting

The post code used in this example is W2 6LE and is the post code of an office building, in Paddington, London.

The map is initialized using these coordinates: -34.397, 150.644. They represent a location in the US and they are from an official Google Maps code snippet. This initial location is not important though, because the map is centered once we get the coordinates for our post code.

A geocoder object is used to get the coordinates corresponding to an address (a post code in our case).

This post doesn’t explain the basic initialization code for Google Maps. For details on that part please consult the official documentation: http://code.google.com/apis/maps/documentation/javascript/basics.html.