3

How to get list of all the installed or supported languages that are listed under "Language and Input settings" programmatically in android. I have used Resources.getSystem().getAssets().getLocales() but it gives me only code of that language like en, en_us but I need both code and display name. I have also tried with Locale.getAvailableLocales() but it gives me repeated items and too big list.

Please tell me what is the correct way of doing this.

Prithniraj Nicyone
  • 5,021
  • 13
  • 52
  • 78

2 Answers2

2

getAvailableLocales() gives you all Languages,but as one/more countries can have same language,it is repeated,you can filter it using

public ArrayList<String> getAllLanguages()
{
 Locale[] locales = Locale.getAvailableLocales();
 ArrayList<String> languages=new ArrayList<String>();
 for(Locale temp : locales)
 {
   if(!languages.Contains(temp.getDisplayLanguage()))
     languages.Add(temp.getDisplayLanguage());
 }
  return languages;
}

Okay ,This will work to get Languages under input Settings

 private ArrayList<String> getInputLanguages() {
       ArrayList<String> inputlanguages=new ArrayList<String>();
       InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
       List<InputMethodInfo> ims = imm.getEnabledInputMethodList();

       for (InputMethodInfo method : ims){
           List<InputMethodSubtype> submethods = imm.getEnabledInputMethodSubtypeList(method, true);
           for (InputMethodSubtype submethod : submethods){
              if (submethod.getMode().equals("keyboard")){
                 String currentLocale = submethod.getLocale();
                 Locale locale = new Locale(localeString);
                 String currentLanguage = locale.getDisplayLanguage();   
                 inputlanguages.Add(currentLanguage );        

               }
           }
     }
 return inputlangauges;
}
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
  • Thanks Rajan for such a nice logic, I have implemented this and this is now giving me unique list of languages. If I want to list only those languages which are listed under "Language and Input settings" then what I need to do for that. Thanks for your help. – Prithniraj Nicyone Oct 19 '15 at 13:36
  • i guess those were the languages,which comes under Language and input settings – Rajan Kali Oct 19 '15 at 15:21
  • Thanks for the reply. I have checked the language list under "Language and input settings " but the list is different with the List I am having using Locale.getAvailableLocales(), do you have any idea about this. Thank you so much in advanced.. – Prithniraj Nicyone Oct 20 '15 at 06:48
  • Can you post the difference for refernce,i mean like which one has more Languages listed among both? – Rajan Kali Oct 20 '15 at 06:50
  • When I use Locale.getAvailableLocales(), it gives me more languages. It's ok I will figure out solution for this and will let you know. I am having one more doubt that is, If I change language I use the below code Locale locale = new Locale("en", "US"); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; getBaseContext().getResources().updateConfiguration(config, null); Do I need to write this code in each activity, or doing so at any common place will reflect changes in each activity. Thank you so much in advanced. – Prithniraj Nicyone Oct 20 '15 at 08:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92833/discussion-between-prithniraj-nicyone-and-rajan-ks). – Prithniraj Nicyone Oct 20 '15 at 09:16
  • Is there a way to do this on an app built with flutter? – Michaeluuk Feb 20 '23 at 12:05
1

Yes you can get the list same as under languageand input settings by using

String[] systemLocaleIetfLanguageTags = Resources.getSystem().getAssets().getLocales();
tanmeet
  • 220
  • 2
  • 11