Android App language switching feature

Source: Internet
Author: User

Because most of the company's products are sold abroad, so the leader requires the app has a language switch function. I searched the internet for some relevant knowledge and realized the function, here to do in the English switching demo record down.

First look at the demo effect:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/84/4F/wKiom1eMiBzhgdUqAB0BiAv51Ng877.gif "title=" Demo.gif "alt=" Wkiom1emibzhgduqab0biav51ng877.gif "/>

This is the effect. Of course, can also be made in other languages, depending on the needs of the switch.


The principle is actually very simple, is the multiple strings.xml to switch and then refreshes the activity.

First, add android:configchanges= "locale" to each activity in the Androidmanifest.xml file that needs to switch languages.


Then add the values file for the corresponding language under the Res folder:

For example, Chinese Simplified is VALUES-ZH-RCN, English is values-en.

Of course, there are other languages, here I only do two commonly used to see the language abbreviations, countries and languages abbreviation. The principle is the same.


Values-zh-rcn/strings.xml:

<resources> <string name= "app_name" >LanguageDemo</string> <string name= "Chinese" > Chinese </ string> <string name= "中文版" > English </string> <string name= "Red" > Red </string> <string N Ame= "Orange" > Yellow </string> <string name= "Blue" > Blue </string> <string name= "green" > Green </ string> <string name= "purple" > Purple </string></resources>


Values-en/strings.xml:

<resources> <string name= "app_name" >LanguageDemo</string> <string name= "Chinese" >chinese& lt;/string> <string name= "中文版" >English</string> <string name= "Red" >Red</string> &L t;string name= "Orange" >Orange</string> <string name= "Blue" >Blue</string> <string name= "Gree N ">Green</string> <string name=" Purple ">Purple</string></resources>


Layout file:

<?xml version= "1.0"  encoding= "Utf-8"? ><relativelayout 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:paddingbottom=" @dimen/activity_vertical_margin "     android:paddingleft= "@dimen/activity_horizontal_margin"     android:paddingright= "@ Dimen/activity_horizontal_margin "    android:paddingtop=" @dimen/activity_vertical_margin "     tools:context= "Com.lg.languagedemo.MainActivity" >    <button         android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"          android:onclick= "Chinese"          android:text= "@string/chinese"  />    < Button        android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"          android:layout_alignparentright= "true"         android:onclick= " 中文版 "        android:text=" @string/english " />     <linearlayout        android:layout_width= "Wrap_content "        android:layout_height="      "Wrap_content"     android:gravity= "Center"         android:layout _centerinparent= "true"         android:orientation= "vertical" >         <textview             Android:layout_width= "Wrap_content"              android:layout_height= "Wrap_content"              android:text= "@string/red"             android: Textcolor= "@android: Color/holo_red_dark"              android:textsize= "16SP"  />        <TextView             android:layout_width= "Wrap_content"              android:layout_height= "Wrap_content"              android:layout_margintop= "10DP"              android:text= "@string/orange"              android:textcolor= "@android: Color/holo_orange_dark"              android:textsize= "16SP"  />         <textview            android:layout_width = "Wrap_content"             android:layout_height= " Wrap_content "            android:layout_margintop= "10DP"             android:text= "@string/blue"             android:textcolor= "@android: Color/holo _blue_dark "            android:textsize=" 16SP "  />        <textview             android:layout_width= "Wrap_content"              android:layout_height= "Wrap_content"              android:layout_margintop= "10DP"              android:text= "@string/green"             android: Textcolor= "@android: Color/holo_green_dark"              android:textsize= "16SP"  />        <TextView             android:layout_width= "Wrap_content"              android:layout_height= "Wrap_content"              android:layout_margintop= "10DP"              android:text= "@string/purple"              android:textcolor= "@android: Color/holo_purple"              android:textsize= "16SP"  />    </ Linearlayout></relativelayout>


Finally, the core code for switching languages:

private configuration configuration;private displaymetrics displaymetrics;private  resources resources; @Overrideprotected  void oncreate (bundle savedinstancestate)  {     super.oncreate (savedinstancestate);     setcontentview ( R.layout.activity_main);     resources = getresources ();//  Get Res Resource object      configuration = resources.getconfiguration ();//  Get Set Object      displaymetrics = resources.getdisplaymetrics ();} Chinese Public void chinese (View view)  {    configuration.locale =  locale.simplified_chinese;    resources.updateconfiguration (configuration,  Displaymetrics);     startactivity (New intent (Mainactivity.this,mainactivity.class));     finish ();} English public void english (View view)  {    configuration.locale = Locale.US;     Resources.updateconfiguration (Configuration, displaymetrics);     startactivity (new  intent (Mainactivity.this,mainactivity.class));     finish ();}

Of course, refreshing the page is more than this one method can be used OnCreate (), but this method is more restrictive. and recreate (), using this method, the screen will flash a bit.


Then source address: http://down.51cto.com/data/2229088

If you like my article, follow my blog!

This article is from the "Android Development column" blog, be sure to keep this source http://liuyvhao.blog.51cto.com/11690759/1827424

Android App language switching feature

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.