Vivek Kalkur
Vivek Kalkur

Reputation: 2186

How to set a different language to the ListContents in my ListView

I have two simple ListViews:

public class SimpleListView extends Activity {
private ListView lv1 = null;
private ListView lv2 = null;
private String s1[] = {"a", "b", "c", "d", "f", "g","h","i"};
private String s2[] = {"j", "k", "l", "m", "n", "o","p","q"};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);         
    lv1 = (ListView) findViewById (R.id.list1);
    lv2 = (ListView) findViewById (R.id.list2);

    lv1.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s1));
    lv2.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s2));

}  

} 

Here I want to list-contents i.e. s1 and s2 to be of different language instead of a,b,c..
How can I do that?

Thanks

EDIT:

According to language support in android I came to know that I can set different language to the TextViews.Now I want to set the language to the list-contents.

Upvotes: 1

Views: 1347

Answers (2)

Michell Bak
Michell Bak

Reputation: 13252

You should use localized strings.xml files.

Your res/values folder contains a strings.xml file.

By creating additional values folders with a translated strings.xml file, you can cover all the regional languages you want.

Here are some examples:

  • res/values (for English)
  • res/values-de (for German)
  • res/values-zh (for Chinese)
  • res/values-es (for Spanish)
  • res/values-no (for Norwegian)
  • res/values-da (for Danish)

All the folder must contain a strings.xml file with the translated strings. If your original strings.xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">English title</string>
</resources>

... then it could be like this for the German version:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">German title</string>
</resources>

Using string resources

If you want to get a string from the strings resources, then here's how you do.

In code: getString(R.strings.app_name) // returns "English title" for English users, "German title" for German users, etc.

In layout: @string/app_name

More info

The Android developer site has an excellent guide on the subject here:

http://developer.android.com/guide/topics/resources/localization.html

Upvotes: 1

Xavjer
Xavjer

Reputation: 9264

Well if you're asking how to find out what the active language on the device is look here Get the current language in device then fill your s1 and s2 according to the language

Upvotes: 2

Related Questions