Search code examples
androidandroid-activityencapsulationandroid-dialog

Android setContentView ecnapsulation


I need to make a singleton or static class to change localization of an app, it will have the following implementation

public static void changeLocale( Context context , int layoutID ,int pos )
{
    if ( currentLocale == pos)
        return;
    Locale locale;
    if (pos == 0)
        locale = new Locale("en");
    else
        locale = new Locale("ar");
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    context.getApplicationContext().getResources().updateConfiguration(config,              context.getApplicationContext().getResources().getDisplayMetrics());
    //setContentView(layoutID);
}

It will be called from either Activity or Dialog, my problem here is that Context doesn't have setContentView(), what interface or class shall I pass instead of context that would have setContentView(), one way to do this is passing an Object and have it to be cast into Activity or Dialog in try catch blocks, but I believe there is a better way to do this, thanks for you help


Solution

  • You don't need to set contentview when you change locale this is an excert from : http://developer.android.com/guide/topics/resources/localization.html

    you only need to create a new layout for the specific locale called res/layout-localename/sameresourceinallotherfolders.xml

    and it should change the content view accordingly, when you change the locale

    Design a flexible layout

    If you need to rearrange your layout to fit a certain language (for example German with its long words), you can create an alternative layout for that language (for example res/layout-de/main.xml). However, doing this can make your application harder to maintain. It is better to create a single layout that is more flexible.

    Another typical situation is a language that requires something different in its layout. For example, you might have a contact form that should include two name fields when the application runs in Japanese, but three name fields when the application runs in some other language. You could handle this in either of two ways:

    Create one layout with a field that you can programmatically enable or disable, based on the language, or Have the main layout include another layout that includes the changeable field. The second layout can have different configurations for different languages. Avoid creating more resource files and text strings than you need

    You probably do not need to create a locale-specific alternative for every resource in your application. For example, the layout defined in the res/layout/main.xml file might work in any locale, in which case there would be no need to create any alternative layout files.

    Also, you might not need to create alternative text for every string. For example, assume the following:

    Your application's default language is American English. Every string that the application uses is defined, using American English spellings, in res/values/strings.xml. For a few important phrases, you want to provide British English spelling. You want these alternative strings to be used when your application runs on a device in the United Kingdom. To do this, you could create a small file called res/values-en-rGB/strings.xml that includes only the strings that should be different when the application runs in the U.K. For all the rest of the strings, the application will fall back to the defaults and use what is defined in res/values/strings.xml.

    Use the Android Context object for manual locale lookup

    You can look up the locale using the Context object that Android makes available:

    String locale = context.getResources().getConfiguration().locale.getDisplayName();