Search code examples
androidlocale

How to get language for Austria, Switzerland,


I have the following code:

if (Locale.getDefault().equals(Locale.GERMANY) ||
    Locale.getDefault().equals(Locale.GERMAN)) {
        tp.setIs24HourView(true);
    }

This is working for language setting "Germany". But there are other languages to choose: Austria (German), Switzerland (German), etc. For these languages I also want the if statement to get "true".

So for example Austria it is "de_AT" Locale.GERMANY gives de_DE Locale.GERMAN gives only de

if I do

if (Locale.getDefault().equals("de_AT") 

it returns false

I can't figure out how to get the value for the other German speaking countries.


Solution

  • Android already provides to you a way to get which hour format the user has selected. It's important to use this setting because the user can override the locales option in the phone settings. In any case, this is what you should be using:

     tp.setIs24HourView(android.text.format.DateFormat.is24HourFormat());
    

    See the docs here.