Java Code Examples for android.text.Editable#clearSpans()

The following examples show how to use android.text.Editable#clearSpans() . 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: ChipDrawableSpan.java    From lttrs-android with Apache License 2.0 5 votes vote down vote up
public static void reset(EditText editText) {
    final Editable editable = editText.getEditableText();
    if (TextUtils.isEmpty(editable)) {
        return;
    }
    editable.clearSpans();
    ChipDrawableSpan.apply(editText.getContext(), editable, editText.hasFocus());
}
 
Example 2
Source File: BaseThreadAndArticleAdapter.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
public void setColoredAdContent(TextView contentText, final ForumAdJson forumAd) {
    String subject = StringUtils.get(forumAd.getContent());
    Editable editable = contentText.getEditableText();
    if (editable != null) {
        editable.clear();
        editable.clearSpans();
    }
    final String recomName = forumAd.getRecomName();
    SpannableStringBuilder ssb = new SpannableStringBuilder(subject + recomName);
    Drawable bg = context.getResources().getDrawable(R.drawable.bg_recom);
    ImageSpan imageSpan = new ImageSpan(bg) {
        @Override
        public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y,
                         int bottom, Paint paint) {
            paint.setTypeface(Typeface.DEFAULT);
            int textSize = DensityUtils.dip2px(context, 12);
            paint.setTextSize(textSize);
            Rect bounds = new Rect();
            paint.getTextBounds(text.toString(), start, end, bounds);
            getDrawable().setBounds(0, 0, bounds.width() + 10, bottom - top);
            super.draw(canvas, text, start, end, x, top, y, bottom, paint);
            paint.setColor(Color.TRANSPARENT);
            canvas.drawText(text.subSequence(start, end).toString(), x + 5, y, paint);
        }
    };
    ssb.setSpan(imageSpan, subject.length(), subject.length() + recomName.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    contentText.setText(ssb);

}
 
Example 3
Source File: SpanHelper.java    From AwesomeValidation with MIT License 5 votes vote down vote up
public static void reset(EditText editText) {
    Editable editable = editText.getText();
    if (editable != null) {
        editable.clearSpans();
    }
    editText.setText(editable);
}
 
Example 4
Source File: CodeTextView.java    From android-quiz-php with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private Editable highlight( Editable e )
    {
        try
        {
            e.clearSpans();

            if( e.length() == 0 )
                return e;

            for( Matcher m = numbers.matcher( e );
                 m.find(); )
                e.setSpan(
                        new ForegroundColorSpan(Color.parseColor(COLOR_NUMBER)),
                        m.start(),
                        m.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );

            for( Matcher m = keywords.matcher( e );
                 m.find(); )
                e.setSpan(
                        new ForegroundColorSpan(Color.parseColor(COLOR_KEYWORD)),
                        m.start(),
                        m.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );

            for( Matcher m = variables.matcher( e );
                 m.find(); )
                e.setSpan(
                        new ForegroundColorSpan(Color.parseColor(COLOR_VARIABLE)),
                        m.start(),
                        m.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );

            for( Matcher m = builtins.matcher( e );
                 m.find(); )
                e.setSpan(
                        new ForegroundColorSpan( COLOR_BUILTIN ),
                        m.start(),
                        m.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );

            for( Matcher m = strings.matcher( e );
                 m.find(); )
                e.setSpan(
                        new ForegroundColorSpan(Color.parseColor(COLOR_STRING)),
                        m.start(),
                        m.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );

            for( Matcher m = comments.matcher( e );
                 m.find(); )
                e.setSpan(
                        new ForegroundColorSpan( COLOR_COMMENT ),
                        m.start(),
                        m.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
        }
        catch( Exception ex )
        {
        }
//
//        e.setSpan(
//                new BackgroundColorSpan( COLOR_COMMENT ),
//                1, e.length() -1,
//                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );

//        e.setSpan(new TypefaceSpan("monospace"), 0, e.length() -1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
        return e;
    }
 
Example 5
Source File: Editor.java    From turbo-editor with GNU General Public License v3.0 4 votes vote down vote up
public Editable highlight(Editable editable, boolean newText) {
    editable.clearSpans();

    if (editable.length() == 0) {
        return editable;
    }

    editorHeight = getHeight();

    if (!newText && editorHeight > 0) {
        firstVisibleIndex = getLayout().getLineStart(LineUtils.Companion.getFirstVisibleLine(MainActivity.verticalScroll, editorHeight, lineCount));
        lastVisibleIndex = getLayout().getLineEnd(LineUtils.Companion.getLastVisibleLine(MainActivity.verticalScroll, editorHeight, lineCount, deviceHeight) - 1);
    } else {
        firstVisibleIndex = 0;
        lastVisibleIndex = CHARS_TO_COLOR;
    }

    firstColoredIndex = firstVisibleIndex - (CHARS_TO_COLOR / 5);

    // normalize
    if (firstColoredIndex < 0)
        firstColoredIndex = 0;
    if (lastVisibleIndex > editable.length())
        lastVisibleIndex = editable.length();
    if (firstColoredIndex > lastVisibleIndex)
        firstColoredIndex = lastVisibleIndex;

    textToHighlight = editable.subSequence(firstColoredIndex, lastVisibleIndex);

    if (TextUtils.isEmpty(MainActivity.Companion.getFileExtension()))
        MainActivity.Companion.setFileExtension("");

    HighlightDriver highlightDriver = new HighlightDriver(new AndroidHighlightColorProvider(),
            MainActivity.Companion.getFileExtension());

    List<HighlightInfo> highlights = highlightDriver.highlightText(textToHighlight, firstColoredIndex);
    for (HighlightInfo info : highlights) {
        editable.setSpan(
                new ForegroundColorSpan(info.getColor()),
                info.getStart(),
                info.getEnd(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return editable;
}