Detailed Android Change app language mode implementation process _android

Source: Internet
Author: User
Tags locale

One, the effect chart

Second, describe

Change the language in the Android project, which works only for this app and does not work on the entire system

Third, the solution

(i) Layout files

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
  xmlns:tools= "http:// Schemas.android.com/tools "
  android:layout_width=" match_parent "
  android:layout_height=" Match_parent "
  android:orientation= "vertical"
  android:padding= "20DP" >

  <textview
    android:layout_ Width= "Wrap_content"
    android:layout_height= "wrap_content"
    android:text= "@string/hellow"

  /> <button
    android:layout_width= "wrap_content"
    android:layout_height= "wrap_content"
    android:o nclick= "Changelanguage" 
    android:text= "language switching"/>

</LinearLayout>

(ii) mainactivity home page

Package com.example.chinesepage;

Import Java.util.Locale;
Import android.app.Activity;
Import android.content.Intent;
Import android.content.res.Configuration;
Import android.content.res.Resources;
Import Android.os.Bundle;
Import Android.util.DisplayMetrics;
Import Android.view.View;

Import Android.widget.Toast; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {Super
    . OnCreate (Savedinstancestate);
  Setcontentview (R.layout.activity_main); /** * Click the button, change the language * * @param view */public void changelanguage (view view) {resources = g
    Etresources (); Configuration Configuration = Resources.getconfiguration ();  Gets the resource configuration if (Configuration.locale.equals (Locale.china)) {//Determines whether the current language is Chinese configuration.locale = locale.english; Sets the current language configuration to English} else {configuration.locale = Locale.china;//Set the current language configuration to Chinese} displaymetrics metrics =
    New Displaymetrics (); Resources.uPdateconfiguration (configuration, metrics); Update configuration file Sendbroadcast (new Intent ("language"));
Send the broadcast and reopen the activtiy to reinitialize the interface language after the broadcast is accepted. Intent Intent = new Intent (mainactivity.this, Mainactivity.class); Alternatively, you can jump directly to mainactivity//Intent.setflags (intent.flag_activity_no_animation);
    Remove the jump from the animation, so that the user does not seem to jump the feeling//startactivity (intent);
  Finish (); }
}

(iii) Changereceiver broadcast class

Package com.example.chinesepage;

Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;

/**
 * Custom Broadcast language changes restart activity
 * 
 * @author ASUS * */Public
class Changereceiver extends Broadcastreceiver {
  private Intent mintent;

  @Override public
  void onreceive (context, Intent Intent) {
    mintent = new Intent (context, Mainactivity.class);
    Mintent.addflags (intent.flag_activity_new_task);
    Context.startactivity (mintent);
  }

(iv) Create Values-en folders under Res, copy string.xml, and change the Chinese into English to achieve internationalization.

Values/strings.xml

<resources>

  <string name= "app_name" > Language switching </string>
  <string name= "Hello_world" > Hello, world!</string>
  <string name= "action_settings" > Settings </string>
  <string name= "Hellow" > Hello </string>

</resources>

Values-en/strings.xml

<resources>

  <string name= "app_name" >ChinesePage</string>
  <string name= "Hello_world" >hello world!</string>
  <string name= "action_settings" >Settings</string>
  < String Name= "Hellow" >Hellow</string>

</resources>

(v) Registration of broadcasts (don't forget this ~)

 <receiver android:name= "Com.example.chinesepage.ChangeReceiver" >
      <intent-filter>
        <action Android:name= "Language"/>
      </intent-filter>
    </receiver>

Summarize

The above is the details of the Android Change app language model of the implementation process of the entire content, I hope that the development of Android to help you, if there are questions welcome to the discussion.

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.