Change app language at run time in Android - android

I am trying to change the language of my Android app with no success.
I have:
/res/
. values/
. . string.xml
. . values-es/
. . . strings.xml
Both strings files contains the same but the content is translated according to the language code.
The default language is english but I want to do some tests and use the spanish language.
I use the code written in this previous question:
Change app language programmatically in Android
Here is my LoginActivity(launcher)'s onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setting default screen to login.xml
setContentView(R.layout.login);
Resources res = getBaseContext().getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale("es");
res.updateConfiguration(conf, dm);
TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
// Listening to register new account link
registerScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(i);
}
});
Button mainMenu = (Button) findViewById(R.id.btnLogin);
// Listening to register new account link
mainMenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), MainMenuActivity.class);
startActivity(i);
}
});
}
My target is Android 4.4.
However, this is not working on my Nexus 4 - Android 4.4, it always uses English language.

The values-es folder should be on the same level as the values folder. Your example above looks like you have values-es inside the values folder.

Related

Language change to arabic not working?

I have implemented change language from settings as per this ans , but after killing of my application some of the screen of application turned to english.
I have implemented code as below
Code for Splash Screen
String deviceLanguage = Locale.getDefault().getLanguage();
if (!"en".equalsIgnoreCase(deviceLanguage) && !"ar".equalsIgnoreCase(deviceLanguage)){
deviceLanguage="en";
}
((AppController)getApplication()).appLang= Utilities.getSaveData(this, getString(R.string.key_language),deviceLanguage);
Code for Detail Activity
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(MyContextWrapper.wrap(newBase, ((AppController)newBase.getApplicationContext()).appLang));
}
Note : I got this problem in one plus 3T device and genymotion emulator with 5.1 android version
final Resources res = appContext.getResources();
final Configuration conf = res.getConfiguration();
conf.locale = new Locale("ar", "AE");
res.updateConfiguration(conf, null);
After updating configuration, Restart the activity as mentioned below
finish();
final Intent intent = getIntent();
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

How to change the language of the app when THE USER selects the language?

How to change the language of the app when THE USER selects the language?
I want to do almost this: http://snowpard-android.blogspot.com.br/2013/03/programmatically-change-language-in.html?google_comment_id=z13isbsazkf3hzea504celo5oy3rjzbyevo0k
but instead of changing the language of a textView, I want to create a button with the name of the language, and when the user clicks on it, it goes to a second page already translated. I already created new values with the languages, but can't think about a code that could open another page with those strings. Could anyone help me, plssss?
I would suggest to pass the language name as an intent extra in the first activity and get it in the second activity and update the language accordingly. Consider the following
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// findViewbyId here for button
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
intent.putExtra("lang", "fr");
startActivity(intent);
}
});
}
}
and in the other activity
public class OtherActivity extends AppCompatActivity {
#Override
private void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String lang = getIntent().getStringExtra("lang") == null ? getIntent().getStringExtra("lang") : "en";
// assuming this is the method you have to call to change the language
changeLang(en);
}
}
Hope this helps.
use localization for achieving this on on click .
String languageToLoad = "hi"; // change your language her this is for hindi
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
this.setContentView(R.layout.main);

How to apply new Locale in Android to current screen

I have implemented an option to select GUI language in my app, but I can't make it refresh the screen when a new language is selected.
The selection is made through a ListPreference so there are 2 problems :
1. Refresh the Preference page in which the language is selected.
2. When the app starts, I set the Locale on the onCreate() of the MainActivity but the MainActivity layout is never getting updated with the new selected Locale. (all other screens are updated so the set code is good).
Here is the code for setting the new Locale :
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ctx);
String langKey = ctx.getString(R.string.shared_options_select_language_key);
String langDefault = ctx.getString(R.string.shared_options_select_language_default_value);
String appLanguage = preferences.getString(langKey, langDefault);
Locale locale = null;
if (appLanguage.equals(langDefault)) {
locale = new Locale(Resources.getSystem().getConfiguration().locale.getLanguage());
} else {
String[] languageInfo = appLanguage.split("_");
if (languageInfo.length > 1) {
locale = new Locale(languageInfo[0], languageInfo[1]);
} else {
locale = new Locale(appLanguage);
}
}
if (appLanguage.contains(currentLang)) {
return false;
}
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
act.getBaseContext().getResources().updateConfiguration(config, act.getBaseContext().getResources().getDisplayMetrics());
Thank you
Try to refresh the MainActivity after selecting the new Locale
use this to reload the main activity or any activity
public void refrehs_me()
{
Intent intent = getIntent();
finish();
startActivity(intent);
}
Edit : As I can get from your explanation above you save the new locale selected in a shared preference.
Make a new boolean key in the shared preference called (locale_changed) - when a user select a new locale set this key to (TURE) and on the onCreate after setting the new locale check for this key if (TRUE) set it to (FALSE) and reload the activity.
on the second go it will not loop as the key remains (FALSE) until a user change the locale preference again.
The answer would be to position the call to setLocale right before setting the screen content :
setLocale();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Wrong language fonts at multi language app

i'm trying to make a multilanguage app, English and Greek.
All i want is 2 buttons at the first screen that u will choose language and then all the other app will bve at that language. I have make a simple test project with 2 buttons and a paragraph, when u push the english button the text will be english, when u push the greek button the text will be greek.
My code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page2);
Button english = (Button) findViewById(R.id.english);
english.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Locale locale = new Locale("en_UK");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getApplicationContext().getResources().updateConfiguration(config, null);
Intent intent = new Intent(v.getContext(), MainActivity.class);
startActivityForResult(intent, 0);
}
});
Button greek = (Button) findViewById(R.id.greek);
greek.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Locale locale = new Locale("el_GR");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getApplicationContext().getResources().updateConfiguration(config, null);
Intent intent = new Intent(v.getContext(), MainActivity.class);
startActivityForResult(intent, 0);
}
});
}
the strings.xml with the english paragraph is at the original res/values folder
and the strings.xml with the greek language is in res/values-el
here is 3 screenshots from the test app
here is some screenshots:
the one with the non letters things is the greek paragraph
the problem is that your phone doesnot supports unicode characters for greek,
there are two solutions,
1)It means that fonts your device uses do not support greek characters (hence unknown unicode). You need to find better font and use it in your application (see http://developer.android.com/reference/android/graphics/Typeface.html docs)
2)get online unicode converter or google translator for english to greek and copy the words to your strings.xml

Changing of Application Locale does not take full effect in android?

I am making an application in two languages English and in Indonesian.
For that i have created two strings file in res folder (values-en and values-in)
In one of my activity I select lanuage and change the locale.I am doing this using following code-
passing language id to this method-
public void setLocale(String lang) {
Locale myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
}
But after changing the language to indonesian strange thing happened. My Some of the text changed to indonesian and some changed to english.
for example- I have two Buttons (Submit and Upload Receipt) in activity.
SUBMIT btn has-
in english it's text is SUBMIT and in indonesian it is KIRIM
<string name="submit_btn_text">Submit</string> for english
<string name="submit_btn_text">Kirim</string> for indonesian
and UPLOAD RECEIPT btn has-
in english it's text is Upload Receipt and in indonesian it is Upload Penerimaan
<string name="upload_receipt">Upload Receipt</string> for english
<string name="upload_receipt">Upload Penerimaan</string> for indonesian
But in indonesian
Submit btn text chanes to Kirim
but Upload Receipt btn remain "Upload Receipt". It doesn't change to Upload Penerimaan.
How this is happened as other text chages as per locale.
Also I am facing same problem in all dialogs pop ups.Text remain in english in all dialogs.
What should be done?
try restarting your activity as suggested by other answers, check Ben's answer How do I restart an Android Activity
public void reload() {
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}

Categories

Resources