Change app language programmatically in Android

前端 未结 30 3074
面向向阳花
面向向阳花 2020-11-21 04:34

Is it possible to change the language of an app programmatically while still using Android resources?

If not, is it possible to request a resource in an specific lan

30条回答
  •  渐次进展
    2020-11-21 05:08

    I was facing the same issue. On GitHub I found the Android-LocalizationActivity library.

    This library makes it very simple to change the language of your app at runtime, as you can see in the code sample below. A sample project including the sample code below and more information can be found at the github page.

    The LocalizationActivity extends AppCompatActivity, so you can also use it when you are using Fragments.

    public class MainActivity extends LocalizationActivity implements View.OnClickListener {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_simple);
    
            findViewById(R.id.btn_th).setOnClickListener(this);
            findViewById(R.id.btn_en).setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.btn_en) {
                setLanguage("en");
            } else if (id == R.id.btn_th) {
                setLanguage("th");
            }
        }
    }
    

提交回复
热议问题