Not Able to Change Webview Language - java

I have a Webview in my Activity and everytime whichever site i open it loads the site in an indian language whereas i always want the user to choose language or atleast set the default language to English
Tried
setLocale(Locale.ENGLISH);
public void setLocale(Locale locale){
Resources resources = getApplicationContext().getResources();
Configuration configuration = resources.getConfiguration();
Locale.setDefault(locale);
configuration.setLocale(locale);
getApplicationContext().getResources().updateConfiguration(configuration,
resources.getDisplayMetrics());
}
And
Locale.setDefault(...);
Locale.getDefault(...);
I even tried https://stackoverflow.com/a/40675539/12004834 this answer but the sites are still loading in other languages.

Related

Setting Locale Not applying to custom dialogue

I have app in which I have below code in base activity.
String CurrentLang = sessionManager.getStringValue("UserLang");
Locale locale = new Locale(CurrentLang);
Locale.setDefault(locale);
Configuration config = new Configuration();
if (Build.VERSION.SDK_INT >= 24) {
config.setLocale(locale);
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
} else {
config.locale = locale;
getBaseContext().getApplicationContext().createConfigurationContext(config);
}
Its working fine in fragment and activity etc, However in custom dialogue, its showing default language. If user change language from setting menu, app restarting with base activity and its start showing custom language even in dialogue but when I remove application from recent app and start again, its showing again default language only for custom dialogue, other place its showing fine, Let me know if someone can help me for solve the puzzle. Thanks!
Try this
Resources resources=getResources();
DisplayMetrics dm=resources.getDisplayMetrics();
Configuration conf=resources.getConfiguration();
conf.setLocale(new Locale(language/*e.g: en for english, ar for arabic ....etc*/));
resources.updateConfiguration(conf,dm);
Then you have to restart your layout

Force a right orientation android

I built an app that's completely in Arabic..
I need the app to run as if the device language is Arabic no matter what language is already set, since the app will only be used be native Arab..
this gives so many advantages in the design such as.. titles in Dialog Alert is to the right, navigation drawer is to the right..etc
Actually it's weird the other way..
does forcing a locale is the solution?
if yes, do I have to create new resources files, or the app will read the default ones but reformat them according to the locale?
how to do this..
thanks in advance
If your app only for Arabic, You can convert your activity to RTL by,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
If your app can be for all language, you can do as per selected locale,
Resources resources = getResources();
Locale locale = resources.getConfiguration().locale;
Configuration config = resources.getConfiguration();
config.locale = locale;
if (Build.VERSION.SDK_INT >= 17) {
config.setLayoutDirection(locale);
}
resources.updateConfiguration(config, resources.getDisplayMetrics());
Declare android:supportsRtl=”true” in the application tag of AndroidManifest.xml
You have to just declare android:supportRtl="true" in the AndroidManifest.xml file to support right to left layout.

How can I change language settings in android studio on a button click.

I only want to change the language in the app. I also want to know how to set a default strings.xml file as main language.
I have seen lots of questions like mine on the internet, but they seem to be a bit outdated.
by this define your default language when it starts
Configuration mainConfig = new Configuration(getResources().getConfiguration());
String languageToLoad = "en";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
mainConfig.setLocale(locale);
getResources().updateConfiguration(mainConfig, null);
and you can change it again and save selected language in shared preferences
new SharedPreferencesUtils(this).setPreferenceStrings("selected_language", "en");

getString() only returns english values

Here is the code of my onActivityResult method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
if (contents.length() == 26) {
BillBarcode barcode = new BillBarcode(contents);
edtBillId.setText(barcode.extract(BillBarcode.BarcodePart.BillId));
edtPaymentId.setText(barcode.extract(BillBarcode.BarcodePart.PaymentId));
launchService();
} else {
Dialog dialog = new DialogBuilder()
.setTitle(getString(R.string.dialog_title_global_error))
.setMessage(getString(R.string.unknown_barcode))
.build(getActivity());
dialog.show();
}
}
}
The problem is that getString(R.string.dialog_title_global_error) and getString(R.string.unknown_barcode) always returns english value while I have Farsi value too and the locale is farsi too.
The problem only exist in this method.
Farsi value:
<string name="unknown_barcode">بارکد قابل استفاده نیست.</string>
English value:
<string name="unknown_barcode">Unknown barcode</string>
EDIT
I have a setting page and set my locale when user selects persian from language page by this code:
String languageToLoad = "fa";
Resources res = context.getResources();
// Change locale settings in the app.
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale(languageToLoad);
Let me try to round up all the comments in an answer. Your problem is a combination of two implementation mistakes:
You are setting the locale of the current Activitys context programmatically. However you are doing so in an unsupported way that may yield incorrect results.
When your activity gets a result from another activity in OnActivityResult(), your Activity either gets restarted completely or the context configuration gets reset to the system's default locale. Either way, the locale you set up in the settings dialog is lost.
Solutions
The proper way of changing the App's locale locally is outlined here: Changing Locale within the app itself.
In particular, while just changing the locale in the configuration class may work for you, it is clearly not the correct way to change an application's locale. Doing it properly requires a little more work:
Locale locale; // set to locale of your choice, i.e. "fa"
Configuration config = getResources().getConfiguration();
config.setLocale(locale); // There's a setter, don't set it directly
getApplicationContext().getResources().updateConfiguration(
config,
getApplicationContext().getResources().getDisplayMetrics()
);
// you might even need to use getApplicationContext().getBaseContext(), here.
Locale.setLocale(locale);
Setting the locale on the application context should survive the restart of an activity, but as per Android's lifecycle guarantees, you should not assume that the locale stays around.
If you really need the ability to locally change the locale of your app, you should save the user specified locale (e.g. in a SharedPreference) and obtain and re-set it when the app or your activity is restarted (i.e. at least in OnCreate()). Remember that Android is free to save and restart your activities at any time outside of your control and it is your responsibility to handle the restart gracefully.

App name doesn't change language after user changes it programmatically

I give my user the opportunity to change the app language. I use this:
public void languageToLoad(String language) {
String lang = language;
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
for english: languageToLoad(en) or french: languageToLoad(fr)
But my problem is that the app name won't change.
strings.xml (en):
<string name="app_name">SoccerPro</string>
strings.xml (fr):
<string name="app_name">FootballPro</string>
Every string changes its value but not 'app_name'.
If the device language gets changed then it changes but not like my way. What am I doing wrong?
Your call to updateConfiguration() affects your process at most. It does not affect other processes. Which means that unless your app is the home screen, the home screen will not change. updateConfiguration() does not affect the user's language at a system level, and AFAIK there is no way for an ordinary SDK app to do that.

Categories