Example usage for android.text Spannable setSpan

List of usage examples for android.text Spannable setSpan

Introduction

In this page you can find the example usage for android.text Spannable setSpan.

Prototype

public void setSpan(Object what, int start, int end, int flags);

Source Link

Document

Attach the specified markup object to the range start…end of the text, or move the object to that range if it was already attached elsewhere.

Usage

From source file:com.silentcircle.accounts.AccountStep1.java

private void stripUnderlines(TextView textView) {
    Spannable s = (Spannable) textView.getText();
    URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
    for (URLSpan span : spans) {
        int start = s.getSpanStart(span);
        int end = s.getSpanEnd(span);
        s.removeSpan(span);//from w w  w  .  j a  v a  2  s .c o m
        span = new URLSpanNoUnderline(span.getURL());
        s.setSpan(span, start, end, 0);
    }
    textView.setText(s);
}

From source file:com.ruesga.rview.tasks.AsyncTextDiffProcessor.java

private CharSequence processHighlightTrailingSpaces(CharSequence text) {
    if (!mHighlightTrailingWhitespaces) {
        return text;
    }/*from w ww .ja va2 s. c om*/

    int color = ContextCompat.getColor(mContext, R.color.diffHighlightColor);
    final Spannable.Factory spannableFactory = Spannable.Factory.getInstance();
    String line = text.toString();
    final Matcher matcher = HIGHLIGHT_TRAIL_SPACES_PATTERN.matcher(line);
    if (matcher.find()) {
        int start = matcher.start();
        int end = matcher.end();
        Spannable span = spannableFactory.newSpannable(text);
        span.setSpan(new BackgroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return span;
    }
    return text;
}

From source file:android.support.text.emoji.EmojiProcessor.java

private void addEmoji(@NonNull final Spannable spannable, final EmojiMetadata metadata, final int start,
        final int end) {
    final EmojiSpan span = mSpanFactory.createSpan(metadata);
    spannable.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

From source file:com.android.inputmethod.latin.suggestions.SuggestionStripLayoutHelper.java

private CharSequence getStyledSuggestedWord(final SuggestedWords suggestedWords,
        final int indexInSuggestedWords) {
    if (indexInSuggestedWords >= suggestedWords.size()) {
        return null;
    }//w w  w.java 2s  .co m
    final String word = suggestedWords.getLabel(indexInSuggestedWords);
    // TODO: don't use the index to decide whether this is the auto-correction/typed word, as
    // this is brittle
    final boolean isAutoCorrection = suggestedWords.mWillAutoCorrect
            && indexInSuggestedWords == SuggestedWords.INDEX_OF_AUTO_CORRECTION;
    final boolean isTypedWordValid = suggestedWords.mTypedWordValid
            && indexInSuggestedWords == SuggestedWords.INDEX_OF_TYPED_WORD;
    if (!isAutoCorrection && !isTypedWordValid) {
        return word;
    }

    final int len = word.length();
    final Spannable spannedWord = new SpannableString(word);
    final int options = mSuggestionStripOptions;
    if ((isAutoCorrection && (options & AUTO_CORRECT_BOLD) != 0)
            || (isTypedWordValid && (options & VALID_TYPED_WORD_BOLD) != 0)) {
        spannedWord.setSpan(BOLD_SPAN, 0, len, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    }
    if (isAutoCorrection && (options & AUTO_CORRECT_UNDERLINE) != 0) {
        spannedWord.setSpan(UNDERLINE_SPAN, 0, len, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    }
    return spannedWord;
}

From source file:net.idlesoft.android.apps.github.activities.Repository.java

public void loadRepoInfo() {
    try {// w  w w  . j av a2 s.c o m
        // TextView title =
        // (TextView)findViewById(R.id.tv_top_bar_title);
        // title.setText(m_jsonData.getString("name"));
        final TextView repo_name = (TextView) findViewById(R.id.tv_repository_info_name);
        repo_name.setText(mJson.getString("name"));
        repo_name.requestFocus();
        final TextView repo_desc = (TextView) findViewById(R.id.tv_repository_info_description);
        repo_desc.setText(mJson.getString("description"));
        final TextView repo_owner = (TextView) findViewById(R.id.tv_repository_info_owner);
        repo_owner.setText(mJson.getString("owner"));
        final TextView repo_watcher_count = (TextView) findViewById(R.id.tv_repository_info_watchers);
        if (mJson.getInt("watchers") == 1) {
            repo_watcher_count.setText(mJson.getInt("watchers") + " watcher");
        } else {
            repo_watcher_count.setText(mJson.getInt("watchers") + " watchers");
        }
        final TextView repo_fork_count = (TextView) findViewById(R.id.tv_repository_info_forks);
        if (mJson.getInt("forks") == 1) {
            repo_fork_count.setText(mJson.getInt("forks") + " fork");
        } else {
            repo_fork_count.setText(mJson.getInt("forks") + " forks");
        }
        final TextView repo_website = (TextView) findViewById(R.id.tv_repository_info_website);
        if (mJson.getString("homepage") != "") {
            repo_website.setText(mJson.getString("homepage"));
        } else {
            repo_website.setText("N/A");
        }

        /* Make the repository owner text link to his/her profile */
        repo_owner.setMovementMethod(LinkMovementMethod.getInstance());
        final Spannable spans = (Spannable) repo_owner.getText();
        final ClickableSpan clickSpan = new ClickableSpan() {
            @Override
            public void onClick(final View widget) {
                final Intent i = new Intent(Repository.this, Profile.class);
                try {
                    i.putExtra("username", mJson.getString("owner"));
                } catch (final JSONException e) {
                    e.printStackTrace();
                }
                startActivity(i);
            }
        };
        spans.setSpan(clickSpan, 0, spans.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    } catch (final JSONException e) {
        e.printStackTrace();
    }

    ((Button) findViewById(R.id.btn_repository_info_branches)).setOnClickListener(new OnClickListener() {
        public void onClick(final View v) {
            final Intent intent = new Intent(Repository.this, BranchesList.class);
            intent.putExtra("repo_name", mRepositoryName);
            intent.putExtra("repo_owner", mRepositoryOwner);
            startActivity(intent);
        }
    });
    ((Button) findViewById(R.id.btn_repository_info_issues)).setOnClickListener(new OnClickListener() {
        public void onClick(final View v) {
            final Intent intent = new Intent(Repository.this, Issues.class);
            intent.putExtra("repo_name", mRepositoryName);
            intent.putExtra("repo_owner", mRepositoryOwner);
            startActivity(intent);
        }
    });
    ((Button) findViewById(R.id.btn_repository_info_network)).setOnClickListener(new OnClickListener() {
        public void onClick(final View v) {
            final Intent intent = new Intent(Repository.this, NetworkList.class);
            intent.putExtra("repo_name", mRepositoryName);
            intent.putExtra("username", mRepositoryOwner);
            startActivity(intent);
        }
    });
}

From source file:hku.fyp14017.blencode.ui.fragment.LookFragment.java

private void updateActionModeTitle() {
    int numberOfSelectedItems = adapter.getAmountOfCheckedItems();

    if (numberOfSelectedItems == 0) {
        actionMode.setTitle(actionModeTitle);
    } else {/*from w  w w  . j  a  v a 2s . co m*/
        String appendix = multipleItemAppendixActionMode;

        if (numberOfSelectedItems == 1) {
            appendix = singleItemAppendixActionMode;
        }

        String numberOfItems = Integer.toString(numberOfSelectedItems);
        String completeTitle = actionModeTitle + " " + numberOfItems + " " + appendix;

        int titleLength = actionModeTitle.length();

        Spannable completeSpannedTitle = new SpannableString(completeTitle);
        completeSpannedTitle.setSpan(
                new ForegroundColorSpan(
                        getResources().getColor(hku.fyp14017.blencode.R.color.actionbar_title_color)),
                titleLength + 1, titleLength + (1 + numberOfItems.length()),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        actionMode.setTitle(completeSpannedTitle);
    }
}

From source file:com.taobao.weex.dom.WXTextDomObject.java

/**
 * Adjust span range after truncate due to the wrong span range during span copy and slicing.
 * @param beforeTruncate The span before truncate
 * @param afterTruncate The span after truncate
 *///from w w  w .  j  a  v a2 s .c o  m
private void adjustSpansRange(@NonNull Spanned beforeTruncate, @NonNull Spannable afterTruncate) {
    Object[] spans = beforeTruncate.getSpans(0, beforeTruncate.length(), Object.class);
    for (Object span : spans) {
        int start = beforeTruncate.getSpanStart(span);
        int end = beforeTruncate.getSpanEnd(span);
        if (start == 0 && end == beforeTruncate.length()) {
            afterTruncate.removeSpan(span);
            afterTruncate.setSpan(span, 0, afterTruncate.length(), beforeTruncate.getSpanFlags(span));
        }
    }
}

From source file:com.ruesga.rview.tasks.AsyncTextDiffProcessor.java

private void processNoIntralineDataA(DiffContentInfo diff, DiffInfoModel m, String line, int color) {
    final Spannable.Factory spannableFactory = Spannable.Factory.getInstance();
    if (diff.a != null && diff.b != null && diff.a.length == 1 && diff.b.length == 1) {
        Spannable span = spannableFactory.newSpannable(prepareTabs(line));
        int z = diff.a[0].indexOf(diff.b[0]);
        if (z != -1) {
            if (z > 0) {
                span.setSpan(new BackgroundColorSpan(color), 0, z, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }//from ww w  . j  a v a  2s  . c o  m
            if (z + diff.b[0].length() < diff.a[0].length()) {
                z = z + diff.b[0].length();
                span.setSpan(new BackgroundColorSpan(color), z, diff.a[0].length(),
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        m.lineA = span;
    } else {
        m.lineA = prepareTabs(line);
    }
}

From source file:com.ruesga.rview.tasks.AsyncTextDiffProcessor.java

private void processNoIntralineDataB(DiffContentInfo diff, DiffInfoModel m, String line, int color) {
    final Spannable.Factory spannableFactory = Spannable.Factory.getInstance();
    if (diff.a != null && diff.b != null && diff.a.length == 1 && diff.b.length == 1) {
        Spannable span = spannableFactory.newSpannable(prepareTabs(line));
        int z = diff.b[0].indexOf(diff.a[0]);
        if (z != -1) {
            if (z > 0) {
                span.setSpan(new BackgroundColorSpan(color), 0, z, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }//from  ww w .ja  v  a  2  s .com
            if (z + diff.a[0].length() < diff.b[0].length()) {
                z = z + diff.a[0].length();
                span.setSpan(new BackgroundColorSpan(color), z, diff.b[0].length(),
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        m.lineB = span;
    } else {
        m.lineB = prepareTabs(line);
    }
}

From source file:com.bt.heliniumstudentapp.SettingsActivity.java

private void setTitles() {
    final Spannable prefGeneral = new SpannableString(getString(R.string.general));
    final Spannable prefCustomization = new SpannableString(getString(R.string.customization));
    final Spannable prefSchedule = new SpannableString(getString(R.string.schedule));
    final Spannable prefGrades = new SpannableString(getString(R.string.grades));
    final Spannable prefUpdate = new SpannableString(getString(R.string.updates));

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
        prefGeneral.setSpan(
                new ForegroundColorSpan(ContextCompat.getColor(this, MainActivity.accentSecondaryColor)), 0,
                prefGeneral.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        prefCustomization.setSpan(/* w ww  . ja  va2  s  .co m*/
                new ForegroundColorSpan(ContextCompat.getColor(this, MainActivity.accentSecondaryColor)), 0,
                prefCustomization.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        prefSchedule.setSpan(
                new ForegroundColorSpan(ContextCompat.getColor(this, MainActivity.accentSecondaryColor)), 0,
                prefSchedule.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        prefGrades.setSpan(
                new ForegroundColorSpan(ContextCompat.getColor(this, MainActivity.accentSecondaryColor)), 0,
                prefGrades.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        prefUpdate.setSpan(
                new ForegroundColorSpan(ContextCompat.getColor(this, MainActivity.accentSecondaryColor)), 0,
                prefUpdate.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    } else {
        prefGeneral.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, R.color.dark_grey_dark)), 0,
                prefGeneral.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        prefCustomization.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, R.color.dark_grey_dark)),
                0, prefCustomization.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        prefSchedule.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, R.color.dark_grey_dark)), 0,
                prefSchedule.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        prefGrades.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, R.color.dark_grey_dark)), 0,
                prefGrades.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        prefUpdate.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, R.color.dark_grey_dark)), 0,
                prefUpdate.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    findPreference("pref_general").setTitle(prefGeneral);
    findPreference("pref_customization").setTitle(prefCustomization);
    findPreference("pref_schedule").setTitle(prefSchedule);
    findPreference("pref_grades").setTitle(prefGrades);
    findPreference("pref_updates").setTitle(prefUpdate);
}