ItemizedOverlay & ArrayIndexOutOfBoundsException > explanation AND solution !!

Showing 1-3 of 3 messages
ItemizedOverlay & ArrayIndexOutOfBoundsException > explanation AND solution !! Piwai 7/15/09 8:09 AM
Hi all !

I want to share with you the solution to a weird problem I was having.
I think it was mostly due to my misunderstanding of the way
ItemizedOverlay works. I couldn't find the sources, and the usage is
not much documented. Hopefully I could connect the debugger and dig...

I have a dynamic ItemizedOverlay, which means that from time to time I
update the list of items inside it, removing some and adding others.

When I would sometimes click on an item, than update the items, than
click again on another item, an ArrayIndexOutOfBoundsException was

I thought that calling the "populate()" method was enought to get the
Overlay updated, and that I only needed to override the createItem(int
i) and size() methods.

However, one should also call this :


The ItemizedOverlay remembers the index of the last focused item, and
does some stuff with it when you focus on a new item (by clicking on
it for instance). If you change the list of items, then the
"lastFocusedIndex" has no more meaning.

I think that this should be written in the documentation, or that
setLastFocusedIndex(-1); should be called from the populate() method.

One more thing : I think that the minimal touchable bounds of a
drawable should be customizable. I was playing with the map on the
emulator, and I was surprised that click quite far away from a pin
would still get it "tapped". I realized that the minimal box for an
item is 100*100 pixels, which is quite large...

Here is a sample code to show how to use a dynamic ItemizedOverlay,
and to customize the minimal size of a box (I have simplified it from
my real class, to show only the interesting stuff).

public class FaultStationOverlay extends
ItemizedOverlay<FaultStationItem> {

        private ArrayList<FaultStationItem>        faultStations                        = new

        private Drawable                                        normalMarker;
        private Drawable                                        fewFaultsMarker;
        private Drawable                                        muchFaultsMarker;

        private Rect                                                touchableBounds                        = new Rect();

        private static final int                        MIN_TOUCHABLE_WIDTH                = 10;
        private static final int                        MIN_TOUCHABLE_HEIGHT        = 10;

        public FaultStationOverlay(Drawable normalMarker, Drawable
                        Drawable muchFaultsMarker) {
                this.normalMarker = boundCenterBottom(normalMarker);
                this.fewFaultsMarker = boundCenterBottom(fewFaultsMarker);
                this.muchFaultsMarker = boundCenterBottom(muchFaultsMarker);


        public void updateStations(List<Station> stations) {
                for (Station station : stations) {
                        FaultStationItem stationItem = new FaultStationItem(station);

                        switch (stationItem.getState()) {
                        case FaultStationItem.STATE_NORMAL:
                        case FaultStationItem.STATE_FEW_FAULTS:
                        case FaultStationItem.STATE_MUCH_FAULTS:
                                throw new IllegalStateException("stationItem should have one of
the three available states");






        protected boolean hitTest(FaultStationItem item, Drawable marker, int
hitX, int hitY) {

               //The code written here was deduced from what happens
in the debugger ;-) .

                Rect bounds = marker.getBounds();

                int width = bounds.width();
                int height = bounds.height();
                int centerX = bounds.centerX();
                int centerY = bounds.centerY();

                int touchWidth = Math.max(MIN_TOUCHABLE_WIDTH, width);
                int touchLeft = centerX - touchWidth / 2;
                int touchHeight = Math.max(MIN_TOUCHABLE_HEIGHT, height);
                int touchTop = centerY - touchHeight / 2;

                touchableBounds.set(touchLeft, touchTop, touchLeft + touchWidth,
touchTop + touchHeight);

                return touchableBounds.contains(hitX, hitY);

        protected FaultStationItem createItem(int i) {
                return faultStations.get(i);

        public int size() {
                        return faultStations.size();


Re: ItemizedOverlay & ArrayIndexOutOfBoundsException > explanation AND solution !! christoph widulle 7/15/09 8:26 PM
Thanks for your post !
Helped me so much !!
Re: ItemizedOverlay & ArrayIndexOutOfBoundsException > explanation AND solution !! Piwai 7/21/09 12:48 AM
You are welcome ;-)

Oh, and by the way: the minimum touchable bounds were a bit small in
my example.

        private static final int
MIN_TOUCHABLE_WIDTH             = 10;
        private static final int

Although 100 might be too much, I realised that 10 was too small. I
finally used 50, and it works well.

The main problem is that the framework does not give you a "touched
area", but a touched pixel. Users have big fingers, obviously more
than one pixel large...

Is there any technical limits that prevents the framework from
calculating the touched area ? Or is there anyway to know the real
"touched area" ?

Any comment on this will be appreciated ;-).

Best Regards,

On Jul 16, 5:26 am, christoph widulle