Nostradamus Nostradamus - 6 months ago 20x
Java Question

NoSuchMethodError Exception with SetLocale

In Xamarin, I have created the following extension to change locale on the fly:

public static void ToEnglishLocale(this Activity activity)
Locale locale = new Locale("en-US");
Configuration config = new Configuration();
activity.BaseContext.Resources.UpdateConfiguration(config, activity.BaseContext.Resources.DisplayMetrics);

I am targeting API 15 an up and it fails with the following exception on API 15:

Unhandled Exception:

Java.Lang.NoSuchMethodError: no method with name='setLocale' signature='(Ljava/util/Locale;)V' in class Landroid/content/res/Configuration;

Is there any other "unified" way to change locale on the fly?


The method SetLocale was added in api level 17 so that's why you are getting the error.

You can use Android.OS.Build.Version.SdkInt property to find api version at runtime and call the method only if it is Android.OS.Build.VERSION_CODES.JellyBeanMr1 or newer. If it isn't you should set the Locale public property instead of calling SetLocale