android.text.style.BackgroundColorSpan Java Examples

The following examples show how to use android.text.style.BackgroundColorSpan. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConverterSpannedToHtml.java    From memoir with Apache License 2.0 6 votes vote down vote up
private void handleEndTag(CharacterStyle style) {
    if (style instanceof URLSpan) {
        mOut.append("</a>");
    } else if (style instanceof TypefaceSpan) {
        mOut.append("</font>");
    } else if (style instanceof ForegroundColorSpan) {
        mOut.append("</font>");
    } else if (style instanceof BackgroundColorSpan) {
        mOut.append("</font>");
    } else if (style instanceof AbsoluteSizeSpan) {
        mOut.append("</font>");
    } else if (style instanceof StrikethroughSpan) {
        mOut.append("</strike>");
    } else if (style instanceof SubscriptSpan) {
        mOut.append("</sub>");
    } else if (style instanceof SuperscriptSpan) {
        mOut.append("</sup>");
    } else if (style instanceof UnderlineSpan) {
        mOut.append("</u>");
    } else if (style instanceof BoldSpan) {
        mOut.append("</b>");
    } else if (style instanceof ItalicSpan) {
        mOut.append("</i>");
    }
}
 
Example #2
Source File: AdvancedAdapter.java    From SimpleDialogFragments with Apache License 2.0 6 votes vote down vote up
/**
 * Highlights everything that matched the current filter (if any) in text
 *
 * @param text the text to highlight
 * @param color the highlight color
 * @return a spannable string
 */
protected Spannable highlight(String text, int color) {
    if (text == null) return null;

    Spannable highlighted = new SpannableStringBuilder(text);
    AdvancedFilter filter = getFilter();

    if (filter == null || filter.mPattern == null){
        return highlighted;
    }

    Matcher matcher = filter.mPattern.matcher(text);

    while (matcher.find()){
        highlighted.setSpan(new BackgroundColorSpan(color), matcher.start(),
                matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        );
    }

    return highlighted;
}
 
Example #3
Source File: TextFormatBar.java    From revolution-irc with GNU General Public License v3.0 6 votes vote down vote up
protected ColorListPickerDialog createColorPicker(boolean fillColor, int selectedColor) {
    ColorListPickerDialog dialog = new ColorListPickerDialog(getContext());
    if (!fillColor) {
        dialog.setTitle(R.string.format_text_color);
    } else {
        dialog.setTitle(R.string.format_fill_color);
    }
    dialog.setSelectedColor(selectedColor);
    dialog.setPositiveButton(R.string.action_cancel, null);
    dialog.setOnColorChangeListener((ColorListPickerDialog d, int newColorIndex, int color) -> {
        if (!fillColor) {
            removeSpan(ForegroundColorSpan.class);
            setSpan(new ForegroundColorSpan(color));
        } else {
            removeSpan(BackgroundColorSpan.class);
            setSpan(new BackgroundColorSpan(color));
        }
        d.cancel();
    });
    return dialog;
}
 
Example #4
Source File: ActivityEditTransaction.java    From fingen with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("ConstantConditions")
    private void initSms() {
        if (sms != null) {
            SmsParser smsParser = new SmsParser(sms, getApplicationContext());
            Spannable text = new SpannableString(sms.getmBody());
            text.setSpan(new BackgroundColorSpan(ContextCompat.getColor(this, R.color.ColorAccount)), smsParser.mAccountBorders.first, smsParser.mAccountBorders.second, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            text.setSpan(new BackgroundColorSpan(ContextCompat.getColor(this, R.color.ColorAmount)), smsParser.mAmountBorders.first, smsParser.mAmountBorders.second, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            text.setSpan(new BackgroundColorSpan(ContextCompat.getColor(this, R.color.ColorAmount)), smsParser.mBalanceBorders.first, smsParser.mBalanceBorders.second, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            text.setSpan(new BackgroundColorSpan(ContextCompat.getColor(this, R.color.ColorPayee)), smsParser.mPayeeBorders.first, smsParser.mPayeeBorders.second, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            text.setSpan(new BackgroundColorSpan(ContextCompat.getColor(this, R.color.ColorDestAccount)), smsParser.mDestAccountBorders.first, smsParser.mDestAccountBorders.second, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            text.setSpan(new BackgroundColorSpan(ContextCompat.getColor(this, R.color.ColorType)), smsParser.mTrTypeBorders.first, smsParser.mTrTypeBorders.second, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            text.setSpan(new BackgroundColorSpan(ContextCompat.getColor(this, R.color.ColorCabbage)), smsParser.mCabbageAmountBorders.first, smsParser.mCabbageAmountBorders.second, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            text.setSpan(new BackgroundColorSpan(ContextCompat.getColor(this, R.color.ColorCabbage)), smsParser.mCabbageBalanceBorders.first, smsParser.mCabbageBalanceBorders.second, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            edSms.setText(text);

//            imageButtonAddMarker.setImageDrawable(IconGenerator.getInstance(this).getAddIcon(this));
            imageButtonAddMarker.setOnClickListener(v -> ActivityEditTransaction.this.showAddMarkerDialog());

            edSms.setCustomSelectionActionModeCallback(new ActionModeCallback(this));
        } else {
            layoutSms.setVisibility(View.GONE);
        }

    }
 
Example #5
Source File: AdvancedAdapter.java    From SimpleDialogFragments with Apache License 2.0 6 votes vote down vote up
/**
 * Highlights everything that matched the current filter (if any) in text
 *
 * @param text the text to highlight
 * @param color the highlight color
 * @return a spannable string
 */
protected Spannable highlight(String text, int color) {
    if (text == null) return null;

    Spannable highlighted = new SpannableStringBuilder(text);
    AdvancedFilter filter = getFilter();

    if (filter == null || filter.mPattern == null){
        return highlighted;
    }

    Matcher matcher = filter.mPattern.matcher(text);

    while (matcher.find()){
        highlighted.setSpan(new BackgroundColorSpan(color), matcher.start(),
                matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        );
    }

    return highlighted;
}
 
Example #6
Source File: SpannableStringHelper.java    From revolution-irc with GNU General Public License v3.0 6 votes vote down vote up
public static JsonObject spanToJson(Object span) {
    JsonObject ret = new JsonObject();
    if (span instanceof ForegroundColorSpan) {
        ret.addProperty("type", SPAN_TYPE_FOREGROUND);
        ret.addProperty("color", ((ForegroundColorSpan) span).getForegroundColor());
    } else if (span instanceof BackgroundColorSpan) {
        ret.addProperty("type", SPAN_TYPE_BACKGROUND);
        ret.addProperty("color", ((BackgroundColorSpan) span).getBackgroundColor());
    } else if (span instanceof StyleSpan) {
        ret.addProperty("type", SPAN_TYPE_STYLE);
        ret.addProperty("style", ((StyleSpan) span).getStyle());
    } else {
        return null;
    }
    return ret;
}
 
Example #7
Source File: PreTagHandler.java    From mvvm-template with GNU General Public License v3.0 6 votes vote down vote up
@Override public void handleTagNode(TagNode node, SpannableStringBuilder builder, int start, int end) {
    if (isPre) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("\n");//fake padding top + make sure, pre is always by itself
        getPlainText(buffer, node);
        buffer.append("\n");//fake padding bottom + make sure, pre is always by itself
        builder.append(replace(buffer.toString()));
        builder.append("\n");
        builder.setSpan(new CodeBackgroundRoundedSpan(color), start, builder.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.append("\n");
        this.appendNewLine(builder);
        this.appendNewLine(builder);
    } else {
        StringBuffer text = node.getText();
        builder.append(" ");
        builder.append(replace(text.toString()));
        builder.append(" ");
        final int stringStart = start + 1;
        final int stringEnd = builder.length() - 1;
        builder.setSpan(new BackgroundColorSpan(color), stringStart, stringEnd, SPAN_EXCLUSIVE_EXCLUSIVE);
        if (theme == PrefGetter.LIGHT) {
            builder.setSpan(new ForegroundColorSpan(Color.RED), stringStart, stringEnd, SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        builder.setSpan(new TypefaceSpan("monospace"), stringStart, stringEnd, SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}
 
Example #8
Source File: Html.java    From tysq-android with GNU General Public License v3.0 6 votes vote down vote up
private static void endCssStyle(Editable text) {
    Strikethrough s = getLast(text, Strikethrough.class);
    if (s != null) {
        setSpanFromMark(text, s, new JerryStrikeThroughSpan());
    }

    Background b = getLast(text, Background.class);
    if (b != null) {
        setSpanFromMark(text, b, new BackgroundColorSpan(b.mBackgroundColor));
    }

    Foreground f = getLast(text, Foreground.class);
    if (f != null) {
        setSpanFromMark(text, f, new ForegroundColorSpan(f.mForegroundColor));
    }
}
 
Example #9
Source File: QaActivity.java    From tflite-android-transformers with Apache License 2.0 6 votes vote down vote up
private void presentAnswer(QaAnswer answer) {
  // Highlight answer.
  Spannable spanText = new SpannableString(content);
  int offset = content.indexOf(answer.text, 0);
  if (offset >= 0) {
    spanText.setSpan(
        new BackgroundColorSpan(getColor(R.color.secondaryColor)),
        offset,
        offset + answer.text.length(),
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  contentTextView.setText(spanText);

  // Use TTS to speak out the answer.
  if (textToSpeech != null) {
    textToSpeech.speak(answer.text, TextToSpeech.QUEUE_FLUSH, null, answer.text);
  }
}
 
Example #10
Source File: HtmlToSpannedConverter.java    From HtmlCompat with Apache License 2.0 6 votes vote down vote up
private void endCssStyle(String tag, Editable text) {
    Strikethrough s = getLast(text, Strikethrough.class);
    if (s != null) {
        setSpanFromMark(tag, text, s, new StrikethroughSpan());
    }
    Background b = getLast(text, Background.class);
    if (b != null) {
        setSpanFromMark(tag, text, b, new BackgroundColorSpan(b.mBackgroundColor));
    }
    Foreground f = getLast(text, Foreground.class);
    if (f != null) {
        setSpanFromMark(tag, text, f, new ForegroundColorSpan(f.mForegroundColor));
    }
    AbsoluteSize a = getLast(text, AbsoluteSize.class);
    if (a != null) {
        setSpanFromMark(tag, text, a, new AbsoluteSizeSpan(a.getTextSize()));
    }
    RelativeSize r = getLast(text, RelativeSize.class);
    if (r != null) {
        setSpanFromMark(tag, text, r, new RelativeSizeSpan(r.getTextProportion()));
    }
}
 
Example #11
Source File: SpannableSerializer.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
private static JSONArray extractClass(final SpannableString ss, final Class<? extends CharacterStyle> clz) {
    val array = new JSONArray();
    val spansBg = ss.getSpans(0, ss.length(), clz);
    for (val span : spansBg) {
        int col;
        switch (clz.getSimpleName()) {
            case "BackgroundColorSpan":
                col = ((BackgroundColorSpan) span).getBackgroundColor();
                break;
            case "ForegroundColorSpan":
                col = ((ForegroundColorSpan) span).getForegroundColor();
                break;
            default:
                throw new RuntimeException("Cant match extract class type: " + clz.getSimpleName());
        }
        pullSpanColor(ss, span, col, array);
    }
    return array;
}
 
Example #12
Source File: SearchResult.java    From IslamicLibraryAndroid with GNU General Public License v3.0 6 votes vote down vote up
public CharSequence getformatedSearchSnippet() {
    //TODO implement improved snippet function
    String CleanedSearchString = " " + ArabicUtilities.cleanTextForSearchingWthStingBuilder(searchString) + " ";
    StringBuilder cleanedUnformattedPage = new StringBuilder(ArabicUtilities.cleanTextForSearchingWthStingBuilder(unformatedPage));
    int firstMatchStart = cleanedUnformattedPage.indexOf(CleanedSearchString);
    cleanedUnformattedPage.delete(0, Math.max(firstMatchStart - 100, 0));
    cleanedUnformattedPage.delete(
            Math.min(firstMatchStart + CleanedSearchString.length() + 100, cleanedUnformattedPage.length())
            , cleanedUnformattedPage.length());
    cleanedUnformattedPage.insert(0, "...");
    cleanedUnformattedPage.append("...");

    Spannable snippet = SpannableString.
            valueOf(cleanedUnformattedPage.toString());
    int index = TextUtils.indexOf(snippet, CleanedSearchString);
    while (index >= 0) {

        snippet.setSpan(new BackgroundColorSpan(0xFF8B008B), index, index
                + CleanedSearchString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        index = TextUtils.indexOf(snippet, CleanedSearchString, index + CleanedSearchString.length());
    }

    return snippet;
}
 
Example #13
Source File: InputLogic.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 6 votes vote down vote up
/**
 * Equivalent to {@link #setComposingTextInternal(CharSequence, int)} except that this method
 * allows to set {@link BackgroundColorSpan} to the composing text with the given color.
 *
 * <p>TODO: Currently the background color is exclusive with the black underline, which is
 * automatically added by the framework. We need to change the framework if we need to have both
 * of them at the same time.</p>
 * <p>TODO: Should we move this method to {@link RichInputConnection}?</p>
 *
 * @param newComposingText the composing text to be set
 * @param newCursorPosition the new cursor position
 * @param backgroundColor the background color to be set to the composing text. Set
 * {@link Color#TRANSPARENT} to disable the background color.
 * @param coloredTextLength the length of text, in Java chars, which should be rendered with
 * the given background color.
 */
private void setComposingTextInternalWithBackgroundColor(final CharSequence newComposingText,
        final int newCursorPosition, final int backgroundColor, final int coloredTextLength) {
    final CharSequence composingTextToBeSet;
    if (backgroundColor == Color.TRANSPARENT) {
        composingTextToBeSet = newComposingText;
    } else {
        final SpannableString spannable = new SpannableString(newComposingText);
        final BackgroundColorSpan backgroundColorSpan =
                new BackgroundColorSpan(backgroundColor);
        final int spanLength = Math.min(coloredTextLength, spannable.length());
        spannable.setSpan(backgroundColorSpan, 0, spanLength,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
        composingTextToBeSet = spannable;
    }
    mConnection.setComposingText(composingTextToBeSet, newCursorPosition);
}
 
Example #14
Source File: InputLogic.java    From openboard with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Equivalent to {@link #setComposingTextInternal(CharSequence, int)} except that this method
 * allows to set {@link BackgroundColorSpan} to the composing text with the given color.
 *
 * <p>TODO: Currently the background color is exclusive with the black underline, which is
 * automatically added by the framework. We need to change the framework if we need to have both
 * of them at the same time.</p>
 * <p>TODO: Should we move this method to {@link RichInputConnection}?</p>
 *
 * @param newComposingText the composing text to be set
 * @param newCursorPosition the new cursor position
 * @param backgroundColor the background color to be set to the composing text. Set
 * {@link Color#TRANSPARENT} to disable the background color.
 * @param coloredTextLength the length of text, in Java chars, which should be rendered with
 * the given background color.
 */
private void setComposingTextInternalWithBackgroundColor(final CharSequence newComposingText,
        final int newCursorPosition, final int backgroundColor, final int coloredTextLength) {
    final CharSequence composingTextToBeSet;
    if (backgroundColor == Color.TRANSPARENT) {
        composingTextToBeSet = newComposingText;
    } else {
        final SpannableString spannable = new SpannableString(newComposingText);
        final BackgroundColorSpan backgroundColorSpan =
                new BackgroundColorSpan(backgroundColor);
        final int spanLength = Math.min(coloredTextLength, spannable.length());
        spannable.setSpan(backgroundColorSpan, 0, spanLength,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
        composingTextToBeSet = spannable;
    }
    mConnection.setComposingText(composingTextToBeSet, newCursorPosition);
}
 
Example #15
Source File: Html.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private static void endCssStyle(Editable text) {
    Strikethrough s = getLast(text, Strikethrough.class);
    if (s != null) {
        setSpanFromMark(text, s, new StrikethroughSpan());
    }

    Background b = getLast(text, Background.class);
    if (b != null) {
        setSpanFromMark(text, b, new BackgroundColorSpan(b.mBackgroundColor));
    }

    Foreground f = getLast(text, Foreground.class);
    if (f != null) {
        setSpanFromMark(text, f, new ForegroundColorSpan(f.mForegroundColor));
    }
}
 
Example #16
Source File: SpannableSerializer.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
private static JSONArray extractClass(final SpannableString ss, final Class<? extends CharacterStyle> clz) {
    val array = new JSONArray();
    val spansBg = ss.getSpans(0, ss.length(), clz);
    for (val span : spansBg) {
        int col;
        switch (clz.getSimpleName()) {
            case "BackgroundColorSpan":
                col = ((BackgroundColorSpan) span).getBackgroundColor();
                break;
            case "ForegroundColorSpan":
                col = ((ForegroundColorSpan) span).getForegroundColor();
                break;
            default:
                throw new RuntimeException("Cant match extract class type: " + clz.getSimpleName());
        }
        pullSpanColor(ss, span, col, array);
    }
    return array;
}
 
Example #17
Source File: Html.java    From ForPDA with GNU General Public License v3.0 6 votes vote down vote up
private static void endCssStyle(Editable text) {
    Font font = getLast(text, Font.class);
    if (font != null) {
        if (font.mFace.equalsIgnoreCase("fontello")) {
            setSpanFromMark(text, font, new AssetsTypefaceSpan(App.getContext(), "fontello/fontello.ttf"));
        }
    }
    Strikethrough s = getLast(text, Strikethrough.class);
    if (s != null) {
        setSpanFromMark(text, s, new StrikethroughSpan());
    }
    Background b = getLast(text, Background.class);
    if (b != null) {
        setSpanFromMark(text, b, new BackgroundColorSpan(b.mBackgroundColor));
    }
    Foreground f = getLast(text, Foreground.class);
    if (f != null) {
        setSpanFromMark(text, f, new ForegroundColorSpan(f.mForegroundColor));
    }

}
 
Example #18
Source File: ConverterSpannedToHtml.java    From memoir with Apache License 2.0 6 votes vote down vote up
private void handleEndTag(CharacterStyle style) {
    if (style instanceof URLSpan) {
        mOut.append("</a>");
    } else if (style instanceof TypefaceSpan) {
        mOut.append("</font>");
    } else if (style instanceof ForegroundColorSpan) {
        mOut.append("</font>");
    } else if (style instanceof BackgroundColorSpan) {
        mOut.append("</font>");
    } else if (style instanceof AbsoluteSizeSpan) {
        mOut.append("</font>");
    } else if (style instanceof StrikethroughSpan) {
        mOut.append("</strike>");
    } else if (style instanceof SubscriptSpan) {
        mOut.append("</sub>");
    } else if (style instanceof SuperscriptSpan) {
        mOut.append("</sup>");
    } else if (style instanceof UnderlineSpan) {
        mOut.append("</u>");
    } else if (style instanceof BoldSpan) {
        mOut.append("</b>");
    } else if (style instanceof ItalicSpan) {
        mOut.append("</i>");
    }
}
 
Example #19
Source File: SpannableStringHelper.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
public static Object spanFromJson(JsonObject obj) {
    String type = obj.get("type").getAsString();
    if (type.equals(SPAN_TYPE_FOREGROUND))
        return new ForegroundColorSpan(obj.get("color").getAsNumber().intValue());
    if (type.equals(SPAN_TYPE_BACKGROUND))
        return new BackgroundColorSpan(obj.get("color").getAsNumber().intValue());
    if (type.equals(SPAN_TYPE_STYLE))
        return new StyleSpan(obj.get("style").getAsNumber().intValue());
    return null;
}
 
Example #20
Source File: SpannableSerializer.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static String serializeSpannableString(final SpannableString ss) {
    val json = new JSONObject();
    try {
        json.put("mText", ss.toString());
        json.put("bgc", extractClass(ss, BackgroundColorSpan.class));
        json.put("fgc", extractClass(ss, ForegroundColorSpan.class));

    } catch (JSONException e) {
        // we're done for if we hit here
    }
    return json.toString();
}
 
Example #21
Source File: DanamakuAdapter.java    From GSYVideoPlayer with Apache License 2.0 5 votes vote down vote up
private SpannableStringBuilder createSpannable(Drawable drawable) {
    String text = "bitmap";
    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);
    ImageSpan span = new ImageSpan(drawable);//ImageSpan.ALIGN_BOTTOM);
    spannableStringBuilder.setSpan(span, 0, text.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    spannableStringBuilder.append("图文混排");
    spannableStringBuilder.setSpan(new BackgroundColorSpan(Color.parseColor("#8A2233B1")), 0, spannableStringBuilder.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    return spannableStringBuilder;
}
 
Example #22
Source File: SpanEZ.java    From SpanEZ with Apache License 2.0 5 votes vote down vote up
@Override
public StyleEZ backgroundColor(@NonNull Locator locator, @ColorRes int bgColorResId) {
    final int bgColor = ContextCompat.getColor(context, bgColorResId);
    BackgroundColorSpan bgColorSpan = new BackgroundColorSpan(bgColor);
    addMultipleSpan(locator, bgColorSpan);
    return this;
}
 
Example #23
Source File: SpannableStringHelper.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
public static Object cloneSpan(Object span) {
    if (span instanceof ForegroundColorSpan)
        return new ForegroundColorSpan(((ForegroundColorSpan) span).getForegroundColor());
    if (span instanceof BackgroundColorSpan)
        return new BackgroundColorSpan(((BackgroundColorSpan) span).getBackgroundColor());
    if (span instanceof StyleSpan)
        return new StyleSpan(((StyleSpan) span).getStyle());
    return null;
}
 
Example #24
Source File: LocalNoteAdapter.java    From nono-android with GNU General Public License v3.0 5 votes vote down vote up
private SpannableStringBuilder makeTitleBuilder(String title, int titleStart, int titleEnd) {
    SpannableStringBuilder titleBuilder = new SpannableStringBuilder(title);
    if(titleStart == -1 || titleEnd == -1) {
        return titleBuilder;
    }
    titleBuilder.setSpan(new BackgroundColorSpan(bg_color),titleStart,titleEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    return titleBuilder;
}
 
Example #25
Source File: EditView.java    From nono-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onSpanClick(SpanButton v) {
    View view=richEdit.findFocus();
    BaseRichEditText editText;
    if(view instanceof BaseRichEditText){
        editText=(BaseRichEditText)view;
    }else {
        return;
    }
    TypedValue typedValue = new TypedValue();
    getContext().getTheme().resolveAttribute(R.attr.colorAccent, typedValue, true);
    int color=typedValue.data;
    switch (v.getSpan()){
        case SpanButton.SPAN_BOLD:editText.toggleBold();if(editText.isBold()){editText.applyStyleSpan(Typeface.BOLD);v.setColor(color);}else {editText.removeSelectionSpan(Typeface.BOLD);v.setColor(0xff757575);}break;
        case SpanButton.SPAN_ITALIC:editText.toggleItalic();if(editText.isItalic()){editText.applyStyleSpan(Typeface.ITALIC);v.setColor(color);}else {editText.removeSelectionSpan(Typeface.ITALIC);v.setColor(0xff757575);}break;
        case SpanButton.SPAN_STRIKETHROUGH:editText.toggleStrikethrough();if(editText.isStrikethrough()){editText.applySelectionSpan(StrikethroughSpan.class);v.setColor(color);}else {editText.removeSelectionSpan(StrikethroughSpan.class);v.setColor(0xff757575);}break;
        case SpanButton.SPAN_UNDERLINE:editText.toggleUnderLine();if(editText.isUnderLine()){editText.applySelectionSpan(UnderlineSpan.class);v.setColor(color);}else {editText.removeSelectionSpan(UnderlineSpan.class);v.setColor(0xff757575);}break;
        case SpanButton.SPAN_FOREGROUND:editText.toggleForegroundColor();if(editText.isForegroundColor()){editText.applyColorSpan(ForegroundColorSpan.class,v.getGroundColor());v.setColor(v.getGroundColor());}else {editText.removeSelectionSpan(ForegroundColorSpan.class);v.setColor(0xff757575);}break;
        case SpanButton.SPAN_BACKGROUND:editText.toggleBackgroundColor();if(editText.isBackgroundColor()){editText.applyColorSpan(BackgroundColorSpan.class,v.getGroundColor());v.setColor(v.getGroundColor());}else {editText.removeSelectionSpan(BackgroundColorSpan.class);v.setColor(0xff757575);}break;
        case SpanButton.SPAN_SUBSCRIPT:editText.toggleSubscript();if(editText.isSubscript()){editText.applyScriptSpan(SubscriptSpan.class);v.setColor(color);}else {editText.removeSelectionSpan(SubscriptSpan.class);v.setColor(0xff757575);}break;
        case SpanButton.SPAN_SUPERSCRIPT:editText.toggleSuperscript();if(editText.isSuperscript()){editText.applyScriptSpan(SuperscriptSpan.class);v.setColor(color);}else {editText.removeSelectionSpan(SuperscriptSpan.class);v.setColor(0xff757575);}break;
        case SpanButton.SPAN_TODO:richEdit.addTodoLayout((BaseContainer) richEdit.findFocus().getParent().getParent());break;
        case SpanButton.SPAN_DOT:richEdit.addDotLayout((BaseContainer) richEdit.findFocus().getParent().getParent());break;
        case SpanButton.SPAN_NUMERIC:richEdit.addNumericLayout((BaseContainer) richEdit.findFocus().getParent().getParent());break;
        case SpanButton.SPAN_PHOTO:richEdit.addPhotoLayout((BaseContainer) richEdit.findFocus().getParent().getParent());break;
        //case SpanButton.SPAN_TEXTSIZE:editText.changeSize();editText.applyRelativeSpan();break;
        //case SpanButton.SPAN_BOLD:editText.toggleBold();if(editText.isBold()){editText.applyStyleSpan(Typeface.BOLD);v.setColor(color);}else {editText.removeSelectionSpan(Typeface.BOLD);v.setColor(0xff757575);}break;
    }
}
 
Example #26
Source File: Prism4jThemeDefault.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@Override
protected void applyColor(
        @NonNull String language,
        @NonNull String type,
        @Nullable String alias,
        @ColorInt int color,
        @NonNull SpannableStringBuilder builder,
        int start,
        int end) {

    if ("css".equals(language) && isOfType("string", type, alias)) {
        super.applyColor(language, type, alias, 0xFF9a6e3a, builder, start, end);
        builder.setSpan(new BackgroundColorSpan(0x80ffffff), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return;
    }

    if (isOfType("namespace", type, alias)) {
        color = applyAlpha(.7F, color);
    }

    super.applyColor(language, type, alias, color, builder, start, end);

    if (isOfType("important", type, alias)
            || isOfType("bold", type, alias)) {
        builder.setSpan(new StrongEmphasisSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    if (isOfType("italic", type, alias)) {
        builder.setSpan(new EmphasisSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}
 
Example #27
Source File: LocalNoteAdapter.java    From nono-android with GNU General Public License v3.0 5 votes vote down vote up
private SpannableStringBuilder makeGroupBuilder(String group, int groupStart, int groupEnd) {
    SpannableStringBuilder groupBuilder = new SpannableStringBuilder(group);
    if(groupStart == -1 || groupEnd == -1){
        return groupBuilder;
    }
    groupBuilder.setSpan(new BackgroundColorSpan(bg_color),groupStart,groupEnd,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    return groupBuilder;
}
 
Example #28
Source File: MenuMainDemoFragment.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void highlightText(TextView textView) {
  Context context = textView.getContext();
  CharSequence text = textView.getText();
  TypedValue value = new TypedValue();
  context.getTheme().resolveAttribute(R.attr.colorPrimary, value, true);
  Spannable spanText = Spannable.Factory.getInstance().newSpannable(text);
  spanText.setSpan(
      new BackgroundColorSpan(value.data), 0, text.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
  textView.setText(spanText);
}
 
Example #29
Source File: BetterLinkMovementExtended.java    From mvvm-template with GNU General Public License v3.0 5 votes vote down vote up
private void highlightUrl(TextView textView, BetterLinkMovementExtended.ClickableSpanWithText spanWithText, Spannable text) {
    if (!this.isUrlHighlighted) {
        this.isUrlHighlighted = true;
        int spanStart = text.getSpanStart(spanWithText.span());
        int spanEnd = text.getSpanEnd(spanWithText.span());
        Selection.removeSelection(text);
        text.setSpan(new BackgroundColorSpan(textView.getHighlightColor()), spanStart, spanEnd, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        textView.setText(text);
        Selection.setSelection(text, spanStart, spanEnd);
    }
}
 
Example #30
Source File: ConversationItem.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private void setBodyText(MessageRecord messageRecord, @Nullable String searchQuery) {
  bodyText.setClickable(false);
  bodyText.setFocusable(false);
  bodyText.setTextSize(TypedValue.COMPLEX_UNIT_SP, TextSecurePreferences.getMessageBodyTextSize(context));
  bodyText.setMovementMethod(LongClickMovementMethod.getInstance(getContext()));

  if (messageRecord.isRemoteDelete()) {
    String deletedMessage = context.getString(R.string.ConversationItem_this_message_was_deleted);
    SpannableString italics = new SpannableString(deletedMessage);
    italics.setSpan(new RelativeSizeSpan(0.9f), 0, deletedMessage.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    italics.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, deletedMessage.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    bodyText.setText(italics);
  } else if (isCaptionlessMms(messageRecord)) {
    bodyText.setVisibility(View.GONE);
  } else {
    Spannable styledText = linkifyMessageBody(messageRecord.getDisplayBody(getContext()), batchSelected.isEmpty());
    styledText = SearchUtil.getHighlightedSpan(locale, () -> new BackgroundColorSpan(Color.YELLOW), styledText, searchQuery);
    styledText = SearchUtil.getHighlightedSpan(locale, () -> new ForegroundColorSpan(Color.BLACK), styledText, searchQuery);

    if (hasExtraText(messageRecord)) {
      bodyText.setOverflowText(getLongMessageSpan(messageRecord));
    } else {
      bodyText.setOverflowText(null);
    }

    bodyText.setText(styledText);
    bodyText.setVisibility(View.VISIBLE);
  }
}