Programmatically switch resource files

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Programmatically switch resource files

Postby Pich » Thu Nov 26, 2009 12:19 am

Hi,

Is there any way to programmatically switch resource files?
To clarify, I have to strings.xml:
res/values/strings.xml
res/values-sv/strings.xml

Since swedish (sv) is not a supported Locale yet I am not sure on how to switch between these files. If it was a supported Locale i would been done automaticly but now instead I use a settings view where the user can select which language to use. When the user saves the settings I would like to, in code, switch to correct strings.xml. Is that possible?

Best regards

Pich
Pich
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed Nov 25, 2009 8:30 pm

Top

Postby Kur1310 » Thu Nov 26, 2009 11:42 am

Hey u have settings.xml file in diff. folders according to the locale.Right?
Besides that, u can also have all the xml files for different languages in single folder values/ , by giving names like settings-sv.xml,settings-gr and so on....
Now u can refer any settings file of your choice by giving its unique name.... :?
-Kur
Kur1310
Junior Developer
Junior Developer
 
Posts: 20
Joined: Wed Oct 28, 2009 10:50 am

Postby Pich » Thu Nov 26, 2009 12:30 pm

Kur1310 wrote:Hey u have settings.xml file in diff. folders according to the locale.Right?
Besides that, u can also have all the xml files for different languages in single folder values/ , by giving names like settings-sv.xml,settings-gr and so on....
Now u can refer any settings file of your choice by giving its unique name.... :?


Ok, but does that really help me? Say I have a simple TextView like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. <TextView
  3.  
  4.         android:id="@+id/my_id"
  5.  
  6.         android:text="@+string/my_text"
  7.  
  8.         android:layout_height="fill_parent"
  9.  
  10.         android:layout_width="fill_parent"/>
  11.  
  12.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


And these two resource files:
Code: Select all
res/values/strings.xml   
    <string name="my_text">My english text</string>
res/values/strings-sv.xml   
    <string name="my_text">My swedish text</string>


How do I make a switch between the resource files?

/Pich
Pich
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed Nov 25, 2009 8:30 pm

Postby sandis84 » Mon Nov 30, 2009 4:45 pm

Im also looking for the answer to this question. An answer would be greatly appriciated!

But why are you using the ISO 3166-1 alpha-2 country code for El Salvador instead of the one for Sweden? If I were you I would use SE instead of SV, to not confuse the poor El Salvadorians.

http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#SV

Cheers,
Per Sandström

edit: some googling provided me with an answer. Unfortunately it does not cover what I am looking for, but I will create a thread about it tomorrow.

Resources resource = super.getResources();
Configuration cf = resource.getConfiguration();
DisplayMetrics dm = resource.getDisplayMetrics();
cf.locale = new Locale(”de”);
resource.updateConfiguration(cf, dm);
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Postby Pich » Mon Nov 30, 2009 9:25 pm

Thanks a lot!

It worked great.

Best regards (Vänliga hälsningar)

Pich
Pich
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed Nov 25, 2009 8:30 pm

changing Locale (updateConfiguration(...)) on Milestone make

Postby adrianvintu » Mon Jan 18, 2010 11:14 am

Hi all,

There is a "bug" when changing the Locale on the Motorola Milestone. The font gets smaller each time the updateConfiguration(...) is called.

See here an article on this and the fix: http://adrianvintu.com/blogengine/post/Force-Locale-on-Android.aspx

BR,
Adrian Vintu
adrianvintu
Freshman
Freshman
 
Posts: 8
Joined: Wed Oct 28, 2009 12:07 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests