I'm using the following code to retrieve the user's local currency symbol:
public static String getCurrencySymbol() {
Currency currency = Currency.getInstance(Locale.getDefault());
return currency.getSymbol();
}
However, one of my users moved from UK to the US and his currency symbol is still showing as the British Pound. Is there some way to make it show as a US Dollar symbol ($) now that's he's moved to the US?
I think Locale.getDefault() is just related to the language settings on the phone and has nothing to do with the current location (of the phone). That's why the actual Locale did not change, even though your User moved to the US.
A possible solution would be to add a settings option in your App, so the user can change his currency when needed. This option then would just change your application wide Locale used for the currency.
Related
I'm trying to get the country language of the user, not the selected one on the phone.
For example:
I'm building an English learning app, but in my phone I've set language to English for learning purposes, and it doesn't translate because it thinks that my language is English, but I live in brazil, I want it to find that my language is Portuguese. to get languages by especific location.
I have tried some codes, but not seems to work.
I have many users in my app that have English language on phone, so they are facing the same problem.
var currentLanguage: String = Locale.getDefault().language
var language = this.getResources().getConfiguration().getLocales().get(0).language
They both give me the same value, that my language is english, and if I get the country it says I'm in US.
EDIT:
I'm able to get the country code with it:
var tm = this.getSystemService(TELEPHONY_SERVICE) as TelephonyManager
var countryCodeValue = tm.networkCountryIso
It returns br, but how can I get the language from this tag?
There is something like:
countryCodeValue.language
?
Well you have it there in your own description of the problem. You want to get the language based on the country the user is in. So you need to first get the country where the user is in. So it's a location problem.
There are several options. One is to get the user's location using the last known location of the user.
Docs: https://developer.android.com/training/location/retrieve-current
The problem here is you need to then transform coordinates to country. There are some API's for this, Google maps has one.
Maybe an easier approach is to use the TelephonyManager to get the country code of the user. This also has a problem because it relies on having a sim card.
Finally you need to transform that country to a language. But you can do that with the Locale class.
IMHO, it would be easier to give your users a selector for the language, that way they can decide what language to use instead of you choosing for them based on some algorithm.
Update:
It's not that straight forward to get the language of a Country. Basically some countries have multiple official languages for example. There are other questions addressing this issue. But in short, you either have a static file mapping from country to language or you iterate over the available Locales and choose one of the languages for the country.
Here are other questions:
If you have the ISO country code `US`, `FR`, how do you get the Locale code (`Locale.US`, `Locale.FRANCE`)?
Getting default language from country code in Java
How to get the language code based on country code in java
Well guys, I did it, and its working almost as I wanted it to be.
I'll have a little work to do it for too much countries. If someone have a better Idea, I'd be glad to know. Thanks. Maybe it can help someone.
var tm = ctx.getSystemService(AppCompatActivity.TELEPHONY_SERVICE) as TelephonyManager
var countryCodeValue = tm.networkCountryIso
fun getLanguage(country: String) : String{
var current : String
when{
country.contains("br") && Locale.getDefault().language.equals("en") -> current = Locale.forLanguageTag("pt").toString()
country.contains("in") && Locale.getDefault().language.equals("en") -> current = Locale.forLanguageTag("hi").toString()
country.contains("pk") && Locale.getDefault().language.equals("en") -> current = Locale.forLanguageTag("ur").toString()
else -> current = Locale.getDefault().language
}
return current
}
var lang = getLanguage(countryCodeValue)
var tradOpt = TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.ENGLISH)
.setTargetLanguage(TranslateLanguage.fromLanguageTag(lang).toString())
.build()
I'm integrating my android app with Google analytics v4. I'm in Argentina, so my currency code is "ARS", not "USD". I need to specify the local currency code (of any other country), otherwise it sends wrong information.
For example, the price tag of an article says it costs "9,32 ARS", if I don't specify the currency code it sends "9,32 USD". Thanks
// Send Item
googleTracker.send(new HitBuilders.ItemBuilder()
.setTransactionId(purchase.getOrderId())
.setName(purchase.getPackageName())
.setSku(purchase.getSku())
.setCategory("Coins")
.setPrice(skuDetails.getPriceMicros())
.setQuantity(1)
.setCurrencyCode(????)
.build());
You can try like this
Currency currency = Currency.getInstance(Locale.getDefault());
String currencyCode = currency. getCurrencyCode();
I actually could get it working using this answer here: SOLUTION
Google added to its API a new field which is "price_currency_code", to getSkuDetails, which allows us to get the currency code of a transaction. You must edit SkuDetails.java as this answer shows.
According to the measurement protocol definition for currency code you need to pass a valid ISO 4217 currency code to HitBuilders.ItemBuilder.setCurrencyCode() call. Android provides Currency class that support ISO 4217. You should be able to use it to compose the parameter you need for the setCurrencyCode call.
In android many ways to get local currency.
But the bad thing is, In some cases, we got device-specific issues.
val currency = Currency.getInstance(Locale.getDefault())
currency.currencyCode
currency.symbol
The above code gives the currency code a and symbol, But have an issue, If we change the phone locale, the currency also changed.
We have another way to get local currency, if we change the Phone locale, the local currency is not affected
Currency.getInstance(Locale("",tm.networkCountryIso)).currencyCode.toString()
Currency.getInstance(Locale("",tm.networkCountryIso)).symbol.toString()
I want to add the countries and their corresponding States in android application locally, in such a way that when the user clicks on the country a dialog should come up and it should show a list of country names, and when the person selects a country, in the state dialog it should show the corresponding state names.
I have a list( in xml format ) for all the country and their states but i am unable to use them in my android app and also do not know the way how to use them.
Please help
you can do like wheel demo like iphone default spinner,
I have a link which has the same concept like you want. but yes it has not been entered all the list of country and their state. but on that you can find the way how to get it.
http://code.google.com/p/android-wheel/
For Country and it's state detail you have to find out in google that any default methods provides to get all countries and their states or otherwise you have to add it manually add it by country wise I have the link that has list of country and it's states.
http://en.wikipedia.org/wiki/List_of_countries_by_population
hope it will help you.
i am new to android development,so dint get me wrong. I am developing an application in which a text is generated.which is normally a word. The word is checked in a database for is corresponding value pair. Here the word is the key and its corresponding value is the value .
Since the text is auto generated it sometimes goes wrong(mis spelled). How do i perform a check of auto generated word to match with the mostly matched letters in the database of key word.
example: auto-generated word(key) - value
americ:
america : a country
Here the auto generatd word is americ(key) is not matched since it only contains america in its pair set.it need to be corrected as america.
You are probably using SQLite. Your best bet is soundex, which is desribed here.
Soundex has many shortcomings, but it might get you started. If you want a real measure, then go with Levenshtein distance, which is not built into the database (as far as I know).
When changing the custom locale the label of the phone types change to the appropriate language. Does anybody know how to get the localized label of the phone types?
I pick a contact in my app to get its phone number and if there is more then one number I use an AlertDialog to let the user select the currect one. In this pick list, I want to show the label of the type, so it's easier for the user to select. Since they labels are somewhere in the Android system, it must be possible to get the localized label. Unfortunately, the Phone.LABEL is null when reading the phone number.
I know this is a bit old, but this:
Phone.getTypeLabel(this.getResources(), cursor.getInt(typeIdx), "");
worked for me
Yes, you can get localized phone type string with the code:
int phoneNumberType = (int)pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
ContactsContract.CommonDataKinds.Phone.getTypeLabel(context.getResources(), phoneNumberType , "")
but for custom phone types you should cosider phone label, not only phone type:
String phoneLabel = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
Inferno's answer is a valid answer, and I was happy to find this answer because it was similar to what I was looking for. However, if you're dealing with phones installed with API Level 5 (Android 2.0) or newer, there is one small problem with this: android.R.array.phoneTypes only returns the list of phone types that were present prior to when ContactsContract class replaced the Contacts interface as of API Level 5. I verified the labels listed when creating a new contact on emulators running these Android versions (API Levels): 1.6 (4), 2.1-update 1 (7), and 2.2 (8).
When printed out, android.R.array.phoneTypes contains these valid phone types:
Home, Mobile, Work, Work Fax, Home Fax, Pager, Other, Custom
These are the valid phone types, present for phones with Android 2.0+ installed, that are missing from that same Array:
Callback, Car, Company Main, ISDN, Main, Other Fax, Radio, Telex, TTY TDD, Work Mobile, Work Pager, Assistant, MMS
Unfortunately, I have not been able to find something like android.R.array.phoneTypes that'll list all of these valid phone types for phones Android 2.0+. Has anyone come across such yet?
References
android.R.array.phoneTypes defined: http://developer.android.com/reference/android/R.array.html#phoneTypes
Note: I'm posting my other two reference links in separate answers, as I can't seem to post more than one hyperlink per post at this time.
i am using this piece of code
public void getPhoneType(){
int res;
for(int i=0;i<=20;i++){
res = ContactsContract.CommonDataKinds.Phone.getTypeLabelResource(i);
Log.d(TAG,"i: "+ i +" type: " + context.getString(res));
}
}
didn't found any place to get the actual count of valid types but http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Phone.html#getTypeLabelResource%28int%29 says it will always give a valid res so, you can iterate until it start giving repeated values... to me after 20 gives me the custom res.