let DynamicLanguage.getLayoutDirection() work on sdk16/android4.1 (#2423)

sdk16/android4.1 does not support RTL fully,
therefore just return LTR in this case
(this is also what `ViewCompat.getLayoutDirection()`
and `View.getLayoutDirection()` are doing).

always using `ViewCompat.getLayoutDirection()`, however,
causes problems as the layout is based on the content then
(see #2422)

closes #2420
This commit is contained in:
bjoern 2022-11-12 22:33:48 +01:00 committed by GitHub
parent 521f55fe04
commit 81c2812390
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 5 deletions

View file

@ -11,6 +11,7 @@ import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import androidx.annotation.RequiresApi;
import androidx.core.os.ConfigurationCompat;
import androidx.core.view.ViewCompat;
import android.text.TextUtils;
import android.util.Log;
@ -49,11 +50,13 @@ public class DynamicLanguage {
return currentLocale;
}
@RequiresApi(VERSION_CODES.JELLY_BEAN_MR1)
public static int getLayoutDirection(Context context) {
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
Configuration configuration = context.getResources().getConfiguration();
return configuration.getLayoutDirection();
}
return ViewCompat.LAYOUT_DIRECTION_LTR;
}
public static void setContextLocale(Context context, Locale selectedLocale) {
Configuration configuration = context.getResources().getConfiguration();

View file

@ -208,7 +208,7 @@ public class ViewUtil {
}
public static boolean isLtr(@NonNull Context context) {
return context.getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_LTR;
return DynamicLanguage.getLayoutDirection(context) == ViewCompat.LAYOUT_DIRECTION_LTR;
}
public static boolean isRtl(@NonNull View view) {
@ -216,7 +216,7 @@ public class ViewUtil {
}
public static boolean isRtl(@NonNull Context context) {
return context.getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
return DynamicLanguage.getLayoutDirection(context) == ViewCompat.LAYOUT_DIRECTION_RTL;
}
public static int dpToPx(Context context, int dp) {