Identifying RTL language in Android

前端 未结 16 1595
既然无缘
既然无缘 2020-11-28 06:59

Is there a way to identify RTL (right-to-left) language, apart from testing language code against all RTL languages?

Since API 17+ allows several resources for RTL a

相关标签:
16条回答
  • 2020-11-28 07:39

    Get it from Configuration.getLayoutDirection():

    Configuration config = getResources().getConfiguration();
    if(config.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
        //in Right To Left layout
    }
    
    0 讨论(0)
  • 2020-11-28 07:39

    Because English language devices are supporting RTL, you can use this code in your MainActivity to change device language to english and you don't need to "supportRTL" code.

    String languageToLoad  = "en"; // your language
    Locale locale = new Locale(languageToLoad);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
    getBaseContext().getResources().getDisplayMetrics());
    
    0 讨论(0)
  • 2020-11-28 07:40

    You can use TextUtilsCompat from the support library.

    TextUtilsCompat.getLayoutDirectionFromLocale(locale)

    0 讨论(0)
  • 2020-11-28 07:40

    You can check like this if you want to check for API lower than 17

    boolean isRightToLeft = TextUtilsCompat.getLayoutDirectionFromLocale(Locale
                   .getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL;
    

    OR for API 17 or above

    boolean isRightToLeft = TextUtils.getLayoutDirectionFromLocale(Locale
                   .getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL;
    
    0 讨论(0)
提交回复
热议问题