Tuesday, September 21, 2010

Solution to: onContextItemSelected not called after selecting a context menu

Here is a solution to Android development problem, where the onContextItemSelected method is not called even after you see the context menu popup and selecting one of the items there.

1. Make sure that registerForContextMenu has been called, with the parameter the view you have the context menu on. For ListActivity with a context menu on the items, call registerForContextMenu(getListView()).

2. If you have an options menu on your activity (the one that pops up from the bottom of the screen when you press the hardware/softshell MENU button, you may be interrupting the event flow when you override onMenuItemSelected method. Overriding the onMenuItemSelected was taught in the Notepad v2 tutorial source code. That was partially wrong. You should not override it, instead you should override onOptionsItemSelected, and return false when the menu item is not handled. Example:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.menuImpor) {
        // do your work...
        return true;
    } else if (item.getItemId() == R.id.menuEkspor) {

        // do your work...
        return true;
    }
    return false; // no need to call super.onOptionsItemSelected(item)
}

That way, your onContextItemSelected should now be called correctly.

In a nutshell, "Menu" is the generic term for both "Context" and "Options" menu. Make sure you pay attention to the naming of the methods.

Tuesday, May 25, 2010

Setting value (object) while enumerating an NSMutableDictionary

Let's say you have an NSMutableDictionary instance which maps strings to numbers. For example, a list of stage names and the number of times the stage has been completed:

"level1" => 10
"level2" => 2
"final" => 1

I wanted to create a "clear data" function, so I enumerate the dictionary using the new foreach loop and set the value to 0 without adding or removing the key:

for (NSString *key in dictionary) {
    [dictionary setObject:[NSNumber numberWithInt:0] forKey:key];
}

This compiles fine, but a runtime error will occur:

Collection was mutated while being enumerated.

Unfortunately there is no API to modify the value even without adding or removing the key.

But, there is a solution: enumerate the keys instead of the dictionary. This works:

for (NSString *key in [dictionary allKeys]) {
    [dictionary setObject:[NSNumber numberWithInt:0] forKey:key];
}

Friday, April 9, 2010

Preventing TextView with links inside a ScrollView from dimming

I wrote an Android application that shows a scrollable TextView because I put it inside a ScrollView. When I added links to the TextView, I had to execute this so that the links can be clicked with proper highlighting:

textView.setLinksClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());

It works:

However, when I use my finger to scroll it, the text other than links are dimmed, very, very dark until we can almost see nothing.
Calling setClickable(false) and setLongClickable(false) fixed the issue, but the link itself is not highlighted anymore when "hovered", and the user may think that the link is not clickable.

I found a solution, which is not perfect, but works. Just set the color of the TextView. The links will stay on the same color, but the normal text will change color, and it does not dim anymore!

Here is what I get using #fff (a.k.a. 0xffffffff) color.

Tuesday, April 6, 2010

String.format in Android is extremely slow!

I'm used to using String.format to construct messages. Even when I don't need to specify width or number of decimal places. It just looks neater to the eyes.

For example, on the Alkitab (Bible) application, I wrote this for debugging:

Log.d("alki", String.format("tebakKitab fase 3: dengan %s:%d skor %d", ref.pendek, ref.pos, skor));

Each of "my operation", involving about 70 calls to the above line, takes about 300 ms. I thought that was acceptable.

But when I tried to build a game engine, I found a bottleneck somewhere that drags the fps. I found it to be the String.format call, which takes about 6ms for just one call! That's ridiculously slow.

Guess what: when I change the above line to:

Log.d("alki", "tebakKitab fase 3: dengan " + ref.pendek + ":" + ref.pos + " skor " + skor);

"my operation" takes only 22 ms!

Okay, once again: Don't use String.format in Android applications!

Note: When I traced method calls to know what makes it slow, String.format apparently calls a very deep code, something with DecimalFormatter, even CurrencyFormatter even though I didn't use it. It also calls com.ibm.icu.** packages. It really has a huge logic inside.