Search in sources :

Example 36 with ForegroundColorSpan

use of android.text.style.ForegroundColorSpan in project Anki-Android by Ramblurr.

the class CompatV11 method setSubtitle.

@Override
public void setSubtitle(Activity activity, String title, boolean inverted) {
    ActionBar ab = activity.getActionBar();
    if (ab != null) {
        if (inverted) {
            CharacterStyle span = new ForegroundColorSpan(activity.getResources().getColor(inverted ? R.color.white : R.color.black));
            SpannableStringBuilder ssb = new SpannableStringBuilder(title);
            ssb.setSpan(span, 0, ssb.length(), 0);
            ab.setSubtitle(ssb);
        } else {
            ab.setSubtitle(title);
        }
    }
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) ActionBar(android.app.ActionBar) CharacterStyle(android.text.style.CharacterStyle) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 37 with ForegroundColorSpan

use of android.text.style.ForegroundColorSpan in project Anki-Android by Ramblurr.

the class CompatV11 method setTitle.

@Override
public void setTitle(Activity activity, String title, boolean inverted) {
    ActionBar ab = activity.getActionBar();
    if (ab != null) {
        CharacterStyle span = new ForegroundColorSpan(activity.getResources().getColor(inverted ? R.color.white : R.color.black));
        SpannableStringBuilder ssb = new SpannableStringBuilder(title);
        ssb.setSpan(span, 0, ssb.length(), 0);
        ab.setTitle(ssb);
    }
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) ActionBar(android.app.ActionBar) CharacterStyle(android.text.style.CharacterStyle) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 38 with ForegroundColorSpan

use of android.text.style.ForegroundColorSpan in project KeepScore by nolanlawson.

the class AbstractHistoryTableFragment method createHistoryItemView.

protected View createHistoryItemView(ViewGroup parent, HistoryItem historyItem, int layoutResId, int rowId, boolean weightIsOne, Activity activity) {
    View view = getInflater().inflate(layoutResId, parent, false);
    // alternating colors for the background, from gray to white
    view.setBackgroundColor(getResources().getColor(rowId % 2 == 0 ? android.R.color.background_light : R.color.light_gray));
    TextView textView1 = (TextView) view.findViewById(android.R.id.text1);
    TextView textView2 = (TextView) view.findViewById(android.R.id.text2);
    if (historyItem == null) {
        // null indicates to leave the text views empty
        setDummyTextView(textView1);
        setDummyTextView(textView2);
        return weightIsOne ? setLayoutWeightToOne(view) : view;
    }
    textView2.setVisibility(View.VISIBLE);
    if (historyItem.isHideDelta()) {
        setDummyTextView(textView1);
        // set as gone to ensure that
        textView1.setVisibility(View.GONE);
    // the first line isn't too tall
    // when we use
    // history_item_tall.xml
    } else {
        int delta = historyItem.getDelta();
        SpannableString deltaSpannable = new SpannableString(IntegerUtil.toCharSequenceWithSign(delta));
        int colorResId = delta >= 0 ? (PreferenceHelper.getGreenTextPreference(activity) ? // green
        ColorScheme.Light.getGreenPositiveColorResId() : // blue
        ColorScheme.Light.getPositiveColorResId()) : // red
        ColorScheme.Light.getNegativeColorResId();
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(getResources().getColor(colorResId));
        deltaSpannable.setSpan(colorSpan, 0, deltaSpannable.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        textView1.setVisibility(View.VISIBLE);
        textView1.setText(deltaSpannable);
    }
    textView2.setText(Long.toString(historyItem.getRunningTotal()));
    return weightIsOne ? setLayoutWeightToOne(view) : view;
}
Also used : SpannableString(android.text.SpannableString) ForegroundColorSpan(android.text.style.ForegroundColorSpan) TextView(android.widget.TextView) TextView(android.widget.TextView) View(android.view.View)

Example 39 with ForegroundColorSpan

use of android.text.style.ForegroundColorSpan in project KeepScore by nolanlawson.

the class AutofitTextView method onMeasure.

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int height = getHeight() - getCompoundPaddingBottom() - getCompoundPaddingTop();
    int maxNumLines = (height / getLineHeight());
    if (maxNumLines >= 2) {
        String oldText = getText().toString();
        int cutoffIndex = StringUtil.getNthIndexOf('\n', oldText, maxNumLines);
        if (cutoffIndex != -1) {
            // cut off the text
            int startOfLastLine = StringUtil.getNthIndexOf('\n', oldText, maxNumLines - 1);
            // make the last line semi-transparent, to indicate that there
            // are more results left
            // (similar to the ellipsize effect in normal text views, but
            // vertical)
            // get the original color (blue or red)
            ForegroundColorSpan[] foregroundColorSpans = ((SpannedString) getText()).getSpans(startOfLastLine + 1, cutoffIndex, ForegroundColorSpan.class);
            // make an alpha-ized gradient out of the original color
            int originalColor = foregroundColorSpans[0].getForegroundColor();
            int startColor = (START_ALPHA << 24) | (0x00FFFFFF & originalColor);
            int endColor = (END_ALPHA << 24) | (0x00FFFFFF & originalColor);
            int numLines = StringUtil.count(getText().subSequence(0, cutoffIndex).toString(), "\n");
            float startY = (numLines * getLineHeight());
            float endY = startY + getLineHeight();
            // build up a new spannable
            SpannableStringBuilder builder = new SpannableStringBuilder().append(getText().subSequence(0, startOfLastLine)).append(getText().subSequence(startOfLastLine, cutoffIndex).toString());
            builder.setSpan(new TopDownGradientSpan(startColor, endColor, startY, endY), startOfLastLine, cutoffIndex, 0);
            setText(builder);
        }
    }
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) SpannedString(android.text.SpannedString) SpannedString(android.text.SpannedString) TopDownGradientSpan(com.nolanlawson.keepscore.android.TopDownGradientSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 40 with ForegroundColorSpan

use of android.text.style.ForegroundColorSpan in project FlexibleAdapter by davideas.

the class Utils method highlightText.

/**
	 * Sets a spannable text with the accent color (if available) into the provided TextView.
	 * <p>Internally calls {@link #fetchAccentColor(Context, int)}.</p>
	 *
	 * @param context      context
	 * @param textView     the TextView to transform
	 * @param originalText the original text which the transformation is applied to
	 * @param constraint   the text to highlight
	 * @param defColor     the default color in case accentColor is not found
	 * @see #fetchAccentColor(Context, int)
	 * @deprecated Use
	 * {@link #highlightText(TextView, String, String, int)} OR
	 * {@link #highlightText(TextView, String, String)}
	 */
@Deprecated
public static void highlightText(@NonNull Context context, @NonNull TextView textView, String originalText, String constraint, @ColorInt int defColor) {
    if (originalText == null)
        originalText = "";
    if (constraint == null)
        constraint = "";
    int i = originalText.toLowerCase(Locale.getDefault()).indexOf(constraint.toLowerCase(Locale.getDefault()));
    if (i != -1) {
        Spannable spanText = Spannable.Factory.getInstance().newSpannable(originalText);
        spanText.setSpan(new ForegroundColorSpan(fetchAccentColor(context, defColor)), i, i + constraint.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanText.setSpan(new StyleSpan(Typeface.BOLD), i, i + constraint.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(spanText, TextView.BufferType.SPANNABLE);
    } else {
        textView.setText(originalText, TextView.BufferType.NORMAL);
    }
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) StyleSpan(android.text.style.StyleSpan) SuppressLint(android.annotation.SuppressLint) Spannable(android.text.Spannable)

Aggregations

ForegroundColorSpan (android.text.style.ForegroundColorSpan)157 SpannableStringBuilder (android.text.SpannableStringBuilder)57 SpannableString (android.text.SpannableString)50 StyleSpan (android.text.style.StyleSpan)25 TextView (android.widget.TextView)25 ImageSpan (android.text.style.ImageSpan)23 Spannable (android.text.Spannable)22 RelativeSizeSpan (android.text.style.RelativeSizeSpan)22 View (android.view.View)22 BackgroundColorSpan (android.text.style.BackgroundColorSpan)19 TypefaceSpan (android.text.style.TypefaceSpan)16 StrikethroughSpan (android.text.style.StrikethroughSpan)14 UnderlineSpan (android.text.style.UnderlineSpan)13 Drawable (android.graphics.drawable.Drawable)12 CharacterStyle (android.text.style.CharacterStyle)11 EditText (android.widget.EditText)11 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)10 SuperscriptSpan (android.text.style.SuperscriptSpan)8 LinearLayout (android.widget.LinearLayout)8 URLSpan (android.text.style.URLSpan)7