Code to Change Android Phone language programmatically,Android

Change Android Phone language programmatically

 





public static void change_setting_arabic(Context con) {
  try {
                        /** here add language code */
   Locale locale = new Locale("ar");

   Class amnClass = Class.forName("android.app.ActivityManagerNative");
   Object amn = null;
   Configuration config = null;

   // amn = ActivityManagerNative.getDefault();
   Method methodGetDefault = amnClass.getMethod("getDefault");
   methodGetDefault.setAccessible(true);
   amn = methodGetDefault.invoke(amnClass);

   // config = amn.getConfiguration();
   Method methodGetConfiguration = amnClass
     .getMethod("getConfiguration");
   methodGetConfiguration.setAccessible(true);
   config = (Configuration) methodGetConfiguration.invoke(amn);

   // config.userSetLocale = true;
   Class configClass = config.getClass();
   Field f = configClass.getField("userSetLocale");
   f.setBoolean(config, true);

   // set the locale to the new value
   config.locale = locale;

   // amn.updateConfiguration(config);
   Method methodUpdateConfiguration = amnClass.getMethod(
     "updateConfiguration", Configuration.class);
   methodUpdateConfiguration.setAccessible(true);
   methodUpdateConfiguration.invoke(amn, config);

  } catch (Exception e) {
   // TODO: handle exception
   Log.d("error lang change-->", "" + e.getMessage().toString());
  }
}

Comments

  1. will you Please send the full source code to my mail :(krishnapillai87@gmail.com)
    thank you

    ReplyDelete
    Replies
    1. Hi Krishna
      This complete method is the complete source code for converting the language.
      For that u can just cal this method from Activity and pass the Activity context to that method.
      Use language whatever u want in first line of the method inside try block.

      Regards,
      Pavan.

      Delete
  2. Hi pavan,Nice article .Keep it Up.I used your code,but its not working in emulator.Did you add any Strings value in the Strings.xml file.otherwise could you send the Full source code of the project.My mail id is arunroy06@yahoo.co.in

    ReplyDelete
  3. This code is for android 2.2+ and before version. Current I need code for android 4.x/5.x/6.x
    Can you provide details for above requirement?
    Thanks

    ReplyDelete
  4. Hi, Thanks for your help.
    This code is for android 2.2+, and before version. Current I need code for android 4.x/5.x/6.x
    Can you provide details for my requirement?
    Thanks

    ReplyDelete

Post a Comment

Popular posts from this blog

Custom Dialog with no title and full screen in Android

Best working code to set Date limits on Date Picker Dialog,in Android

code to display .pdf,.text,.DOC,.DOCX,.doc extension files exists in SDCard