0
   Button dar =(Button)findViewById(R.id.dr);
    dar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {

            String languageToLoad  = "fr"; // your language
            Locale locale = new Locale("fr");
            Locale.setDefault(locale);
           Configuration config = new Configuration();
           config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());           
            Intent ints =new Intent(getApplication(),MainMenu.class);
            startActivity(ints);
        }
    });

why going to other page my values-fr is loading translations for french. i observe that it change to right side for right side lang but not translating the buttons text

maztt
  • 12,278
  • 21
  • 78
  • 153

2 Answers2

1

Keep in your mind when you set a specific locale from your app, you change your app configuration not system!

When your app launches it use default system locale. You must update Configurations (Change Locale) before appor Activity launch.

Relaunch your current Activity and see Result

I recommend to you change your app locale in Application Object

UPDATE:

If you want to able users to choose language on app first launch, you can save the language code to shared preferences after user click on button and use it to set locale before your launcher Activity start (in application Object class), And for your buttons i recommend to you use country flag instead of text.

public class MyApp extends Application {

    String languageCode; // get it from shared preferences

    @Override
    public void onCreate() {
        super.onCreate();
        setLocale(languageCode);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setLocale(languageCode);
    }

    public static void setLocale(String lang) {
        // do stuff
    }
}

And on button click

MyApp.setLocale("fa");

don't forgot to add this class to your manifest

<application
    android:name="yourPakageName.MyApp"
    ... >
Hamed Nabizadeh
  • 527
  • 4
  • 9
  • How about this thing. i think you are refering to http://stackoverflow.com/questions/2900023/change-language-programmatically-in-android/30885593#30885593 – maztt Jun 03 '16 at 07:54
0

You can try like this for button text translation

   button.setTextLocale(locale);
akhil Rao
  • 1,169
  • 7
  • 17