23 Sept 2015

how to get the user prefered units ? (metric (meters) or imperial (miles)) in Android?

I would like to know what are the user prefered units between miles and meters. It seems that the "Locale" class doesn't allow that. 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Although there isn't a way to get the preferred units using Locale, you can assume by default that if the Locale indicates the United States or Myanmar (locale.getISO3Country().equalsIgnoreCase("usa") || locale.getISO3Country().equalsIgnoreCase("mmr")) the preferred units are imperial, otherwise the preferred units are metric.



I don't think there's necessarily a correct answer to the Metric/Imperial question for all locales.
For example, in the UK we generally use miles for large distances like a car journey, feet and inches for people's height but may use meters to describe the dimensions of a room.
We almost always use litres for measuring liquid volume except for beer and sometimes milk which can still be sold in pints.
Kilograms are usually preferred to pounds and ounces but most recipe books will list both since some people still have scales which use the latter.
I think you will have to do some research to find out which countries use which scale for what you're interested in measuring (and of course, give the user the option to select one or the other manually.)

Since some people outside the US may have set the language to English (US) it is eventually better to take the return value of getSimCountryIso() instead of getISO3Country()
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();
If the countryCode is us the SIM card is an american and therefore imperial units might be the better choice - in any other case the metric system is applied

No comments:

Post a Comment