Skip to content Skip to sidebar Skip to footer

Trying To Change Android Locale On The Fly

I'm familiar with Java, but just learning Android Programming. I liked the color bars in their Hello, LinearLayout example quite a bit, so I've been trying to expand that into a li

Solution 1:

try updateConfiguration function to set newLocale........


  Resourcesres= context.getResources();
// Change locale settings in the app.DisplayMetricsdm= res.getDisplayMetrics();
android.content.res.Configurationconf= res.getConfiguration();
conf.locale = newLocale(language_code.toLowerCase());
res.updateConfiguration(conf, dm);

in place of spinner.setOnItemSelectedListener(null);

1 - publicclassHelloLinearLayoutActivityextendsActivitimplementsSpinner.OnItemSelectedListener
2- spinner.setOnItemSelectedListener(this);

Solution 2:

Try it this way! See below code (notice that setContentView is not inside the onCreate method anymore):

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    doUIStuff();
    //otherThanUILogicGoesHere
}

@Override
public void onResume() {
    super.onResume();
    if (JustModifiedLocale){
        doUIStuff();
    }
}

void doUIStuff(){
    setContentView(R.layout.my_layout);
    //other UI_related necessary initializations go here
}

Post a Comment for "Trying To Change Android Locale On The Fly"