Change application language in settings

    In the Android system, the localization system is very convenient to use, just create a folder and a file with lines in it. But it’s difficult to integrate all possible languages ​​into the application, and it would be nice to give the user a choice of a language other than the standard one.
    I will give an example of this situation:
    The application has 2 languages ​​standard English and Russian. This application was decided to be installed by a Ukrainian who has a device in Ukrainian, but he also knows Russian well, but not very English. But Android, having discovered that there is no Ukrainian language in the application, will launch the application with the standard language, which in our situation is English, but in order for the application to run in Russian, it is necessary to change the system language, which is not very good.
    Here for this and many similar situations there is a solution, in the settings display the language selection item, which includes automatic language selection, English, Russian, etc. (depending on what is required).


    Let's start writing.
    1. It is necessary to create the Application class and define it in the manifest in the corresponding application section in the android: name = "" parameter.
    eg:

    (although some may already have this class)
    2. Create a setting with a choice of language, for this we add in the settings file:


    3. Add the necessary lines to the file with lines:
    ЯзыкВыберите язык:Язык приложения.\nНеобходимо перезапустить приложение для принятия изменений.

    4. And in the file with arrays 2 text arrays:
    Язык аппаратаАнглийскийРусскийdefaultenru

    5. In the created class, in the onCreate method, declare the string variable “lang”, read the variable from the settings, change the application configuration, and the method that is called when the configuration is changed, in which we change it again (without changing the language, I did not want to change the whole application ) As a result, we get the following class:
    public class MyApplication extends Application {
    	private SharedPreferences preferences;
    	private Locale locale;
    	private String lang;
    	@Override
    	public void onCreate() {
    		preferences = PreferenceManager.getDefaultSharedPreferences(this);
    		lang = preferences.getString("lang", "default");	
    			if (lang.equals("default")) {lang=getResources().getConfiguration().locale.getCountry();}
    			locale = new Locale(lang);
    			Locale.setDefault(locale);
    			Configuration config = new Configuration();
    			config.locale = locale;
    			getBaseContext().getResources().updateConfiguration(config, null);
    	}
    	@Override
        public void onConfigurationChanged(Configuration newConfig)
        {
            super.onConfigurationChanged(newConfig);
            locale = new Locale(lang);
    		Locale.setDefault(locale);
    		Configuration config = new Configuration();
    		config.locale = locale;
    		getBaseContext().getResources().updateConfiguration(config, null);     
        }	
    }
    

    6. After that, in order for the language to be applied, it is necessary to completely restart the application (finish (); this will not help, since it only restarts activity), for this I use the System.exit () command;
    (In the example, I made a restart point on the alarm).
    7. In order to avoid problems in the network, it is advised in the manifest for each activity that uses localization:
     android:configChanges="locale"
    

    And also, so that the application is displayed correctly determine the supported screen sizes:


    In such a simple way, you can simplify the life of the user.



    Advantages of this approach:
    -The user is given a choice of language.
    Cons:
    -Need to specify added languages ​​in arrays.

    As usual, an example application with source code:
    1. Source code of an example ;
    2. Sample application (apk) .

    PS What other way can I close the application completely (including Application), except for System.exit ()?

    Also popular now: