Multi-language switching for Android apps

Source: Internet
Author: User
Tags locale

There are several ways to implement multi-language switching for Android applications, and there are a lot of information on the web, and there is a simple and effective way.

The steps are as follows:

Create a new folder in the resource file Values-en, Create a new Strings.xml, copy the contents of the Strings.xml in the default values folder to Strings.xml in Values-en, and then convert the Chinese content in the folder to English, for example <string name = "Set" > Set </string> Convert to <string name= "set" >set</string>

Then add a method to the main activity

The code is as follows:

public void Switchlanguage (locale locale) {
Configuration config = Getresources (). GetConfiguration ();//Get Set Object
Resource resources = Getresources ();//Get Res Object
Displaymetrics DM = Resources.getdisplaymetrics ();//Get screen parameters: mainly resolution, pixels, etc.
Config.locale = locale; Chinese Simplified
Resources.updateconfiguration (config, DM);
}

It is then called in the OnCreate function (and must also be called in the oncreate function ).

For example:

Switchlanguage (Locale.getdefault ()); or Switchlanguage (locale.us);

Finally, it is important to note that there are two places to configure in the Androidmanifest.xml file:

1. Add this attribute to the corresponding activity node: android:configchanges= "Locale"

2. Add Permission: <uses-permission android:name= "Android.permission.CHANGE_CONFIGURATION"/>

Multi-language switching for Android apps

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.