Piyush Piyush - 5 months ago 10
Android Question

Changing locale within the app effectively

Following this answer, I'm using the following code in the

onCreate()
method of each activity:

Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.setLocale(newLocale);
res.updateConfiguration(conf, dm);


But this does not effectively update the configuration. The following problems need to be resolved:


  1. The layout direction of the
    dialogs
    seems to be overridden by the system, which should have been handled by
    conf.setLocale(newLocale)
    .

  2. The same is with some widgets such as
    TimePicker
    .

  3. Numbers in
    NumberPicker
    and
    TimePicker
    are overridden as well.



What should be the effective approach here?

Answer

Seems a bit too early to post an answer to my question, but adding Locale.setDefault(newLocale) to the code above solves all the three problems mentioned.

Comments