1

Is it possible to manually select a strings.xml file without changing the language of your device?

I would like to have Button to switch between two languages, but all i found is to use the build-in feature of android studio to create different strings.xml, which are selected by your localization.

Anil Meenugu
  • 1,411
  • 1
  • 11
  • 16
snapz
  • 13
  • 5

1 Answers1

0

If you want to use a different strings.xml you have to switch localization. This can be done programmatically but is highly not recommended because you will mess with the system.

You can create a different resource XML file that you can put your strings there, give them a unique id and fetch them in the same way (R.id.your_string)

EDIT:

You create an XML file that you put into /res/values. This file will declare some strings in the same way the strings.xml does. With ids, values and everything. Then from the code what you do is to conditionally change strings. For example :

if (this){ //get the string from strings.xml here (R.strings.your_string else { //get the string from your_file.xml here the same way you did above. {

For more info check the documentation.

George Daramouskas
  • 3,720
  • 3
  • 22
  • 51
  • Thanks for your answer, but i am not sure how to use the new resources file. Do i have to read a string from that file in every TextView or is it possible to globally change the "@string/[..]" to a "@newresource/[...]" – snapz Jun 28 '15 at 10:09
  • Edited my answer please check to see if there is something you do not understand. – George Daramouskas Jun 28 '15 at 10:15
  • Okay, i got it, thank you very much! But there is no way to globally change all strings without explicit changing every TextView, right? Because the strings have to have different names it is not possible to match them i guess. And there is no way to give them the same name. Well, how it's now i could better place everything in the strings.xml without using another resource – snapz Jun 28 '15 at 10:32
  • Well there can be. A simple way I can think of is: Declare a boolean value to work as a flag. And put every `textView.setText()` within a condition that goes `if myFlag==true` do this, else do that. If my answer helped you please accept it, thanks. – George Daramouskas Jun 28 '15 at 10:37
  • i know what you mean and i implemented it like that. works fine, but i hope there is a solution like: two resource files with same string names: one in resources.xml and in the resources2.xml two. In my java code i want to say: use this resources.xml and if(...) use the strings from resources2.xml – snapz Jun 28 '15 at 11:33