How to change the language of android app from wit

2019-05-28 02:28发布

问题:

I am developing a comic app.I have three radio buttons which are english,french and spanish respectively. And when the user clicks on any one of them the strings.xml from values-es,values-fr and values-en should change the string of the required references but and i click on the radio buttons it does not change the references are still in engish and do not change to french or spanish

回答1:

I am not sure this answer will help you out. But the pinch of code will change the locale pragmatically. Try this, if it works fine, accept the answer and +1 :)

   Resources res = context.getResources();
    // Change locale settings in the app.
    DisplayMetrics dm = res.getDisplayMetrics();
    android.content.res.Configuration conf = res.getConfiguration();
    conf.locale = new Locale("Your language code(lower_case)");
    res.updateConfiguration(conf, dm);


回答2:

Change your language according to Locale like this :

public class AndroidLocalize extends Activity {
    Spinner spinnerctrl;
    Button btn;
    Locale myLocale;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        spinnerctrl = (Spinner) findViewById(R.id.spinner1);
        spinnerctrl.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {

                if (pos == 1) {

                    Toast.makeText(parent.getContext(),
                            "You have selected Es", Toast.LENGTH_SHORT)
                            .show();
                    setLocale("ta");
                } else if (pos == 2) {

                    Toast.makeText(parent.getContext(),
                            "You have selected Fr", Toast.LENGTH_SHORT)
                            .show();
                    setLocale("hi");
                } else if (pos == 3) {

                    Toast.makeText(parent.getContext(),
                            "You have selected En", Toast.LENGTH_SHORT)
                            .show();
                    setLocale("en");
                }

            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }

        });
    }

    public void setLocale(String lang) {

        myLocale = new Locale(lang);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        Intent refresh = new Intent(this, AndroidLocalize.class);
        startActivity(refresh);
    }
}

After that create values folder which you change. In your case strings.xml from values-es,values-fr and values-en.