Change language on runtime in Android -


i have change language on runtime in android (yes, know not behaviour, requirement...).

so have basic class, every activity extends. class has following function:

public static void changelanguage(context context) {     resources res = context.getresources();     /*      * change locale settings in app.      */     displaymetrics dm = res.getdisplaymetrics();      /*      * store , load data in preferences      */     android.content.res.configuration conf = res.getconfiguration();     string[] localarray = res.getstringarray(r.array.language_short_array);     if (localarray != null) {         sharedpreferences settings = context.getsharedpreferences(                 myservice.app_id, mode_private);         conf.locale = new locale(localarray[settings.getint(                 prefered_language_key, 0)]);         res.updateconfiguration(conf, dm);     } } 

i call method in oncreate:

@override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     changelanguage(this); } 

this in super-class. activities extends , call super.oncreate @ first. after call, set layout , initializes settings...

i thought lines of code make it. have following problem: sometimes, activity changes language , not!

if set debug breakpoint on , after programm pauses press continue, works fine. think, in cases, application "slow enough", language change correctly, whereas language won't change if application fast...

is there solution of problem? how can sure, language change correctly in time?

thanks lot!

edit: here example class extends super-class

public class mainmenuactivity extends baseactivity {  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_start);     } } 

changelanguage(this); needs called when language has changed or when app loaded.. res.updateconfiguration(conf, dm); updates global config , specific app instance not activity instance.

when change locale in activity have recreate activity see language change. can done forcing orientation change forcing this:

setrequestedorientation(activityinfo.screen_orientation_portrait); setrequestedorientation(activityinfo.screen_orientation_landscape); setrequestedorientation(activityinfo.screen_orientation_sensor); 

if hit after language change see old language because oncreate not called. have detect in onresume language changed , force recreate of activity.

-= edit =-

using screen orientation reload activity has proven bit buggy on devices. using reload current activity:

public static void resetscreen(activity activity) {     if (activity != null) {         if (build.version.sdk_int >= 11) {             activity.recreate();         } else {             intent intent = activity.getintent();             activity.overridependingtransition(0, 0);             intent.addflags(intent.flag_activity_no_animation);             activity.finish();              activity.overridependingtransition(0, 0);             activity.startactivity(intent);         }     } } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -