localization - How to change our own application language from device languages list in android? -
i want change application language dynamically not enter hard coded string language like"us""uk" etc want device languages list. important thing want open languages list on activity in dialog list not setting.e.g when user press text-view device language list opened , when user selected language language set on same text-view he/she pressed , @ same time whole app language change. default 1 language selected, e.g english.
please me in form of code , information either work on "onactivityresult" or other process. google found every static code means hard code string.
you can selected language of device with
locale.getdefault().getdisplaylanguage();
and can set language (for english)
locale.setdefault("en")
edit:
this source code of customlocale app android emulators
package com.android.customlocale; import android.app.activitymanagernative; import android.app.iactivitymanager; import android.app.listactivity; import android.content.intent; import android.content.sharedpreferences; import android.content.res.configuration; import android.os.bundle; import android.os.remoteexception; import android.util.log; import android.view.contextmenu; import android.view.menuitem; import android.view.view; import android.view.contextmenu.contextmenuinfo; import android.widget.button; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; import android.widget.textview; import android.widget.toast; import android.widget.adapterview.adaptercontextmenuinfo; import java.util.arraylist; import java.util.collections; import java.util.comparator; import java.util.hashmap; import java.util.locale; import java.util.map; /** * displays list of system locales maintain custom list of user * locales. user can select locale , apply or can create or remove * custom locale. */ public class customlocaleactivity extends listactivity { private static final string custom_locales_sep = " "; private static final string custom_locales = "custom_locales"; private static final string key_custom = "custom"; private static final string key_name = "name"; private static final string key_code = "code"; private static final string tag = "localesetup"; private static final boolean debug = true; /** request code returned when newlocaledialog activity finishes. */ private static final int update_list = 42; /** menu item id applying locale */ private static final int menu_apply = 43; /** menu item id removing custom locale */ private static final int menu_remove = 44; /** list view displaying system , custom locales. */ private listview mlistview; /** textview used display current locale */ private textview mcurrentlocaletextview; /** private shared preferences of activity. */ private sharedpreferences mprefs; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); mprefs = getpreferences(mode_private); button newlocalebutton = (button) findviewbyid(r.id.new_locale); newlocalebutton.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { intent = new intent(customlocaleactivity.this, newlocaledialog.class); startactivityforresult(i, update_list); } }); mlistview = (listview) findviewbyid(android.r.id.list); mlistview.setfocusable(true); mlistview.setfocusableintouchmode(true); mlistview.requestfocus(); registerforcontextmenu(mlistview); setuplocalelist(); mcurrentlocaletextview = (textview) findviewbyid(r.id.current_locale); displaycurrentlocale(); } @suppresswarnings("unchecked") @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == update_list && resultcode == result_ok && data != null) { string locale = data.getextras().getstring(newlocaledialog.intent_extra_locale); if (locale != null && locale.length() > 0) { // current custom locale list string customlocales = mprefs.getstring(custom_locales, null); // update if (customlocales == null) { customlocales = locale; } else { customlocales += custom_locales_sep + locale; } // save prefs if (debug) { log.d(tag, "add/customlocales: " + customlocales); } mprefs.edit().putstring(custom_locales, customlocales).commit(); toast.maketext(this, "added custom locale: " + locale, toast.length_short).show(); // update list view setuplocalelist(); // find item select in list view listadapter = mlistview.getadapter(); (int = 0; < a.getcount(); i++) { object o = a.getitem(i); if (o instanceof map<?, ?>) { string code = ((map<string, string>) o).get(key_code); if (code != null && code.equals(locale)) { mlistview.setselection(i); break; } } } if (data.getextras().getboolean(newlocaledialog.intent_extra_select)) { selectlocale(locale); } } } } private void setuplocalelist() { if (debug) { log.d(tag, "update locate list"); } arraylist<map<string, string>> data = new arraylist<map<string, string>>(); // insert system locales string[] locales = getassets().getlocales(); (string locale : locales) { locale loc = new locale(locale); map<string, string> map = new hashmap<string, string>(1); map.put(key_code, locale); map.put(key_name, loc.getdisplayname()); data.add(map); } locales = null; // insert custom locales string customlocales = mprefs.getstring(custom_locales, ""); if (debug) { log.d(tag, "customlocales: " + customlocales); } (string locale : customlocales.split(custom_locales_sep)) { if (locale != null && locale.length() > 0) { locale loc = new locale(locale); map<string, string> map = new hashmap<string, string>(1); map.put(key_code, locale); map.put(key_name, loc.getdisplayname() + " [custom]"); // presence of "custom" key marks custom. map.put(key_custom, ""); data.add(map); } } // sort locales code collections.sort(data, new comparator<map<string, string>>() { public int compare(map<string, string> lhs, map<string, string> rhs) { return lhs.get(key_code).compareto(rhs.get(key_code)); } }); // update list view adapter mlistview.setadapter(new simpleadapter(this, data, r.layout.list_item, new string[] { key_code, key_name}, new int[] {r.id.locale_code, r.id.locale_name})); } @suppresswarnings("unchecked") @override public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo) { super.oncreatecontextmenu(menu, v, menuinfo); if (menuinfo instanceof adaptercontextmenuinfo) { int position = ((adaptercontextmenuinfo) menuinfo).position; object o = mlistview.getitematposition(position); if (o instanceof map<?, ?>) { string locale = ((map<string, string>) o).get(key_code); string custom = ((map<string, string>) o).get(key_custom); if (custom == null) { menu.setheadertitle("system locale"); menu.add(0, menu_apply, 0, "apply"); } else { menu.setheadertitle("custom locale"); menu.add(0, menu_apply, 0, "apply"); menu.add(0, menu_remove, 0, "remove"); } } } } @suppresswarnings("unchecked") @override public boolean oncontextitemselected(menuitem item) { string pendinglocale = null; boolean is_custom = false; contextmenuinfo menuinfo = item.getmenuinfo(); if (menuinfo instanceof adaptercontextmenuinfo) { int position = ((adaptercontextmenuinfo) menuinfo).position; object o = mlistview.getitematposition(position); if (o instanceof map<?, ?>) { pendinglocale = ((map<string, string>) o).get(key_code); is_custom = ((map<string, string>) o).get(key_custom) != null; } } if (pendinglocale == null) { // should never happen return super.oncontextitemselected(item); } if (item.getitemid() == menu_remove) { // current custom locale list string customlocales = mprefs.getstring(custom_locales, ""); if (debug) { log.d(tag, "remove " + pendinglocale + " custom locales: " + customlocales); } // update stringbuilder sb = new stringbuilder(); (string locale : customlocales.split(custom_locales_sep)) { if (locale != null && locale.length() > 0 && !locale.equals(pendinglocale)) { if (sb.length() > 0) { sb.append(custom_locales_sep); } sb.append(locale); } } string newlocales = sb.tostring(); if (!newlocales.equals(customlocales)) { // save prefs mprefs.edit().putstring(custom_locales, customlocales).commit(); toast.maketext(this, "removed custom locale: " + pendinglocale, toast.length_short) .show(); } } else if (item.getitemid() == menu_apply) { selectlocale(pendinglocale); } return super.oncontextitemselected(item); } private void selectlocale(string locale) { if (debug) { log.d(tag, "select locale " + locale); } try { iactivitymanager = activitymanagernative.getdefault(); configuration config = am.getconfiguration(); locale loc = null; string[] langcountry = locale.split("_"); if (langcountry.length == 2) { loc = new locale(langcountry[0], langcountry[1]); } else { loc = new locale(locale); } config.locale = loc; // indicate isn't passing default - user wants // remembered config.usersetlocale = true; am.updateconfiguration(config); toast.maketext(this, "select locale: " + locale, toast.length_short).show(); } catch (remoteexception e) { if (debug) { log.e(tag, "select locale failed", e); } } } private void displaycurrentlocale() { try { iactivitymanager = activitymanagernative.getdefault(); configuration config = am.getconfiguration(); if (config.locale != null) { string text = string.format("%s - %s", config.locale.tostring(), config.locale.getdisplayname()); mcurrentlocaletextview.settext(text); } } catch (remoteexception e) { if (debug) { log.e(tag, "get current locale failed", e); } } }
}
Comments
Post a Comment