Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.HashMap;
import java.util.Locale;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;

public class Main {
    private static HashMap<String, Locale> mLocaleMap;
    private static Locale mDefaultLocale = Locale.SIMPLIFIED_CHINESE;

    public static void setLanguage(Context context, String language) {
        Resources resources = context.getResources();
        Configuration config = resources.getConfiguration();
        DisplayMetrics dm = resources.getDisplayMetrics();
        if (mLocaleMap == null) {
            config.locale = mDefaultLocale;
        } else {
            if (mLocaleMap.containsKey(language)) {
                config.locale = mLocaleMap.get(language);
            } else {
                config.locale = mDefaultLocale;
            }
        }
        resources.updateConfiguration(config, dm);
    }
}