CategoryAPI

Search icon style in Action Bar

Do you know that you can change menu icon style in honeycomb Action Bar?… and cannot do the same with search icon since last SDK tools update.

Join discussion here: https://groups.google.com/forum/?hl=en#!search/ActionBar$2FMode$20Style$20Regressions/flutuberwachung/KAamkrQ_6LI/K5dIort18eYJ

//DL

Efficient JSON parsing on Android

The most common question: which parser to use for JSON parsing on Android device.
Here are several options:

  1. That comes with platform (org.json) – optimized since 2.2
  2. Jackson
  3. GSON
  4. Something else

Let’s take a look.

  1. org.json – Not very convenient to use. Doesn’t support stream parsing out of the box.
  2. jackson – very convenient to use, excellent mapping features, custom stream deserializers… but don’t use it on Android. Like Facebook and Twitter did… This will put you into the grave:) Everything is excellent before you will take a look into the ddms log.
  3. This is the the log when you are trying to open Facebook news feed (Google Nexus One, 2.2. Facebook v.1.5):

    02-25 02:49:52.862: DEBUG/dalvikvm(22579): GC_FOR_MALLOC freed 3339 objects / 504352 bytes in 68ms
    02-25 02:49:53.762: DEBUG/dalvikvm(170): GC_EXPLICIT freed 1246 objects / 63000 bytes in 94ms
    02-25 02:49:54.002: DEBUG/WifiService(85): releaseWifiLockLocked: WifiLock{NetworkLocationProvider type=2 <a href="mailto:binder=android.os.Binder@44b3cb58">binder=android.os.Binder@44b3cb58</a>}
    02-25 02:49:58.942: INFO/ActivityManager(85): Starting activity: Intent { cmp=com.facebook.katana/.activity.stream.StreamActivity }
    02-25 02:49:59.152: DEBUG/dalvikvm(14758): GC_FOR_MALLOC freed 2159 objects / 607368 bytes in 37ms
    02-25 02:49:59.532: DEBUG/dalvikvm(14758): GC_FOR_MALLOC freed 2156 objects / 152352 bytes in 40ms
    02-25 02:49:59.752: INFO/ActivityManager(85): Displayed activity com.facebook.katana/.activity.stream.StreamActivity: 778 ms (total 778 ms)
    02-25 02:50:00.262: DEBUG/dalvikvm(14758): GC_FOR_MALLOC freed 1128 objects / 271976 bytes in 46ms
    02-25 02:50:00.632: DEBUG/dalvikvm(14758): GC_FOR_MALLOC freed 4492 objects / 928160 bytes in 55ms
    02-25 02:50:03.922: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1845 objects / 556056 bytes in 66ms
    02-25 02:50:04.012: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 697 objects / 49840 bytes in 39ms
    02-25 02:50:05.702: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1686 objects / 560232 bytes in 45ms
    02-25 02:50:05.812: DEBUG/dalvikvm(14535): GC_EXPLICIT freed 1270 objects / 45920 bytes in 142ms
    02-25 02:50:05.862: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 535 objects / 26608 bytes in 70ms
    02-25 02:50:06.422: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1279 objects / 390888 bytes in 42ms
    02-25 02:50:06.782: INFO/dalvikvm(14758): Jit: resizing JitTable from 8192 to 16384
    02-25 02:50:06.952: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1490 objects / 558784 bytes in 42ms
    02-25 02:50:08.702: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1222 objects / 498232 bytes in 38ms
    02-25 02:50:09.272: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1711 objects / 349648 bytes in 72ms
    02-25 02:50:11.322: DEBUG/dalvikvm(14758): GC_FOR_MALLOC freed 3758 objects / 1647488 bytes in 45ms
    02-25 02:50:12.012: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 2347 objects / 624600 bytes in 53ms
    02-25 02:50:12.142: DEBUG/dalvikvm(22579): GC_EXPLICIT freed 2020 objects / 316328 bytes in 86ms
    02-25 02:50:15.712: DEBUG/dalvikvm(14758): GC_FOR_MALLOC freed 2275 objects / 823520 bytes in 65ms
    02-25 02:50:17.552: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1776 objects / 591760 bytes in 54ms
    02-25 02:50:17.822: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 623 objects / 101408 bytes in 73ms
    02-25 02:50:19.452: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1172 objects / 473632 bytes in 57ms
    02-25 02:50:20.212: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 700 objects / 74080 bytes in 46ms
    02-25 02:50:20.772: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 804 objects / 119440 bytes in 37ms
    02-25 02:50:21.672: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1529 objects / 344096 bytes in 42ms
    02-25 02:50:23.602: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 914 objects / 326848 bytes in 44ms
    02-25 02:50:23.662: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 174 objects / 7232 bytes in 40ms
    02-25 02:50:23.752: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 116 objects / 10616 bytes in 50ms
    02-25 02:50:25.202: DEBUG/dalvikvm(14758): GC_EXTERNAL_ALLOC freed 1286 objects / 368296 bytes in 70ms
    

    scaring, right?:)

  4. gson – GSON stream is very nice and small. It doesn’t produce so much garbage as Jackson. But it is not so convenient to use. Good news that GSON stream parser is included in API 11.

I would prefer GSON and org.json for my code.

Happy parsing!

//DL

SIP support in Android 2.3 gingerbread

As you know there is native SIP support in 2.3. There’s SipManager class for it.

But it doesn’t work on Emulator! Both functions isApiSupported and isVoipSupported return false, SipManager.newInstance(this) returns null.

Application is installed without errors althought we have this line in manifest file:

<uses-feature
    android:name="android.hardware.sip.voip"
    android:required="true" />

Holy crap, Google! πŸ™‚

//DL

Localized vs locale-independent double

Sometimes we need to have double converted to string in specific format. For example, when this data will be used in system level rather than displayed to user.

Let’s say, we need to send double to server in format like X.XX

The easiest (wrong) way to do this, for example:

String formattedDouble = String.format("%.2f", 0.01f);

Expecting to see “0.01” as output. But actually it will be converted using phone default locale.

On most locales this will work and nobody will notice the difference. However if your default locale is, for example, Russian, decimal delimeter for it is “,” opposing to “.”

In this case we will get “0,01” and the server will return error (evil laugh here) as it expects “.” as delimiter.

So we want to avoid any unexpected locale-specific formatting. We can use US locale to get expected results.

String formattedDouble = String.format(Locale.US, "%.2f", 0.01f)

This will return β€œ0.01” and server will be happy.

Another way is to use DecimalFormat for advanced formatting.

Again, be aware of default locale, doing this:

DecimalFormat decimalFormat = new DecimalFormat("0.00");
String formattedDouble = decimalFormat.format(0.01f);

could result β€œ0,01” if default locale is Russian.

//Anton

How to quick create QR-code for your Android application

Sometimes you need QR-code to be posted somewhere to promote your Android application. You can use a lot of online tools to create such image, but from my point of view the fastest way is to use Google Chart API for this.
What do you need is to create URL-request to Google Chart API with correct parameters:

1. The main part is URL:

http://chart.apis.google.com/chart?cht=qr

where cht=qr is the first parameter saying that we need QR-code.

2. Then adding image size, for example 200×200 pixels:

http://chart.apis.google.com/chart?cht=qr&chs=200x200

3. At last you should add label to be encoded in QR-code, and you need to have direct link to Android Market with your application. To do this you need to create market:// scheme url:

market://details?id=com.your.packagename

where “com.your.packagename” is the main package name of your application. Now add it to the full charts url:

http://chart.apis.google.com/chart?cht=qr&chs=200x200&chl=market://details?id=com.yota.screentest

The result of it is the QR-code image that you can add as src to img html tag:

Android 2.2 Froyo new APIs

Just a reference list of classes. Make sure you haven’t missed some of them like AndroidHttpClient or HeterogeneousExpandableList.

http://developer.android.com/sdk/api_diff/8/changes.html

Completely new packages:

New classes

android.app

Continue reading

© 2019 Android Tales

Theme by Anders NorenUp ↑