Android setContentView ecnapsulation -
i need make singleton or static class change localization of app, have 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 called either activity or dialog, problem here context
doesn't have setcontentview()
, interface or class shall pass instead of context have setcontentview()
, 1 way passing object , have cast activity
or dialog
in try catch blocks, believe there better way this, help
you don't need set contentview when change locale excert : http://developer.android.com/guide/topics/resources/localization.html
you need create new layout specific locale called res/layout-localename/sameresourceinallotherfolders.xml
and should change content view accordingly, when change locale
design flexible layout
if need rearrange layout fit language (for example german long words), can create alternative layout language (for example res/layout-de/main.xml). however, doing can make application harder maintain. better create single layout more flexible.
another typical situation language requires different in layout. example, might have contact form should include 2 name fields when application runs in japanese, 3 name fields when application runs in other language. handle in either of 2 ways:
create 1 layout field can programmatically enable or disable, based on language, or have main layout include layout includes changeable field. second layout can have different configurations different languages. avoid creating more resource files , text strings need
you not need create locale-specific alternative every resource in application. example, layout defined in res/layout/main.xml file might work in locale, in case there no need create alternative layout files.
also, might not need create alternative text every string. example, assume following:
your application's default language american english. every string application uses defined, using american english spellings, in res/values/strings.xml. few important phrases, want provide british english spelling. want these alternative strings used when application runs on device in united kingdom. this, create small file called res/values-en-rgb/strings.xml includes strings should different when application runs in u.k. rest of strings, application fall defaults , use defined in res/values/strings.xml.
use android context object manual locale lookup
you can locale using context object android makes available:
string locale = context.getresources().getconfiguration().locale.getdisplayname();
Comments
Post a Comment