Example usage for android.text Editable replace

List of usage examples for android.text Editable replace

Introduction

In this page you can find the example usage for android.text Editable replace.

Prototype

public Editable replace(int st, int en, CharSequence text);

Source Link

Document

Convenience for replace(st, en, text, 0, text.length())

Usage

From source file:me.albertonicoletti.latex.activities.EditorActivity.java

/**
 * Used to maintain the same indentation as the upper line
 * @param editable Text/*from  ww w .  j  av a 2 s  .  c om*/
 * @param spannable Spannable
 * @param span Modified span
 */
private void autoIndentAndTabEditor(Editable editable, SpannableString spannable, RelativeSizeSpan span) {
    int beginIndex = spannable.getSpanStart(span);
    int endIndex = spannable.getSpanEnd(span);
    // If the last written character is a newline
    if (editable.length() > 0) {
        if (editable.charAt(endIndex - 1) == '\n') {
            int lineModified = editor.getLayout().getLineForOffset(beginIndex);
            int modifiedBeginIndex = editor.getLayout().getLineStart(lineModified);
            int modifiedEndIndex = editor.getLayout().getLineEnd(lineModified);
            String str = editable.subSequence(modifiedBeginIndex, modifiedEndIndex).toString();
            // Collects the whitespaces and tabulations in the upper line
            String whitespaces = "";
            int i = 0;
            while (str.charAt(i) == ' ' || str.charAt(i) == '\t') {
                whitespaces += str.charAt(i);
                i++;
            }
            // And inserts them in the newline
            editable.insert(beginIndex + 1, whitespaces);
        }
        if (editable.charAt(endIndex - 1) == '\t') {
            int tabSize = Integer.valueOf(PreferenceManager.getDefaultSharedPreferences(this)
                    .getString(SettingsActivity.TAB_SIZE, ""));
            String whitespaces = "";
            for (int i = 0; i < tabSize; i++) {
                whitespaces += " ";
            }
            editable.replace(beginIndex, beginIndex + 1, whitespaces);
        }
    }
}

From source file:com.android.ex.chips.RecipientEditTextView.java

private void submitItemAtPosition(final int position) {
    final RecipientEntry entry = createValidatedEntry(getAdapter().getItem(position));
    if (entry == null)
        return;//from   w w w.  j ava 2s. com
    clearComposingText();
    final int end = getSelectionEnd();
    final int start = mTokenizer.findTokenStart(getText(), end);
    final Editable editable = getText();
    QwertyKeyListener.markAsReplaced(editable, start, end, "");
    final CharSequence chip = createChip(entry, false);
    if (chip != null && start >= 0 && end >= 0)
        editable.replace(start, end, chip);
    sanitizeBetween();
    if (mChipListener != null)
        mChipListener.onDataChanged();
}

From source file:com.android.ex.chips.RecipientEditTextView.java

private void handleEdit(final int start, final int end) {
    if (start == -1 || end == -1) {
        // This chip no longer exists in the field.
        dismissDropDown();/*w  w  w  .  jav a2s.  co m*/
        return;
    }
    // This is in the middle of a chip, so select out the whole chip
    // and commit it.
    final Editable editable = getText();
    setSelection(end);
    final String text = getText().toString().substring(start, end);
    if (!TextUtils.isEmpty(text)) {
        final RecipientEntry entry = RecipientEntry.constructFakeEntry(text, isValid(text));
        QwertyKeyListener.markAsReplaced(editable, start, end, "");
        final CharSequence chipText = createChip(entry, false);
        final int selEnd = getSelectionEnd();
        if (chipText != null && start > -1 && selEnd > -1)
            editable.replace(start, selEnd, chipText);
    }
    dismissDropDown();
}

From source file:com.android.ex.chips.RecipientEditTextView.java

void createMoreChipPlainText() {
    // Take the first <= CHIP_LIMIT addresses and get to the end of the second one.
    final Editable text = getText();
    int start = 0;
    int end = start;
    for (int i = 0; i < CHIP_LIMIT; i++) {
        end = movePastTerminators(mTokenizer.findTokenEnd(text, start));
        start = end; // move to the next token and get its end.
    }/*  w  ww .  ja  va  2 s  .c om*/
    // Now, count total addresses.
    start = 0;
    final int tokenCount = countTokens(text);
    final MoreImageSpan moreSpan = createMoreSpan(tokenCount - CHIP_LIMIT);
    final SpannableString chipText = new SpannableString(text.subSequence(end, text.length()));
    chipText.setSpan(moreSpan, 0, chipText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    text.replace(end, text.length(), chipText);
    mMoreChip = moreSpan;
}

From source file:com.android.ex.chips.RecipientEditTextView.java

/**
 * Replace this currently selected chip with a new chip that uses the contact data provided.
 *///from   w w w .  j  a  v a2  s  . co m
// Visible for testing.
/* package */void replaceChip(final DrawableRecipientChip chip, final RecipientEntry entry) {
    final boolean wasSelected = chip == mSelectedChip;
    if (wasSelected)
        mSelectedChip = null;
    final int start = getChipStart(chip);
    final int end = getChipEnd(chip);
    getSpannable().removeSpan(chip);
    final Editable editable = getText();
    final CharSequence chipText = createChip(entry, false);
    if (chipText != null)
        if (start == -1 || end == -1) {
            Log.e(TAG, "The chip to replace does not exist but should.");
            editable.insert(0, chipText);
        } else if (!TextUtils.isEmpty(chipText)) {
            // There may be a space to replace with this chip's new
            // associated space. Check for it
            int toReplace = end;
            while (toReplace >= 0 && toReplace < editable.length() && editable.charAt(toReplace) == ' ')
                toReplace++;
            editable.replace(start, toReplace, chipText);
        }
    setCursorVisible(true);
    if (wasSelected)
        clearSelectedChip();
    if (mChipListener != null)
        mChipListener.onDataChanged();
}

From source file:com.android.ex.chips.RecipientEditTextView.java

/**
 * Create the more chip. The more chip is text that replaces any chips that do not fit in the pre-defined available
 * space when the RecipientEditTextView loses focus.
 *//*from w ww.j a v  a2  s .c  o m*/
// Visible for testing.
/* package */void createMoreChip() {
    if (mNoChips) {
        createMoreChipPlainText();
        return;
    }
    if (!mShouldShrink)
        return;
    final ImageSpan[] tempMore = getSpannable().getSpans(0, getText().length(), MoreImageSpan.class);
    if (tempMore.length > 0)
        getSpannable().removeSpan(tempMore[0]);
    final DrawableRecipientChip[] recipients = getSortedRecipients();
    if (recipients == null || recipients.length <= CHIP_LIMIT) {
        mMoreChip = null;
        return;
    }
    final Spannable spannable = getSpannable();
    final int numRecipients = recipients.length;
    final int overage = numRecipients - CHIP_LIMIT;
    final MoreImageSpan moreSpan = createMoreSpan(overage);
    mRemovedSpans = new ArrayList<DrawableRecipientChip>();
    int totalReplaceStart = 0;
    int totalReplaceEnd = 0;
    final Editable text = getText();
    for (int i = numRecipients - overage; i < recipients.length; i++) {
        mRemovedSpans.add(recipients[i]);
        if (i == numRecipients - overage)
            totalReplaceStart = spannable.getSpanStart(recipients[i]);
        if (i == recipients.length - 1)
            totalReplaceEnd = spannable.getSpanEnd(recipients[i]);
        if (mTemporaryRecipients == null || !mTemporaryRecipients.contains(recipients[i])) {
            final int spanStart = spannable.getSpanStart(recipients[i]);
            final int spanEnd = spannable.getSpanEnd(recipients[i]);
            recipients[i].setOriginalText(text.toString().substring(spanStart, spanEnd));
        }
        spannable.removeSpan(recipients[i]);
    }
    if (totalReplaceEnd < text.length())
        totalReplaceEnd = text.length();
    final int end = Math.max(totalReplaceStart, totalReplaceEnd);
    final int start = Math.min(totalReplaceStart, totalReplaceEnd);
    final SpannableString chipText = new SpannableString(text.subSequence(start, end));
    chipText.setSpan(moreSpan, 0, chipText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    text.replace(start, end, chipText);
    mMoreChip = moreSpan;
    // If adding the +more chip goes over the limit, resize accordingly.
    if (!isPhoneQuery() && getLineCount() > mMaxLines)
        setMaxLines(getLineCount());
}

From source file:com.android.ex.chips.RecipientEditTextView.java

private boolean commitChip(final int start, final int end, final Editable editable) {
    final ListAdapter adapter = getAdapter();
    if (adapter != null && adapter.getCount() > 0 && enoughToFilter() && end == getSelectionEnd()
            && !isPhoneQuery()) {
        // choose the first entry.
        submitItemAtPosition(0);// w  w  w.  j av  a  2  s.  co m
        dismissDropDown();
        return true;
    } else {
        int tokenEnd = mTokenizer.findTokenEnd(editable, start);
        if (editable.length() > tokenEnd + 1) {
            final char charAt = editable.charAt(tokenEnd + 1);
            if (charAt == COMMIT_CHAR_COMMA || charAt == COMMIT_CHAR_SEMICOLON || charAt == COMMIT_CHAR_SPACE) //// ---- Added by shreyash
                tokenEnd++;

        }

        //-----------------------
        final String text = editable.toString().substring(start, tokenEnd).trim();
        clearComposingText();
        if (text != null && text.length() > 0 && !text.equals(" ")) {
            final RecipientEntry entry = createTokenizedEntry(text);
            if (entry != null) {
                QwertyKeyListener.markAsReplaced(editable, start, end, "");
                final CharSequence chipText = createChip(entry, false);
                if (chipText != null && start > -1 && end > -1)
                    editable.replace(start, end, chipText);
            }
            // Only dismiss the dropdown if it is related to the text we
            // just committed.
            // For paste, it may not be as there are possibly multiple
            // tokens being added.
            if (end == getSelectionEnd())
                dismissDropDown();
            sanitizeBetween();
            return true;
        }
    }
    return false;
}