简体   繁体   中英

Launch an android application with different resource file

I have an application which can be launched in 2 versions.

The main difference between the 2 application are the resource file. (Some parts are renamed, but the application handle only one language)

Is it possible to switch between 2 strings.xml file with @string/hello or R.string.hello with some specific settings in android ? If not, what is the best approach to that problem ? I imagined that I can use some eclipse feature to switch the xml file if there is some parameter ... I prefer have only apk package but it isn't mandatory.

I can't change the language on the client ...

Regards

Edit

good idea Aleks. I refactored the projet into a library and launched it with good locale parameters. In that case I can have 3 differents project with the good database.

If I understand correctly, you want to be able to switch the language of the application without changing the language on the phone. I suppose, I can see a scenario where this can be useful.

You could try to do something like this.

1) Build your app with multiple string resources just like you would if you were to support multiple locales/languages.

2) In your manifest, add configChanges="locale" to your activity:

<activity android:name=".Main" android:configChanges="locale" android:label="@string/app_name" />

3) The first time the app starts, it will be in the language of the client phone. When the user changes the language in your app, save the new language in whatever way you prefer (sqlite, file, app bundle, etc.) and force-restart the app.

4) When your app is starting, retrieve the saved language and set default Locale to it:

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        //load saved language
        String languageToUse  = ...
        if(languageToUse != null)
        {
            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);
    }

I haven't tried this myself, so this is just a general idea - see if it works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM