appLocale setting in android studio

后端 未结 1 642
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-28 17:46

I have made radio group where user can select their desire language and app language changes to selected language but i am not able to use the functions (not sure how to!)

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-28 18:10

    NOTE

    You can download source code from github repo

    Mark current language radio input as selected

    Then you need to save your locale change flag/state inside SharedPreferences

    SAMPLE CODE follow these steps

    Create one class name PrefManager

    import android.content.Context;
    import android.content.SharedPreferences;
    
    public class PrefManager {
        private SharedPreferences.Editor editor;
        private Context mContext;
        private SharedPreferences prefs;
        private final String LANGUAGE = "language";
        private final String PREF = "user_data";
    
        public PrefManager(Context mContext) {
            this.mContext = mContext;
        }
    
        public String getLanguage() {
            this.prefs = this.mContext.getSharedPreferences(PREF, 0);
            return this.prefs.getString(LANGUAGE, "en");
        }
    
        public void setLanguage(String language) {
            this.editor = this.mContext.getSharedPreferences(PREF, 0).edit();
            this.editor.putString(LANGUAGE, language);
            this.editor.apply();
        }
    }
    

    Now add below code/ condition in your settingsActivity.java

    public class JavaActivity extends AppCompatActivity {
    
        PrefManager prefManager;
        RadioButton radio_indo, radio_english;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_java);
            prefManager = new PrefManager(this);
            radio_indo = findViewById(R.id.radio_indo);
            radio_english = findViewById(R.id.radio_english);
    
            if (prefManager.getLanguage().equals("en")) {
                radio_english.setChecked(true);
            } else {
                radio_english.setChecked(true);
            }
        }
    
    }
    

    Make changes when user select another radio button

    • When the user changes the language you need to update it SharedPreferences
    • You need to restart or recreate your activity after locale change

    Note : you should use RadioGroup.OnCheckedChangeListener() instead of android:onClick="onRadioButtonClicked"

    SAMPLE CODE

    public class JavaActivity extends AppCompatActivity {
    
        PrefManager prefManager;
        RadioButton radio_indo, radio_english;
        RadioGroup appLang;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_java);
            prefManager = new PrefManager(this);
    
            radio_indo = findViewById(R.id.radio_indo);
            radio_english = findViewById(R.id.radio_english);
            appLang = findViewById(R.id.appLang);
    
            if (prefManager.getLanguage().equals("en")) {
                radio_english.setChecked(true);
            } else {
                radio_english.setChecked(true);
            }
    
            appLang.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
                    switch (checkId) {
                        case R.id.radio_indo:
                            prefManager.setLanguage("id");
                            // you need to restart or recreate your activity after locale change
                            break;
                        case R.id.radio_english:
                            prefManager.setLanguage("en");
                            // you need to restart or recreate your activity after locale change
                            break;
                    }
                }
            });
        }
    
    }
    

    Please follow this my previous answer to change locale runtime

    https://stackoverflow.com/a/52270630/7666442

    0 讨论(0)
提交回复
热议问题