change Language via Intent - Android Intent

Android examples for Intent:Open App

Description

change Language via Intent

Demo Code


//package com.java2s;
import java.util.Locale;
import android.app.Activity;
import android.app.Application;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;

public class Main {
    /**//from   www . j  a v  a2  s  . co  m
     * @param activity
     * @param isEnglish
     * @param clazz
     */
    public static <T> void changeLanguage(Activity activity,
            boolean isEnglish, Class<T> clazz) {
        Resources resource = activity.getResources();
        Configuration config = resource.getConfiguration();
        config.locale = isEnglish ? Locale.ENGLISH : Locale.CHINESE;
        activity.getBaseContext().getResources()
                .updateConfiguration(config, null);
        Intent intent = new Intent();
        intent.setClass(activity, clazz);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);
    }

    /**
     * @param myApplication
     * @param englishLanguage
     */
    public static void changeLanguage(Application myApplication,
            boolean englishLanguage) {
        Resources resource = myApplication.getResources();
        Configuration config = resource.getConfiguration();
        config.locale = englishLanguage ? Locale.ENGLISH : Locale.CHINESE;
        myApplication.getBaseContext().getResources()
                .updateConfiguration(config, null);
    }
}

Related Tutorials