6

Recently, my users send me the following screenshot, when Android 9.0 Pie & Android OS's Night Mode is enabled.

enter image description here

As you can see, the stock name is not visible, because black color is applied for stock name. In normal white theme, it supposes to look as following

enter image description here


This is my code use to highlight the text color. In my code, I always assume the background for drop down notification is white. (Which is not correct in case dark mode is enabled)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="NotificationTitle" parent="android:TextAppearance.Material.Notification.Title"></style>
</resources>


public SpannableString getFallBelowSpannableString(Context context) {
    if (fallBelowSpannableString != null) {
        return fallBelowSpannableString;
    }

    if (fallBelow == null) {
        return null;
    }

    // The attributes you want retrieved
    int[] attrs = {android.R.attr.textColor};
    TypedArray ta = context.obtainStyledAttributes(R.style.NotificationTitle, attrs);
    int textColor;
    try {
        textColor = ta.getColor(0, context.getResources().getColor(R.color.notification_symbol_name));
    } finally {
        ta.recycle();
    }

    final String notificationMessage = context.getString(R.string.falls_below_template, symbol, org.yccheok.jstock.watchlist.Utils.toStockPrice(fallBelow));
    fallBelowSpannableString = new SpannableString(notificationMessage);
    ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(textColor);
    fallBelowSpannableString.setSpan(foregroundColorSpan, 0, symbol.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    return fallBelowSpannableString;
}

May I know, how to get the notification drop-down background color, and decide best text color when night Mode is enabled?

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • might be useful https://stackoverflow.com/questions/44170028/android-how-to-detect-if-night-mode-is-on-when-using-appcompatdelegate-mode-ni – Manohar Apr 04 '19 at 04:27
  • This link may help you https://medium.com/@pkjvit/https-medium-com-pkjvit-android-multi-theme-night-mode-and-material-design-c186bf9fd678 – primo Apr 04 '19 at 05:03

1 Answers1

0

I think you can use UiModeManager class for getting the current mode. Though it can't be sure if the user in dark mode when he is 'Auto' mode. Let's look at the code -

UiModeManager uiModeManager = (UiModeManager) c.getSystemService(Context.UI_MODE_SERVICE);
    int modeType = uiModeManager.getNightMode(); 

You can also mode type from this below code -

int currentNightMode = getResources().getConfiguration().uiMode
        & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
    case Configuration.UI_MODE_NIGHT_NO:
        // Night mode is not active, we're in day time
    case Configuration.UI_MODE_NIGHT_YES:
        // Night mode is active, we're at night!
    case Configuration.UI_MODE_NIGHT_UNDEFINED:
        // We don't know what mode we're in, assume notnight
}

After that you can change your text color based on the mode. But I found another alternative way to do your work. Like change your resources style according to the mode, like in your res/values/themes.xml file-

<style name="Theme.AppCompat.DayLight" 
       parent="Theme.AppCompat.Light" />

And another file res/values-night/themes.xml -

<style name="Theme.AppCompat.Night" 
       parent="Theme.AppCompat.DayNight" />

You can try different attributes like the only Theme.AppCompat instead of Theme.AppCompat.DayNight. I actually didn't run this code but seems to work with little more changes. Please have a look on the android documentation, this stackoverflow question and this medium blog.