ice spirit
ice spirit

Reputation: 1535

Android - Change in language using locale on lollipop

this is my code :-

SplashActivity.java

public class SplashActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);`langData = "hi"`
            setLocale(langData);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    //setLocale(langData);
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    finish();
                }
            }, 3000);
        }



    public void setLocale(String lang) {
        Resources res = this.getResources();
        // Change locale settings in the app.
        DisplayMetrics dm = res.getDisplayMetrics();
        android.content.res.Configuration conf = res.getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            setSystemLocale(conf, new Locale(lang.toLowerCase()));
        } else {
            setSystemLocaleLegacy(conf, new Locale(lang.toLowerCase()));
        }
        res.updateConfiguration(conf, dm);
        //startActivity(new Intent(this,MainActivity.class));
        //finish();
        //this.setContentView(R.layout.activity_main);
    }

    @SuppressWarnings("deprecation")
    public Locale getSystemLocaleLegacy(Configuration config) {
        return config.locale;
    }

    @TargetApi(Build.VERSION_CODES.N)
    public Locale getSystemLocale(Configuration config) {
        return config.getLocales().get(0);
    }

    @SuppressWarnings("deprecation")
    public void setSystemLocaleLegacy(Configuration config, Locale locale) {
        config.locale = locale;
    }

    @TargetApi(Build.VERSION_CODES.N)
    public void setSystemLocale(Configuration config, Locale locale) {
        config.setLocale(locale);
    }
}

When i run this code on my emulator (Android 7.0 Api 24) it changes the language in my MainActivity smoothly without causing any errors but when i tried it on lollipop emulator it does't changes the language Please help and thanks in advance :-)

Upvotes: 3

Views: 544

Answers (1)

ice spirit
ice spirit

Reputation: 1535

Just forgot to add country code there new Locale(lang.toLowerCase(),"ISO- COUNTRY_CODE") and application runs fine.

Upvotes: 5

Related Questions