multi-language in runtime

Put your problem here if it does not fit any of the other categories.

multi-language in runtime

Postby landy » Mon Jan 30, 2012 8:16 am

Dear all,
please help me change language in run-time, I have menu setting to change language in application. But when i choose other language when I rotate to landscape form , this language will be lost.How can i keep language after rotation ?

this is code I used :
Code: Select all
Resources res = v.getResources();
      // Change locale settings on the device
      DisplayMetrics dm = res.getDisplayMetrics();
      android.content.res.Configuration conf = res.getConfiguration();
      conf.locale = new Locale("de");
      res.updateConfiguration(conf, dm);
           Intent intent = new Intent(this, mainActivity.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      startActivity(intent);


Thanks and best regards,
landy
landy
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed Nov 23, 2011 11:26 am

Top

Re: multi-language in runtime

Postby Phyll » Mon Jan 30, 2012 12:48 pm

Hi landy,

The easiest thing to do is capture the configuration changes (like vertical to horizontal or the hard keyboard) and manage them yourself. The reason it causes trouble is because when this happens the activity is pretty much created again.

This goes in your manifest entry for the activity:

Code: Select all
android:configChanges="orientation|keyboard"


Then put this in the activity:

Code: Select all
    @Override
    public void onConfigurationChanged(Configuration _newConfig) {
        super.onConfigurationChanged(_newConfig);
        //Fix resource based screen elements
        if (_newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
           //Fix orientation here
           }
        if (_newConfig.keyboardHidden == Configuration.KEYBOARDHIDDEN_NO) {
           //Set for available keyboard here
           }
        }//onconfigurationchanged


You don't really have to do anything in this method, it just keeps it from doing what it usually does.

You could also make the selection a permanent change that is read its value in during the onCreate or onResume from persistant data. That will also fix it. Make your selection in a preferences editor and always read the preferences in the onCreate for instance.

The third thing you can do is make it always in portrait mode by adding this to the activity in the manifest:

Code: Select all
android:screenOrientation="portrait"


Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: multi-language in runtime

Postby landy » Tue Jan 31, 2012 11:02 am

Dear Phyll,
You're right when set android:screenOrientation="portrait" ,but my form have to display with landscape ,so i can not change it to portrait.How can i keep language in this case ?

Thanks for your help,
landy
landy
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed Nov 23, 2011 11:26 am

Re: multi-language in runtime

Postby Phyll » Tue Jan 31, 2012 9:27 pm

Hi landy,

I think you need to store the language choice and put it back if the orientation changes. Store it in preferences or a file when the choice is made and then read that choice back if something happens or when they start the app again. That takes care of every circumstance.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: multi-language in runtime

Postby landy » Fri Feb 03, 2012 5:11 am

Thanks Phyll
landy
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed Nov 23, 2011 11:26 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 32 guests