How to change locale and layout dynamically in android? - android

I am developing an app that uses text in 3 languages. English, Kurdish and Arabic. I am trying to change the layouts dynamically when user changes language. I have a settings screen where the user can change language that is I am not changing language from the system settings but from within my app. For example the layout should change from RTL to LTR if I switch from Kurdish to English or vice versa. I tried to use onConfiguterionchanged() but it is not getting triggered.
Changing locale: Force activity to reload resources? had the answer to the question.
I tried
Android: locale(system Language) change effect my application layouts.
Android Localization problem: Not all items in the layout update properly when switching locales,
Trying to change Android Locale on the fly.
http://www.c-sharpcorner.com/UploadFile/1e5156/how-to-change-locale-of-an-application-dynamically-in-androi/
without success.
I tried onResume() which gets triggered but the layout is not changed when the language is changed. I use the following code to change the locale. LocaleSingleton is a class that I created to hold an instance of Configuration.
#Override
protected void onResume() {
getResources().updateConfiguration(LocaleSingleton.getInstance().getConfig(), getResources().getDisplayMetrics());
AsyncData data = new AsyncData();
data.execute(String.valueOf(id), Constants.SERVER + "ad_detail.php");
super.onResume();
}
Any help is appreciated. Thanks in advance.
Edit: I have also tried to restart the activity in onResume() but I get only a black screen. Thanks again.

Add this to your activity in your manifest
<activity
android:name=".YourActivity"
android:configChanges="orientation|keyboardHidden|screenSize" >
</activity>

Related

Onchange Phone language in Android my App layout change

when i change my phone language it effect my App also,,i want that whatever the phone language as,,not effected my App layouts,for example my App is an English language i want the app language and layouts remain same,,while phone language keep changing
Changing the language is considered a configuration change, just like rotating the device or switching the UI from light- to dark mode. Your activity is then recreated and loaded with the appropriate resources.
If you wan't to handle a configuration change yourself, you need to specify this in your manifest for your activities as stated here.
<activity android:name=".MyActivity"
android:configChanges="locale"
android:label="#string/app_name">
You can find other valid values for configChanges here.

How to prevent layout changes based on phone's language?

I know that android:supportsRtl="true" method fits the app to the screen in different sizes. But if a phone has a language which is right-to-left instead of left-to-right, like Persian the buttons or text views, specially if they are in rows, will be right-to-left.
How can I prevent app's layout's changing based on language?
Or keep it in English language in all phones?
Thanks for helping.
In your manifest.xml, inside the application tag add this line.
<application
...
android:supportsRtl="false"
...
Set all the strings in your resources with translatable=false in your strings definitions (as shown here) and set supportsRtl=false, so that your app will never attempt to adapt to the phone conditions.
For language, another strategy could be simply to not provide any translation for the resources: this way the system cannot use layout resources relative to your phone language and region, simply because there aren't any.
Try adding android:layoutDirection="ltr" to your layouts and appBar. It can solve your problem.
Adding android:supportsRtl="false" to manifest can help you, But it isn't enough.

How to update ActionBar after change language in PreferenceFragment?

I want to update language in app by click on the preference in setting fragment which I use preference fragment. Successfully, I can update almost everything in app after change language except ActionBar in each activity including the setting fragment. The only solution I know is to restart app.
Again, I want to change app language include ActionBar after click change in setting. Is it possible?
I have search this question, but not solve yet. Here is my setting layout.
Please help. Thank
Finally, I found a solution:
App won't change Actionbar language after change locale. I won't refresh it. It will change when I remove app from the recent app which make the app completely close.
To solve:
I use setTitle(R.id.myapplabel) when you want to refresh app or
oncreate, so no need to restart app.
Translate activity label on string.xml and it works.

How to display checkbox on the left when change language of Android app to Arabic?

In Settings of my Android app, I use many of checkboxPreferences. When the locale selected is English, text appears at the Left side and Check box at the right side.
Text ------------------Checkbox
When I change locale to Arabic, I want to get this format:
Checkbox---------------Text
But it keeps the same as English locale. I don't know what to do. Can someone help me?
To take advantage of RTL layout mirroring, simply make the following changes to your app:
Declare in your app manifest that your app supports RTL mirroring. Add android:supportsRtl="true" to the element in your manifest file.
Change all of your app's "left/right" layout properties to new "start/end" equivalents.
For more details follow the link
Use RelativeLayout as the parent layout and then add layout_alignParentStart or layout_alignParentEnd attributes for Text / Checkbox views.
Please note, this only works from API v17 on.

textview is cleaned after rotation in Android 4.0 but not in Android 2.0 and 3.0

I have three include other layout (IOL) to include layout with textviews and data in them. Also the background color of the textview changes depending if a related checkbox is checked or not. It happens that if I change device orientation all data and checkboxe states are cleaned, the textview only show the hint text and the background color is conserved. I mention that each IOL include the same layout, but the data in them are accordingly loaded after user selection. I don't understand why this issue occurs for android 4.0 (and up I think, I didn't test yet) but it doesn't for android 2.X and 3.X.
My manifest file has:
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:configChanges="orientation">
how can I fix this problem?
try adding this to your manifest, within the relevant activity
android:configChanges="orientation|keyboardHidden|screenSize|layoutDirection"
I believe it's the 'screenSize' change issue that your dealing with in 4.0 and up.
You Should restore the valure using SavedInstance in 4.0 wht you change the rotation of the screen the oncreate method will get called so the values are not saved.
For Honeycomb and up, we mus put:
android:configChanges="orientation|screenSize"
this is necessary because the new device technology that could also change its screen size.

Categories

Resources