0

I'm developing an android application which should support both English and Arabic.I have done a. Created values folder for English and Arabic and updated the strings used. b. Created an Application class for localization c. Implemented the code on button click in Activity

See my Application file

MyApp.java

import android.app.Application; import android.content.Context; import android.content.res.Configuration; import java.util.Locale; public class MyApp extends Application{ @Override public void onCreate() { super.onCreate(); } public static void setLocaleAr (Context context){ Locale locale = new Locale("ar"); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; context.getApplicationContext().getResources().updateConfiguration(config, null); } public static void setLocaleEn (Context context){ Locale locale = new Locale("en_US"); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; context.getApplicationContext().getResources().updateConfiguration(config, null); } } 

My activity class using button click

MainActivity.java

 import android.app.Activity; import android.content.Intent; import android.content.res.Configuration; import android.content.res.Resources; import android.os.Bundle; import android.os.Handler; import android.util.DisplayMetrics; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.SimpleAdapter; import java.util.Locale; public class SplashActivity extends Activity implements View.OnClickListener{ // Splash screen timer private static int SPLASH_TIME_OUT = 3000; Button buttonEnglish,buttonArabic; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); buttonEnglish =(Button) findViewById(R.id.buttonEnglish); buttonArabic =(Button) findViewById(R.id.buttonArabic); buttonEnglish.setOnClickListener(this); buttonArabic.setOnClickListener(this); } @Override public void onClick(View view) { switch(view.getId()) { case R.id.buttonEnglish: MyApp.setLocaleEn(SplashActivity.this); setLocale("en"); break; case R.id.buttonArabic: { //buttonArabic.setBackground(getDrawable(R.color.white)); MyApp.setLocaleAr(SplashActivity.this); setLocale("ar"); break; } } } } 
3
  • needs restart or not - have you tried it? And why don't you jus fall-back to the device's locale? Commented Jun 30, 2017 at 10:59
  • Is this the correct way of doing localisation for new versions also. Now the localisation is not done with my code. Commented Jun 30, 2017 at 11:02
  • 1
    no, the correct way is to use system locale, and not set it manually. Commented Jun 30, 2017 at 11:10

2 Answers 2

5

Just forget everything and just implement the below code Snippet

Inside your build.gradle file

defaultConfig { resConfigs "en", "ar" } 

And inside your activity:

 @Override public void onClick(View view) { switch(view.getId()) { case R.id.buttonEnglish: updateLanguage("en"); break; case R.id.buttonArabic: updateLanguage("ar"); break; } } @Override public void recreate() { if (android.os.Build.VERSION.SDK_INT >= 14) { super.recreate(); } else { startActivity(getIntent()); finish(); } } private void updateLanguage(String language) { Locale locale = new Locale(language); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); SharedPreferences languagepref = getSharedPreferences("language",MODE_PRIVATE); SharedPreferences.Editor editor = languagepref.edit(); editor.putString("languageToLoad",language); editor.apply(); recreate(); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Got it.Thanks for the answer :)
1

You need to recreate your activity.

You can update locale from Splash Activity class itself, use this method to update locale

 //input: local values like "en","fr", "it" public void setLocale(String locale) { // Update Locale in App Configuration getResources().getConfiguration().setLocale(new Locale(locale)); getResources().updateConfiguration(getResources().getConfiguration(), getResources().getDisplayMetrics()); // Restart Activity finish(); overridePendingTransition(0, 0); startActivity(getIntent()); overridePendingTransition(0, 0); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.