Almett
Almett

Reputation: 906

How to change locale of android system automatically via code

I have apk file of the application undertest(no source code,is robotium UI automated-tests).I need to change system locale automatically via code. Could anyone give me a mechanism or way to solve this problem?

Someone recommended me this code:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.settings", "com.android.settings.LanguageSettings");            
startActivity(intent);

It helps me to open Settings->Language & Input, but i need to select one language automatically via code(Because it is requirement for robotium automated-tests).

Please give me a specific process of solution.

Thanks

Upvotes: 3

Views: 913

Answers (1)

Virthuss
Virthuss

Reputation: 3213

Do like this if you want for example to change your app language:

Resources res = getResources();
                // Change locale settings in the app.
                DisplayMetrics dm = res.getDisplayMetrics();
                android.content.res.Configuration conf = res.getConfiguration();
                conf.locale = new Locale(/* your language here */);
                res.updateConfiguration(conf, dm);

Upvotes: 1

Related Questions