properly theme navigation/systembar for system default theme

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Andy Scherzinger 2021-04-07 12:50:54 +02:00
parent a125185915
commit 7751e108e8
No known key found for this signature in database
GPG Key ID: 6CADC7E3523C308B
3 changed files with 54 additions and 17 deletions

View File

@ -332,7 +332,7 @@ public class ConversationsListController extends BaseController implements Searc
if (getResources() != null) { if (getResources() != null) {
DisplayUtils.applyColorToStatusBar( DisplayUtils.applyColorToStatusBar(
activity, activity,
ResourcesCompat.getColor(getResources(), R.color.bg_default, null) ResourcesCompat.getColor(getResources(), R.color.appbar, null)
); );
} }
}); });
@ -344,7 +344,7 @@ public class ConversationsListController extends BaseController implements Searc
if (activity != null && getResources() != null) { if (activity != null && getResources() != null) {
DisplayUtils.applyColorToStatusBar( DisplayUtils.applyColorToStatusBar(
activity, activity,
ResourcesCompat.getColor(getResources(), R.color.appbar, null) ResourcesCompat.getColor(getResources(), R.color.bg_default, null)
); );
} }
} else { } else {
@ -370,6 +370,12 @@ public class ConversationsListController extends BaseController implements Searc
); );
activity.toolbar.setVisibility(View.GONE); activity.toolbar.setVisibility(View.GONE);
activity.searchCardView.setVisibility(View.VISIBLE); activity.searchCardView.setVisibility(View.VISIBLE);
if (getResources() != null) {
DisplayUtils.applyColorToStatusBar(
activity,
ResourcesCompat.getColor(getResources(), R.color.bg_default, null)
);
}
} }
SmoothScrollLinearLayoutManager layoutManager = SmoothScrollLinearLayoutManager layoutManager =
(SmoothScrollLinearLayoutManager) recyclerView.getLayoutManager(); (SmoothScrollLinearLayoutManager) recyclerView.getLayoutManager();

View File

@ -29,6 +29,7 @@ import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.graphics.drawable.Animatable; import android.graphics.drawable.Animatable;
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
@ -67,6 +68,7 @@ import androidx.appcompat.widget.AppCompatDrawableManager;
import androidx.appcompat.widget.SearchView; import androidx.appcompat.widget.SearchView;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat; import androidx.core.content.res.ResourcesCompat;
import androidx.core.graphics.ColorUtils;
import androidx.core.graphics.drawable.DrawableCompat; import androidx.core.graphics.drawable.DrawableCompat;
import androidx.emoji.text.EmojiCompat; import androidx.emoji.text.EmojiCompat;
import androidx.viewpager.widget.ViewPager; import androidx.viewpager.widget.ViewPager;
@ -109,6 +111,9 @@ public class DisplayUtils {
private static final String TAG = "DisplayUtils"; private static final String TAG = "DisplayUtils";
private static final int INDEX_LUMINATION = 2;
private static final double MAX_LIGHTNESS = 0.92;
public static void setClickableString(String string, String url, TextView textView) { public static void setClickableString(String string, String url, TextView textView) {
SpannableString spannableString = new SpannableString(string); SpannableString spannableString = new SpannableString(string);
spannableString.setSpan(new ClickableSpan() { spannableString.setSpan(new ClickableSpan() {
@ -403,32 +408,57 @@ public class DisplayUtils {
return drawable; return drawable;
} }
public static void applyColorToNavigationBar(Window window, @ColorInt int color) { /**
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { * Sets the color of the status bar to {@code color}.
*
* @param activity activity
* @param color the color
*/
public static void applyColorToStatusBar(Activity activity, @ColorInt int color) {
Window window = activity.getWindow();
boolean isLightTheme = lightTheme(color);
if (window != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View decor = window.getDecorView(); View decor = window.getDecorView();
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_NO) { if (isLightTheme) {
int systemUiFlags = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; int systemUiFlagLightStatusBar;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
systemUiFlags = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR | systemUiFlagLightStatusBar = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS; } else {
systemUiFlagLightStatusBar = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
} }
decor.setSystemUiVisibility(systemUiFlags); decor.setSystemUiVisibility(systemUiFlagLightStatusBar);
} else { } else {
decor.setSystemUiVisibility(0); decor.setSystemUiVisibility(0);
} }
window.setNavigationBarColor(color); window.setStatusBarColor(color);
} else if (isLightTheme) {
window.setStatusBarColor(Color.BLACK);
} }
} }
} }
public static void applyColorToStatusBar(Activity activity, @ColorInt int color) { /**
Window window = activity.getWindow(); * Tests if light color is set
if (window != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { *
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); * @param color the color
window.setStatusBarColor(color); * @return true if primaryColor is lighter than MAX_LIGHTNESS
*/
public static boolean lightTheme(int color) {
float[] hsl = colorToHSL(color);
return hsl[INDEX_LUMINATION] >= MAX_LIGHTNESS;
} }
private static float[] colorToHSL(int color) {
float[] hsl = new float[3];
ColorUtils.RGBToHSL(Color.red(color), Color.green(color), Color.blue(color), hsl);
return hsl;
}
public static void applyColorToNavigationBar(Window window, @ColorInt int color) {
window.setNavigationBarColor(color);
} }
/** /**

View File

@ -36,6 +36,7 @@
<item name="actionBarPopupTheme">@style/appActionBarPopupMenu</item> <item name="actionBarPopupTheme">@style/appActionBarPopupMenu</item>
<item name="searchViewStyle">@style/SearchView</item> <item name="searchViewStyle">@style/SearchView</item>
<item name="textInputStyle">@style/TextInputLayout</item> <item name="textInputStyle">@style/TextInputLayout</item>
<item name="android:navigationBarColor">@color/bg_default</item>
</style> </style>
<style name="ErrorAppearance" parent="@android:style/TextAppearance"> <style name="ErrorAppearance" parent="@android:style/TextAppearance">