0

I want to use multi language in my app but cant solve problem with array; I couldn't define text from XML here. So what do you advise me to do here so that i can use multi language ? thanks a lot

public static final String[] columns = { "Bluetooth", "WiFi",
"Mobile Networks", "Auto Sync", "Gps", "Auto-rotate screen",
"Vibrate on touch", "Airplane mode", "Brightness", "Sleep",
"Volume Settings", "Phone Ringtone", "Uninstall",
"Backup & Restore", "Battery Usage", "Cache Clear", "System Clear",
"System Info" };
}
riteshtch
  • 8,629
  • 4
  • 25
  • 38
theroverx
  • 11
  • 2
  • 1
    http://developer.android.com/guide/topics/resources/string-resource.html#StringArray – kris larson Mar 01 '16 at 10:50
  • I try this but get that error = getResources unknown and advise to creat a new method – theroverx Mar 01 '16 at 15:15
  • You can call getResresources() from any class that exteds from context (for example an Activity). if you are not in one, than pass the context to the class in the contructor like http://stackoverflow.com/questions/8238588/how-to-call-getresources-from-a-class-which-has-no-context – bendaf Mar 03 '16 at 15:30

2 Answers2

2

You can define string array in strings.xml, as the documentation says like:

<resources>
    ...
    <string-array name="numbers">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
        <item>5</item>
        <item>6</item>
        <item>7</item>
        <item>8</item>
        <item>9</item>
        <item>10</item>
    </string-array>
    ...
</resources>

Than you can get it from code like :

String [] fiilliste = getResources().getStringArray(R.array.numbers); 

Don't forget to define your array in every strigns.xml in each of your values-XX folder which you want to support.

bendaf
  • 2,981
  • 5
  • 27
  • 62
  • 1
    You literally beat me to it by seconds! Only thing I would add is that if the those `columns` are for a database's columns this will break things, therefore there needs to be a better separation of concerns. – Graham Smith Mar 01 '16 at 10:57
0

I think the best approach is to use R.string.whateveryouwant as Integer array, something like this:

Integer[] columns = { R.string.bluetooth, R.string.wifi, .... }

And when you display the text get the value from the language xml file, given the Integer key.

Mike-Bell
  • 951
  • 1
  • 11
  • 25