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.
Device locale - Android Tutorial Locale is the representation of a specific region.
In order to localize the strings used in your application , make a new folder under res with name of values-local where local would be the replaced with the region. Once that folder is made, copy the strings. xmlfrom default folder to the folder you have created. And change its contents.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With