-2

There is setLocale method in MainActivity.class.

public void setLocale(String selected) {
    Locale locale = new Locale( selected );
    Locale.setDefault( locale );
    Configuration config = new Configuration(  );
    config.setLocale( locale );
    getBaseContext().getResources().updateConfiguration( config, getBaseContext().getResources().getDisplayMetrics() );

    SharedPreferences.Editor editor = getSharedPreferences( "LanguageSetting", MODE_PRIVATE ).edit();
    editor.putString( "MyLanguage", selected );
    editor.apply();

}

I want to call this method above in a Fragment below.

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        final String[] languageItems = {"English", "Polish"};
        mainActivity = new MainActivity();
        switch (parent.getItemAtPosition( position ).toString()){
            case "Language":
                new AlertDialog.Builder( getContext() ).setTitle( R.string.nav_bottom_dialog )
                        .setSingleChoiceItems( languageItems, -1, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                switch (languageItems[which]){
                                    case "English" :
                                        mainActivity.setLocale( "en" );
                                        mainActivity.recreate();
                                        break;
                                    case "Polish" :
                                        mainActivity.setLocale( "pl" );
                                        mainActivity.recreate();
                                        break;
                                }

                                dialog.dismiss();
                            }
                        } ).show();

It works when I have setLocale method and run it in MainActivity. I don't know why it doesn't work when I call the method in a Fragment showing NullPointerException Error like below.

There is no any error shown in Android Studio, does anyone know why??

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: org.language.languageapp, PID: 16894
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
        at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:118)
        at org.techtown.techApp.MainActivity.setLocale(MainActivity.java:134)
        at org.techtown.techApp.ui.fragment.Fragment01$1.onClick(Fragment01.java:74)

MainActivity.java:134 => getBaseContext().getResources().updateConfiguration( config, getBaseContext().getResources().getDisplayMetrics() );

Fragment01.java:74 => mainActivity.setLocale( "en" );

  • You do not create Activity's Object yourself ever .. Read about `Activity` component .. use `getActivity()` in fragment to access Activity's reference . [See this](https://stackoverflow.com/questions/12659747/call-an-activity-method-from-a-fragment). – ADM Mar 19 '20 at 06:39

1 Answers1

0

Hello I put this in parent activity of my fragment:

    public void updateResources(Context context) {

        SharedPreferences pref = getSharedPreferences(Constants.SHARED_PREF_NAME, MODE_PRIVATE);
        int langKey = pref.getInt(Constants.LANGUAGE_SELECTION_KEY, 0);

        Resources res = context.getResources();
        String language;
        String country;

        if (langKey == 0) {
            language = "en";
            country = "1";
        } else {
            language = "am";
            country = "ET";
        }

        Locale locale = new Locale(language , country);
        Locale.setDefault(locale);

        Configuration config = new Configuration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            config.setLocale(locale);
        }
        config.fontScale = 1.0f;

        res.updateConfiguration(config, res.getDisplayMetrics());
    }

and I call this function in the attachBaseContext function of the activity, like this:

    @Override
    protected void attachBaseContext(Context newBase) {
        Log.d(TAG, "attachBaseContext: ");
        super.attachBaseContext(newBase);

        Log.v(TAG, "attachBaseContext: Applying new configuration");

        updateResources(newBase);
    }

Basically what I would do is change the locale in the shared prefs, then I would recreate the activity. When the function attachBaseContext would be called it would check the what locale is in the Shared Preferences and update accordingly.

Maybe you could try the same but putting your setLocale function in attachBaseContext?

Hope this helps, Im still new here

aztek
  • 33
  • 4