SoFunction
Updated on 2025-04-09

Android system language switching listening and setting instance code

Recently, the product manager asked to turn off the internationalization of the language. No matter which country the mobile phone system is set, it must display Chinese. Well, since there is a demand, then do it. However, the project already has English configuration and is provided as the default String. It feels a pity to just delete and replace it with Chinese as the default String. Therefore, the solution is under Google online. Let's start reading down.

1. Dynamically set the application display language in the code (manually control the string under values-zh-rCN)

This method is implemented by changing the configuration in Resource, the code is as follows:

 public static void initAppLanguage(Context context) {
  if (context == null) {
    return;
  }
  ();
  Configuration config = ().getConfiguration();
   = ;
  ().updateConfiguration(config
      , ().getDisplayMetrics());
}

We can set the above method in MyApplication or call it in the base class of Activity

However, the above method is still not enough to keep our application displaying the specified language. Because our application is in the running stage, after the system language changes, our application will still be switched. If no language is specified, the default language will be displayed. This is definitely not the result we want to see, so it is the language switching monitoring mentioned in the title.

2. Language switching monitoring

1. Broadcast monitoring

We can realize it through monitoring system broadcasting (broadcast static registration and dynamic registration can be done, dynamic registration can be used to grasp the registration timing)

/**
  * ClassName: LocaleChangeReceiver
  * Description: (I use one sentence to describe the function of this class)
  * Created by chensf on 2016-8-17 16:47.
  */

public class LocaleChangeReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    (TAG, "mReceiver onReceive (): "+());

    if(().equals(Intent.ACTION_LOCALE_CHANGED)) {
      ("LocaleChangeReceiver","Language change");
      (context);
    }
  }
}

Here we listen to the Intent.ACTION_LOCALE_CHANGED broadcast. After the system language switches, we will be notified and then do whatever we like. Here I set the language to the initial setting language by calling the settings language method mentioned above. Most of the Internet use this method to monitor language switching, but there is actually another method.

2. Rewrite the onConfigurationChanged method

Some system settings will call back this method after they change, so we can start from here.

So where to rewrite this method? Do you feel familiar with this method? Is it useful to monitor horizontal and vertical screen switching? When it comes to horizontal and vertical screen switching, here is a bit of a topic

When switching horizontally and vertically, in order to avoid the Activity calling onCreate again, we usually rewrite this method. However, after switching horizontally and vertically, the Activity does not call onConfigurationChanged but still calls onCreate, because our Activity does not call back onConfigurationChanged by default. The method is to enable it

 <activity
  android:name=".MainActivity"
  android:configChanges="orientation|screenSize">
  <intent-filter>
    <action android:name="" />
    <category android:name="" />
  </intent-filter>
</activity>

Add android:configChanges="orientation|screenSize under the Activity tag in AndroidManifest. Orientation is a configuration for horizontal and vertical screen switching. As for why you need to add screenSize at the same time, it is because after Android 3.0, if you only set orientation but not screenSize, onConfigurationChanged will not be called back.

Let’s go back to the topic. Through the above example, you know that the onConfigurationChanged must be rewritten in the Activity. The configuration is consistent with the above, and the parameters have changed.

<activity
  android:name=".MainActivity"
  android:configChanges="locale|layoutDirection">
  <intent-filter>
    <action android:name="" />
    <category android:name="" />
  </intent-filter>
</activity>

android:configChanges="locale|layoutDirection There is also a problem here. Systems before 4.2 only need to set locale, and 4.2+ need to set layoutDirection configuration, otherwise onConfigurationChanged will not be called back.

The above method of rewriting onConfigurationChanged in Activity obviously has a disadvantage. If you only need to change the language settings of a certain activity, the above method can be applied. If you want to change the language settings of the entire application, the above method is very low. You have to rewrite onConfigurationChanged in each Activity. Although it can be rewrite in the activity base class, you still have to set it under each Activity tag in AndroidManifest.
android:configChanges="locale|layoutDirection, after saying so much, we won't go around the corner, we can rewrite onConfigurationChanged in Application

@Override
public void onConfigurationChanged(Configuration newConfig) {
  ("MyApplication","onConfigurationChanged");
  (newConfig);
  (getBaseContext());
}

This configuration is set by the entire application. Setting the language here can change the language settings of the entire application. In this way, there is no need to add configuration in AndroidManifest. I have tested it. Rewrite onConfigurationChanged in Application and onConfigurationChanged in Activity

It can still callback, but I haven't noticed it yet. If you use the method I provide to implement language switching monitoring, you can tell me when you encounter problems and let us discuss it together.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.