The application must specify the default language.

Source: Internet
Author: User

The Android Application is still well internationalized. By setting the name of the resource file, for example, values-ZH-RCN, the system will automatically set it based on the language set by the current system, by default, resource files under the values directory are selected. If an application needs to specify the default language, no matter what language the system sets, the default language will be displayed. How can this problem be solved? In fact, it is very simple. It only takes three steps.

Step 1: Write a class (functionapplication) to inherit the application and implement the following methods in the oncreate () method:

?
1234567891011 @Overridepublic
void
onCreate() {    super.onCreate();    String languageToLoad  =
"zh";    Locale locale =
new Locale(languageToLoad);    Locale.setDefault(locale);    Configuration config = getResources().getConfiguration();    DisplayMetrics metrics = getResources().getDisplayMetrics();    config.locale = Locale.SIMPLIFIED_CHINESE;    getResources().updateConfiguration(config, metrics);}

Step 2: Set the default androidmanifest. xmlapplication to the following:

?
12345 <application    android:name=".FunctionApplication"    android:icon="@drawable/ic_launcher"    android:label="@string/app_name"    android:configChanges="locale"
>

Android: configchanges = "locale" is the key. Of course, if you do not allow the entire application to be programmed with Chinese characters, you do not need to inherit the application, simply add the first method in the class that inherits the activity. Of course, do not forget to add Android: configchanges = "locale" to the activity tag ".

Step 3: add the corresponding permissions:

?
1 <uses-permission
android:name="android.permission.CHANGE_CONFIGURATION"/>

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.