Android Learning – Android app language switching function

Source: Internet
Author: User

Features:

App users set the app language according to their language preferences. The language setting is for this app only and retains the previous startup settings the next time the app is launched.

Update language:
Public Static voidChangeapplanguage(ResourcesResources, StringLanatr) { ConfigurationConfig=Resources.GetConfiguration(); DisplaymetricsDm=Resources.Getdisplaymetrics(); If (Lanatr.equals("Ru_ru")) {Config.Locale= New Locale("Ru", "RU"); } Else If (Lanatr.equals("en_US")) {Config.Locale= Locale.中文版; } Else If (Lanatr.equals("PT")) {Config.Locale= New Locale("PT"); } else { Config. Locale = locale. getdefault ()  } Resources. Updateconfiguration (config , Dm              
Sharepreferences Deposit Settings Language:
Sharences sharedpreferences = ().  ().  (),  0< Span style= "color: #666600;" >); sharedpreferences. Edit ().  ( ,  Lanatr). commit ();        

After the language is updated, the language settings do not take effect for activity that has previously appeared and is still alive. You can make the language effective in time by restarting the corresponding activity.

Mainactivity is the activity you want to restart.Itaddflagsintent. flag_activity_clear_top); It. Addflags (intent.< Span style= "color: #000000;" >flag_activity_new_task); Getactivity (). Startactivity (it

Note:

  • Generally speaking, from the user experience point of view, the language setting function entrance will be placed in the first few layers of the app, if the entrance is too deep, so that users can not quickly find the language settings portal, and if you want to say the application restart, user behavior operation record will be more troublesome.
  • There are several ways to restart a corresponding activity:
    • If a user enters a language setting that requires too many levels, or other actions that the app wants to save before manipulating the language setting, that can be broadcast by broadcasting (Sendbroadcast ()), when the language changes, and all activity is received after the broadcast ( Broadcastreceiver), all restart operation;
    • If you allow the user to set the language, the app goes back to the home directory, which is a lot simpler, just call the restart () method above.
Restart Singletask activity: If your startup activity is Singletask, restart it as above, the language will not take effect. What is the situation like? can be resolved by understanding, using its life cycle, when the activity of the Singletask property is cut back, the activity invokesonnewintent () method. You can override this method. Here is a workaround, finish yourself first, and then restart yourself.

IntentI= New Intent(This, mainactivity. class); Startactivityi); //overridependingtransition (0, 0); } else { //other logical } Conclusion: The app language settings are not complex, mainly how to set the language settings in time, involving activity restart, broadcast and so on.

Android Learning – Android app language switching function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.