I am working on a application in which I have to change apps content from English to Arabic and vice versa.
Language is being changed according to language selection in Activities and Fragments but sometimes its doesn't change in AlertDialog. Please suggest me what I am doing wrong.
Below are the details.
Class & Method to show dialog
public class AlertDialogManager {
public static void showAlertDialog(Context ctx, String message) {
if (ctx == null) {
return;
}
showAlertDialog(ctx, message, Gravity.CENTER);
}
public static void showAlertDialog(Context ctx, String message, int gravity) {
try {
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setMessage(message).setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated
// method stub
dialog.cancel();
}
});
AlertDialog dialog = builder.show();
TextView messageText = (TextView) dialog
.findViewById(android.R.id.message);
messageText.setGravity(gravity);
} catch (Exception e) {
// TODO: handle exception
}
}
}
Method which is calling above method
@Override
public void showAlert(String message) {
if (!TextUtils.isEmpty(message)) {
AlertDialogManager.showAlertDialog(MerchantLoginActivity.this, message);
}
}
Method being used in every activity to change content's language
@Override
protected void attachBaseContext(Context newBase) {
Locale languageType = LanguageUtil.getLanguageType(newBase);
super.attachBaseContext(LocaleContextWrapper.wrap(newBase, languageType));
}
Button click event on which language is being changed | MainActivity
@OnClick(R.id.language_button)
void onLanguageClicked(View view) {
// Getting saved language
currentLanguage = LocaleHelper.getLanguage(getBaseContext());
String languageToLoad = (currentLanguage.equals(Language.ARABIC) ? Language.ENGLISH : Language.ARABIC); // your language
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.setLocale(locale);
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
LocaleHelper.setLocale(getApplicationContext(), languageToLoad);
//LanguageUtil.changeLanguageType(getApplicationContext(), locale);
FlooosyApplication.getInstance().setLanguage(languageToLoad);
this.recreate();
//onRestart();
}
onRestart() | MainActivity
@Override
protected void onRestart() {
super.onRestart();
if (locale.equals(Language.ARABIC))
locale = Language.ENGLISH;
else
locale = Language.ARABIC;
startActivity(new Intent(this, MainActivity.class));
finish();
}