Example usage for android.text Editable charAt

List of usage examples for android.text Editable charAt

Introduction

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

Prototype

char charAt(int index);

Source Link

Document

Returns the char value at the specified index.

Usage

From source file:org.miaowo.miaowo.util.Html.java

private static void appendNewlines(Editable text, int minNewline) {
    final int len = text.length();

    if (len == 0) {
        return;//from   ww w  . j a v  a 2  s .co m
    }

    int existingNewlines = 0;
    for (int i = len - 1; i >= 0 && text.charAt(i) == '\n'; i--) {
        existingNewlines++;
    }

    for (int j = existingNewlines; j < minNewline; j++) {
        text.append("\n");
    }
}

From source file:dev.drsoran.moloko.fragments.ChangeTagsFragment.java

private void onListItemClick(ListView l, View v, int position, long id) {
    final ChangeTag tag = (ChangeTag) l.getAdapter().getItem(position);
    final Editable tagsEdit = editView.getEditableText();

    if (tag.isAvailable) {
        if (TextUtils.isEmpty(tagsEdit)) {
            tagsEdit.append(tag.tag);/*from  w w w .  j  a v a 2 s.  co m*/
        } else {
            final int trimmedLength = TextUtils.getTrimmedLength(tagsEdit);
            if (tagsEdit.charAt(trimmedLength - 1) == ',') {
                tagsEdit.append(tag.tag);
            } else {
                tagsEdit.append(", " + tag.tag);
            }
        }
    } else {
        // Cut the removed tag including any trailing ,
        String content = UIUtils.getTrimmedText(editView);
        content = content.replaceAll(tag.tag + "\\,*\\s*", Strings.EMPTY_STRING);

        editView.setText(content);
    }

    Selection.setSelection(tagsEdit, tagsEdit.length());
    updateTagList();
}

From source file:org.eyeseetea.malariacare.network.CustomParser.java

@Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
    if (tag.equalsIgnoreCase("ul")) {
        if (opening) {
            lists.push(tag);/*from www . j a v a  2s . c  o m*/
        } else {
            lists.pop();
        }
    } else if (tag.equalsIgnoreCase("ol")) {
        if (opening) {
            lists.push(tag);
            olNextIndex.push(Integer.valueOf(1)).toString();//TODO: add support for lists starting other index than 1
        } else {
            lists.pop();
            olNextIndex.pop().toString();
        }
    } else if (tag.equalsIgnoreCase("li")) {
        if (opening) {
            if (output.length() > 0 && output.charAt(output.length() - 1) != '\n') {
                output.append("\n");
            }
            String parentList = lists.peek();
            if (parentList.equalsIgnoreCase("ol")) {
                start(output, new Ol());
                output.append(olNextIndex.peek().toString() + ". ");
                olNextIndex.push(Integer.valueOf(olNextIndex.pop().intValue() + 1));
            } else if (parentList.equalsIgnoreCase("ul")) {
                start(output, new Ul());
            }
        } else {
            if (lists.peek().equalsIgnoreCase("ul")) {
                if (output.length() > 0 && output.charAt(output.length() - 1) != '\n') {
                    output.append("\n");
                }
                // Nested BulletSpans increases distance between bullet and text, so we must prevent it.
                int bulletMargin = indent;
                if (lists.size() > 1) {
                    bulletMargin = indent - bullet.getLeadingMargin(true);
                    if (lists.size() > 2) {
                        // This get's more complicated when we add a LeadingMarginSpan into the same line:
                        // we have also counter it's effect to BulletSpan
                        bulletMargin -= (lists.size() - 2) * listItemIndent;
                    }
                }
                BulletSpan newBullet = new BulletSpan(bulletMargin);
                end(output, Ul.class, new LeadingMarginSpan.Standard(listItemIndent * (lists.size() - 1)),
                        newBullet);
            } else if (lists.peek().equalsIgnoreCase("ol")) {
                if (output.length() > 0 && output.charAt(output.length() - 1) != '\n') {
                    output.append("\n");
                }
                int numberMargin = listItemIndent * (lists.size() - 1);
                if (lists.size() > 2) {
                    // Same as in ordered lists: counter the effect of nested Spans
                    numberMargin -= (lists.size() - 2) * listItemIndent;
                }
                end(output, Ol.class, new LeadingMarginSpan.Standard(numberMargin));
            }
        }
    } else {
        if (opening)
            Log.d("TagHandler", "Found an unsupported tag " + tag);
    }
}

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

/**
 * Used to maintain the same indentation as the upper line
 * @param editable Text/* ww  w. ja  v a  2 s. co  m*/
 * @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 static int findText(final Editable text, final int offset) {
    if (text.charAt(offset) != ' ')
        return offset;
    return -1;/*from w  ww .j a v  a 2  s.  com*/
}

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

void sanitizeBetween() {
    // Don't sanitize while we are waiting for content to chipify.
    if (mPendingChipsCount > 0)
        return;/*from  www  . j a va  2  s . co m*/
    // Find the last chip.
    final DrawableRecipientChip[] recips = getSortedRecipients();
    if (recips != null && recips.length > 0) {
        final DrawableRecipientChip last = recips[recips.length - 1];
        DrawableRecipientChip beforeLast = null;
        if (recips.length > 1)
            beforeLast = recips[recips.length - 2];
        int startLooking = 0;
        final int end = getSpannable().getSpanStart(last);
        if (beforeLast != null) {
            startLooking = getSpannable().getSpanEnd(beforeLast);
            final Editable text = getText();
            if (startLooking == -1 || startLooking > text.length() - 1)
                // There is nothing after this chip.
                return;
            if (text.charAt(startLooking) == ' ')
                startLooking++;
        }
        if (startLooking >= 0 && end >= 0 && startLooking < end)
            getText().delete(startLooking, end);
    }
}

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

private int putOffsetInRange(final int o) {
    int offset = o;
    final Editable text = getText();
    final int length = text.length();
    // Remove whitespace from end to find "real end"
    int realLength = length;
    for (int i = length - 1; i >= 0; i--)
        if (text.charAt(i) == ' ')
            realLength--;/* w w w  .ja  v  a 2 s. co m*/
        else
            break;
    // If the offset is beyond or at the end of the text,
    // leave it alone.
    if (offset >= realLength)
        return offset;
    final Editable editable = getText();
    while (offset >= 0 && findText(editable, offset) == -1 && findChip(offset) == null)
        // Keep walking backward!
        offset--;
    return offset;
}

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

/**
 * Remove the chip and any text associated with it from the RecipientEditTextView.
 *
 * @param alsoNotifyAboutDataChanges/*from   ww w . j  ava2 s .  co  m*/
 */
// Visible for testing.
/* package */void removeChip(final DrawableRecipientChip chip, final boolean alsoNotifyAboutDataChanges) {
    if (!alsoNotifyAboutDataChanges)
        --mPreviousChipsCount;
    final Spannable spannable = getSpannable();
    final int spanStart = spannable.getSpanStart(chip);
    final int spanEnd = spannable.getSpanEnd(chip);
    final Editable text = getText();
    int toDelete = spanEnd;
    final boolean wasSelected = chip == mSelectedChip;
    // Clear that there is a selected chip before updating any text.
    if (wasSelected)
        mSelectedChip = null;
    // Always remove trailing spaces when removing a chip.
    while (toDelete >= 0 && toDelete < text.length() && text.charAt(toDelete) == ' ')
        toDelete++;
    spannable.removeSpan(chip);
    if (spanStart >= 0 && toDelete > 0)
        text.delete(spanStart, toDelete);
    if (wasSelected)
        clearSelectedChip();
}

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

private void shrink() {
    if (mTokenizer == null)
        return;//  www .j  av  a  2s  .co m
    final long contactId = mSelectedChip != null ? mSelectedChip.getEntry().getContactId() : -1;
    if (mSelectedChip != null && contactId != RecipientEntry.INVALID_CONTACT && !isPhoneQuery()
            && contactId != RecipientEntry.GENERATED_CONTACT)
        clearSelectedChip();
    else {
        if (getWidth() <= 0) {
            // We don't have the width yet which means the view hasn't been drawn yet
            // and there is no reason to attempt to commit chips yet.
            // This focus lost must be the result of an orientation change
            // or an initial rendering.
            // Re-post the shrink for later.
            mHandler.removeCallbacks(mDelayedShrink);
            mHandler.post(mDelayedShrink);
            return;
        }
        // Reset any pending chips as they would have been handled
        // when the field lost focus.
        if (mPendingChipsCount > 0)
            postHandlePendingChips();
        else {
            final Editable editable = getText();
            final int end = getSelectionEnd();
            final int start = mTokenizer.findTokenStart(editable, end);
            final DrawableRecipientChip[] chips = getSpannable().getSpans(start, end,
                    DrawableRecipientChip.class);
            if (chips == null || chips.length == 0 && start >= 0) {
                final Editable text = getText();
                // TODO check why this code can crash (index out of bounds). Currently fixed by checking that
                // start>=0
                int whatEnd = mTokenizer.findTokenEnd(text, start);
                // This token was already tokenized, so skip past the ending token.
                if (whatEnd < text.length() && text.charAt(whatEnd) == ',')
                    whatEnd = movePastTerminators(whatEnd);
                // In the middle of chip; treat this as an edit
                // and commit the whole token.
                final int selEnd = getSelectionEnd();
                if (whatEnd != selEnd)
                    handleEdit(start, whatEnd);
                else
                    commitChip(start, end, editable);
            }
        }
        mHandler.post(mAddTextWatcher);
    }
    createMoreChip();
}

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);/*from  w  ww .j  av a 2  s  .c  o 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;
}