TheO
TheO

Reputation: 667

Multiple langues with textView and setText

I use a ImageAdapter to set a custom view with images and a caption. How can I support multiple langues for this text?

The text is now wet in the getView(...) method in the imageAdapter:

public View getView(int position, View convertView, ViewGroup parent) {
        View v;
        if (convertView == null) {
            LayoutInflater li = getLayoutInflater();
            v = li.inflate(R.layout.activity_main_icon, null);

            TextView tv = (TextView)v.findViewById(R.id.main_icon_text);
            tv.setText("**MENU TEXT**");

            ImageView iv = (ImageView)v.findViewById(R.id.main_icon_image);

            iv.setImageResource(mThumbIds[position]);
        } else {
            v = convertView;
        }

        return v;
    }

I guess the "MENU TEXT" should be set dynamically from res/Strings to support different langues, but how can I do this?

The GridView has four images that should have four different strings. E.g. "Add friend", "find friend", "Edit friend" and "Delete friend".

Upvotes: 0

Views: 824

Answers (3)

EJK
EJK

Reputation: 12527

First put the string value in a resource file: res/values/strings.xml

<string name="menu_text">menu text</string>

Then resolve the string at runtime:

String menuText = context.getString(R.string.menu_text);

Then you can use foreign-language versions of the strings file for translations: e.g. res/values-fr/strings.xml

Upvotes: 2

noni
noni

Reputation: 2947

I recommend you to read this training:

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

Then, your code should look like this:

view.setText(getResources().getString(R.string.YOURSTRINGKEY));

Upvotes: 2

Edison
Edison

Reputation: 5971

You will simply use the TextView.setText(int) varient (e.g. R.string.menu_text_hello) and put the strings into the respected res folders.

Upvotes: 0

Related Questions