Skip to content

Commit

Permalink
better way to colorize text
Browse files Browse the repository at this point in the history
Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
  • Loading branch information
tobiasKaminsky committed Jul 1, 2019
1 parent 3a7755e commit c8d9a79
Showing 1 changed file with 22 additions and 7 deletions.
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

0 comments on commit c8d9a79

Please sign in to comment.