Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better way to colorize text #4193

Merged
merged 1 commit into from
Jul 1, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/main/java/com/owncloud/android/utils/ThemeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
Expand Down Expand Up @@ -219,15 +221,24 @@ public static void setColoredTitle(@Nullable ActionBar actionBar, String title,
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
actionBar.setTitle(title);
} else {
String colorHex = colorToHexString(fontColor(context));
actionBar.setTitle(Html.fromHtml("<font color='" + colorHex + "'>" + title + "</font>"));
Spannable text = new SpannableString(title);
text.setSpan(new ForegroundColorSpan(fontColor(context)),
0,
text.length(),
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
actionBar.setTitle(text);
}
}
}

public static Spanned getColoredTitle(String title, int color) {
String colorHex = colorToHexString(color);
return Html.fromHtml("<font color='" + colorHex + "'>" + title + "</font>");
Spannable text = new SpannableString(title);
text.setSpan(new ForegroundColorSpan(color),
0,
text.length(),
Spannable.SPAN_INCLUSIVE_INCLUSIVE);

return text;
}

/**
Expand All @@ -241,9 +252,13 @@ public static void setColoredTitle(@Nullable ActionBar actionBar, int titleId, C
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
actionBar.setTitle(titleId);
} else {
String colorHex = colorToHexString(fontColor(context));
String title = context.getString(titleId);
actionBar.setTitle(Html.fromHtml("<font color='" + colorHex + "'>" + title + "</font>"));
Spannable text = new SpannableString(title);
text.setSpan(new ForegroundColorSpan(fontColor(context)),
0,
text.length(),
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
actionBar.setTitle(text);
}
}
}
Expand Down