7

Hi in my application i need to display the input address according to the locale set by the user. Has anyone worked or address formatting or has any pointers wrt it in Android platform.

Matt
  • 22,721
  • 17
  • 71
  • 112
shailbenq
  • 1,440
  • 1
  • 14
  • 25

2 Answers2

6

Check out googlei18n/libaddressinput: Google’s postal address library, powering Android and Chromium. There are two modules in the project :android and :common. You should only need :common to format the address for locale aware display.

import android.location.Address;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import com.google.i18n.addressinput.common.AddressData;
import com.google.i18n.addressinput.common.FormOptions;
import com.google.i18n.addressinput.common.FormatInterpreter;
...
public static String getFormattedAddress(@NonNull final Address address, 
                                         @NonNull final String regionCode) {
    final FormatInterpreter formatInterpreter
            = new FormatInterpreter(new FormOptions().createSnapshot());
    final AddressData addressData = (new AddressData.Builder()
            .setAddress(address.getThoroughfare())
            .setLocality(address.getLocality())
            .setAdminArea(address.getAdminArea())
            .setPostalCode(address.getPostalCode())
            .setCountry(regionCode) // REQUIRED
            .build());
    // Fetch the address lines using getEnvelopeAddress,
    List<String> addressFragments = formatInterpreter.getEnvelopeAddress(addressData);
    // join them, and send them to the thread.
    return TextUtils.join(System.getProperty("line.separator"),
            addressFragments);
}

NOTE: regionCode must be a valid iso2 country code, because this is where the format interpreter pulls the address format from. (See RegionDataConstants for the list of formats, if you're curious.)

Sets the 2-letter CLDR region code of the address; see AddressData#getPostalCountry(). Unlike other values passed to the builder, the region code can never be null.

Example: US

801 CHESTNUT ST
ST. LOUIS, MO 63101

Example: JP

〒1600023
NISHISHINJUKU
3-2-11 NISHISHINJUKU SHINJUKU-KU TOKYO

Funktional
  • 4,083
  • 1
  • 30
  • 27
-1

I've done a fair bit of localization on other platforms. Here's a guide to localization for Android: http://developer.android.com/guide/topics/resources/localization.html#using-framework

One approach would be to use localized resources to store MessageFormat strings that correspond to the Address layout for each locale you want. From there, you'll need a reference to the global address block standards and create the format strings for each locale you need.

James
  • 710
  • 6
  • 19
  • Thanks, i am referring the same android localization it does has Address API but no formatting of address is provided based on the locale selected. – shailbenq Jun 29 '12 at 21:46
  • 1
    I think the reasoning is that an already entered postal address "belongs" to a locale and has a fixed way of formatting it. It doesn't make much sense for example to express a US postal address by German conventions or the other way around. If you are accessing the ContactsContract database then just use the formatted address field of the StructuredPostal entity. – tiguchi Jun 29 '12 at 21:57
  • In this [Google example code](http://developer.android.com/training/basics/location/geocoding.html) they format an address line using an idea similar to my suggestion. Not sure if there is an easier way. – James Jun 29 '12 at 22:03