You are currently viewing Allow users to change language in the Open Event Android app via settings

Allow users to change language in the Open Event Android app via settings

Localisation of the application is a major step towards maximizing market penetration especially in cases like that of India which houses about 22 official languages. Therefore it is important to allow users to change their language preference from the Open Event Android App itself. There are two different ways through which this could be implemented.

  1. Navigate the user to the phone settings page by firing an implicit intent.
  2. Build an in-app language chooser specific to the country and prompt user to choose language on the first launch of the application.

Out of the two, the open event developers community along the mentors decided to go with the first option since it seemed more apt.

Implementation

In order to implement this, the first and foremost requirement is to keep culture-specific resources separated from the rest of your app. Android resolves language and culture-specific resources based on the system locale setting. Support is provided for different locales by using the resources directory in the Android project. The resources especially the string resources have their own language specific string.xml files and they get resolved with the help of key-value pair mapping. The following steps implement it –

Create a static variable in the application class of the app and assign it the default display language. Make sure to override OnConfigurationChanged method to change the language of the app when the user changes the language.

public class OpenEventApp extends Application {

public static String sDefSystemLanguage;

@Override
public void onCreate() {
 super.onCreate();
 
 sDefSystemLanguage = Locale.getDefault().getDisplayLanguage();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
 super.onConfigurationChanged(newConfig);
 sDefSystemLanguage = newConfig.locale.getDisplayLanguage();
}
}

Include a preference category in your application’s settings.xml which corresponds to the preference activity of the app.

< PreferenceCategory
android: key = “change_language_category”
android: title = “@string/language_preferences” >

< Preference
android: defaultValue = “@string/default_mode_language”
android: key = “@string/language_key”
android: title = “@string/change_language” / >
< /PreferenceCategory>

Finally in your preference activity, set your language preference to the string defined in the application class and implement a click listener on the preference to fire an implicit intent.

public class SettingsActivity extends PreferenceActivity implements Preference.OnPreferenceChangeListener {

private Preference languagePreference;
private SharedPreferences preferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 preferences = PreferenceManager.getDefaultSharedPreferences(this);
 addPreferencesFromResource(R.xml.settings);
 setContentView(R.layout.activity_settings);

 languagePreference = findPreference(LANGUAGE_PREF_MODE);
 languagePreference.setSummary(OpenEventApp.sDefSystemLanguage);
}

@Override
public boolean onPreferenceChange(Preference preference, Object o) {

  if (preference.getKey().equals(getResources().getString(R.string.language_key))) {
  languagePreference.setSummary((String) o);
 }

 return false;
}

 @Override
 public void onResume() {
 super.onResume();
 languagePreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
  @Override
  public boolean onPreferenceClick(Preference preference) {
   startActivity(new Intent((Settings.ACTION_LOCALE_SETTINGS)));
   return true;
  }
 });

 languagePreference.setSummary(OpenEventApp.sDefSystemLanguage);
}

}

Conclusion

Language can be the greatest asset for an app to reach maximum market capitalisation. It is therefore critical for the apps like open event android to reach scalability for the events to have maximum outreach.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.