Force a right orientation android - java

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.

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

Not Able to Change Webview Language

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.

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");

OnConfigurationChanged is not called after locale config change

What I want to do is to provide a simple language change for the user within the app. The text of the current view as well as the Back Stack should be replaced with the strings of the selected language. I have just written the following code snippet which does his job fine:
private void setLocale(Locale locale) {
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
After some researching I figured out, that every activity should automatically refreshes itself after the locale configuration has been changed - but it doesn't in my case. The current view as well as the whole Back Stack have still the default locale - they just change after reopening the activity.
I have already added the attribute android:configChanges="locale|layoutDirection" to my AndroidManifest.xml so that won't do the trick.
Do you guys have any suggestions? Or do I have the wrong purpose for solving this problem eitherway?
EDIT
Okay it seems like OnConfigurationChanged() only triggers by changing the language device-wide in the android settings?
Is there any other way to refresh EVERY active activity (current and back stack) after changing the language in my app?
I think if you create a Receiver who has this event filter
<action android:name="android.intent.action.LOCALE_CHANGED"/>
will give you an event when device locale is changed then you can refresh all activity you want. For refreshing you can look into this.
Hope it will solve your issue.

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.

Categories