Metalloid66
Metalloid66

Reputation: 119

Setting a locale for textviews in fragment

I want to set locale for a single text view . Which means only that textview will be shown in another language (in my example french) but unfortunately it is not working. While I do not get an error, nothing happens.

 Locale locale = new Locale("fr");
 maintext.setTextLocale(locale);
tried updating confguirations , same thing . Nothing changes .

 Locale locale = new Locale("fr");
Configuration config = new Configuration();
config.locale = locale;
getActivity().getResources().updateConfiguration(config,
    getActivity().getResources().getDisplayMetrics());
maintext.setTextLocale(locale);

I tried doing that in normal activities and it worked perfectly fine .

Upvotes: 4

Views: 2265

Answers (2)

Khalid ElSayed
Khalid ElSayed

Reputation: 278

You can use String.format() with Locale.FRENCH to localize the text of that TextView.

example

String txtFrench = String.format(Locale.FRENCH, "%s", item.getFrenchName());
mFrenchTextView.setText(txtFrench);

Upvotes: 2

jagsaund
jagsaund

Reputation: 2387

Setting the text locale via setTextLocale only specifies the type of font to use to render the text. The locale is applied directly to the underlying Paint object used to render the font on the screen. Here's the documentation for the call: http://developer.android.com/reference/android/graphics/Paint.html#setTextLocale(java.util.Locale)

Changing the locale directly through the Configuration file will apply the locale change across the entire application. This is probably not what you want.

You will have to explicitly specify the french text to use for that view.

Upvotes: 0

Related Questions