Creating a multilingual Android application allows users to experience your app in their preferred language. This guide explains how to support multiple languages, both automatically and with manual switching options.
1. Create Language Resources
Android uses res
folders to organize language-specific resources:
Default Language
Create res/values/strings.xml
for your app's default language:
<resources> <string name="app_name">My App</string> <string name="greeting">Hello!</string> </resources>
Additional Languages
Create res/values-
files for each supported language:
<resources> <string name="app_name">Mi Aplicación</string> <string name="greeting">¡Hola!</string> </resources>
Use ISO 639-1 language codes and optionally include region codes (e.g., values-es-rES
for Spain's Spanish).
2. Automatic Language Selection
Android automatically selects the appropriate language based on the user's device settings. You don't need to handle this unless you want to provide a manual language switcher.
3. Manually Switch Languages
Update Locale
Create a helper method to change the app's locale dynamically:
import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; import java.util.Locale; public class LocaleHelper { public static void setLocale(Resources resources, String languageCode) { Locale locale = new Locale(languageCode); Locale.setDefault(locale); Configuration config = resources.getConfiguration(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { config.setLocale(locale); resources.updateConfiguration(config, resources.getDisplayMetrics()); } else { config.locale = locale; resources.updateConfiguration(config, resources.getDisplayMetrics()); } } }
Use the Locale Helper
Call the method to update the app's language dynamically:
LocaleHelper.setLocale(getResources(), "es"); // Switch to Spanish recreate(); // Restart the activity for changes to take effect
4. Provide a Language Selector
Add a dropdown or dialog in your app for users to select a language. For example, using a Spinner:
Spinner languageSpinner = findViewById(R.id.language_spinner); String[] languages = {"English", "Español"}; String[] languageCodes = {"en", "es"}; ArrayAdapteradapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, languages); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); languageSpinner.setAdapter(adapter); languageSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { LocaleHelper.setLocale(getResources(), languageCodes[position]); recreate(); // Apply the changes } @Override public void onNothingSelected(AdapterView parent) {} });
5. Save User's Language Preference
Store the selected language in SharedPreferences
:
SharedPreferences prefs = getSharedPreferences("AppPrefs", MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); editor.putString("language", "es"); // Save selected language editor.apply();
Restore the language on app launch:
String languageCode = prefs.getString("language", "en"); LocaleHelper.setLocale(getResources(), languageCode);
6. Test Your App
- Change your device's language settings and verify the automatic switch.
- Test the manual language switch functionality using the provided dropdown or selector.
7. Handle Special Cases
RTL Support
For right-to-left languages (e.g., Arabic, Hebrew), enable RTL support in AndroidManifest.xml
:
<application android:supportsRtl="true"> </application>
Pluralization and Gender
Use quantity strings and placeholders for complex translations.
Conclusion
Implementing multilingual support in your Android app ensures a better user experience for a global audience. Follow this guide to make your app accessible and inclusive.
0 Comments