Example usage for android.text Spannable length

List of usage examples for android.text Spannable length

Introduction

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

Prototype

int length();

Source Link

Document

Returns the length of this character sequence.

Usage

From source file:com.icecream.snorlax.module.feature.encounter.EncounterNotification.java

private Spannable getBoldSpannable(String text) {
    Spannable spannable = new SpannableString(text);
    spannable.setSpan(new StyleSpan(Typeface.BOLD), 0, spannable.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    return spannable;
}

From source file:com.keylesspalace.tusky.activity.ComposeActivity.java

private static void highlightSpans(Spannable text, int colour) {
    // Strip all existing colour spans.
    int n = text.length();
    ForegroundColorSpan[] oldSpans = text.getSpans(0, n, ForegroundColorSpan.class);
    for (int i = oldSpans.length - 1; i >= 0; i--) {
        text.removeSpan(oldSpans[i]);/*from   w  w  w . j  a v a  2  s .  c  om*/
    }
    // Colour the mentions and hashtags.
    String string = text.toString();
    int start;
    int end = 0;
    while (end < n) {
        char[] chars = { '#', '@' };
        FindCharsResult found = findStart(string, end, chars);
        start = found.stringIndex;
        if (start < 0) {
            break;
        }
        if (found.charIndex == 0) {
            end = findEndOfHashtag(string, start);
        } else if (found.charIndex == 1) {
            end = findEndOfMention(string, start);
        } else {
            break;
        }
        if (end < 0) {
            break;
        }
        text.setSpan(new ForegroundColorSpan(colour), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    }
}

From source file:com.android.messaging.datamodel.MessageNotificationState.java

private static void stripUrls(final Spannable text) {
    final URLSpan[] spans = text.getSpans(0, text.length(), URLSpan.class);
    for (final URLSpan span : spans) {
        text.removeSpan(span);/*  ww  w  .  jav a 2  s.co m*/
    }
}

From source file:com.googlecode.eyesfree.brailleback.NodeBrailler.java

/**
 * Adds {@code node} as a span on {@code content} if not already
 * fully covered by an accessibility node info span.
 */// w ww  . j  av  a 2  s .co m
private void addNodeSpanForUncovered(AccessibilityNodeInfoCompat node, Spannable spannable) {
    AccessibilityNodeInfoCompat[] spans = spannable.getSpans(0, spannable.length(),
            AccessibilityNodeInfoCompat.class);
    for (AccessibilityNodeInfoCompat span : spans) {
        if (spannable.getSpanStart(span) == 0 && spannable.getSpanEnd(span) == spannable.length()) {
            return;
        }
    }
    DisplaySpans.setAccessibilityNode(spannable, node);
}

From source file:com.lloydtorres.stately.helpers.SparkleHelper.java

/**
 * Stylify text view to primary colour and no underline
 * @param c App context/*from  w  ww  . j a v a 2 s .c om*/
 * @param t TextView
 */
public static void styleLinkifiedTextView(Context c, TextView t) {
    // Get individual spans and replace them with clickable ones.
    Spannable s = new SpannableString(t.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);
        span = new URLSpanNoUnderline(c, span.getURL());
        s.setSpan(span, start, end, 0);
    }

    t.setText(s);
    // Need to set this to allow for clickable TextView links.
    if (!(t instanceof HtmlTextView)) {
        t.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

From source file:com.pocketsoap.convodroid.AuthorMessageFragment.java

@Override
public void onClick(View v) {
    Log.i("Convodroid", "send " + messageText.getText() + " to " + recipients.getText());
    Spannable r = (Spannable) recipients.getText();
    sendButton.setEnabled(false);//from www . j a v  a2  s .  c o m
    UserSpan[] users = r.getSpans(0, r.length(), UserSpan.class);
    List<String> recipients = new ArrayList<String>(users.length);
    for (UserSpan us : users)
        recipients.add(us.user.id);
    NewMessage m = new NewMessage();
    m.recipients = recipients;
    m.body = messageText.getText().toString();
    final RestRequest req = ChatterRequests.postMessage(m);
    getLoaderManager().restartLoader(0, null, new LoaderCallbacks<String>() {

        @Override
        public Loader<String> onCreateLoader(int id, Bundle args) {
            return new JsonLoader<String>(getActivity(), client, req, new TypeReference<String>() {
            });
        }

        @Override
        public void onLoadFinished(Loader<String> loader, String val) {
            getActivity().setResult(Activity.RESULT_OK);
            getActivity().finish();
        }

        @Override
        public void onLoaderReset(Loader<String> loader) {
        }
    });
}

From source file:mobisocial.musubi.ui.util.EmojiSpannableFactory.java

public void updateSpannable(Spannable span) {
    Spannable source = span;
    for (int i = 0; i < source.length(); i++) {
        char high = source.charAt(i);
        if (high <= 127) {
            // fast exit ascii
            continue;
        }//from w ww .java  2 s.  co  m

        // Block until we're initialized
        waitForEmoji();

        long codePoint = high;
        if (Character.isHighSurrogate(high)) {
            char low = source.charAt(++i);
            codePoint = Character.toCodePoint(high, low);
            if (Character.isSurrogatePair(high, low)) {
                // from BMP
                if (!mEmojiMap.containsKey(codePoint)) {
                    if (i >= source.length() - 2) {
                        continue;
                    }
                    high = source.charAt(++i);
                    if (!Character.isHighSurrogate(high)) {
                        Log.w(TAG, "bad unicode character? " + high);
                        continue;
                    }
                    low = source.charAt(++i);
                    if (!Character.isSurrogatePair(high, low)) {
                        Log.d(TAG, "Bogus unicode surrogate " + high + ", " + low);
                        continue;
                    }
                    int codePoint2 = Character.toCodePoint(high, low);
                    //String label = String.format("U+%X U+%X", codePoint, codePoint2);
                    codePoint = ((long) codePoint << 16) | codePoint2;
                }
            } else {
                Log.d(TAG, "Bogus unicode");
            }
        }

        if (mEmojiMap.containsKey(codePoint)) {
            Bitmap b = mStickerCache.get(codePoint);
            if (b != null) {
                DynamicDrawableSpan im = createStickerSpan(b);
                span.setSpan(im, i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                Log.d(TAG, "failed to decode bitmap for codepoints: " + codePoint);
            }
        }
    }
}

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

private static void setSpanFromMark(Spannable text, Object mark, Object... spans) {
    int where = text.getSpanStart(mark);
    text.removeSpan(mark);/* w w  w.  j  av a2s . c  o m*/
    int len = text.length();
    if (where != len) {
        for (Object span : spans) {
            text.setSpan(span, where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}

From source file:com.arantius.tivocommander.Person.java

private void requestFinished() {
    if (--mOutstandingRequests > 0) {
        return;//from w w  w.  j a v a 2s  .c o  m
    }
    setProgressBarIndeterminateVisibility(false);

    if (mPerson == null || mCredits == null) {
        setContentView(R.layout.no_results);
        return;
    }

    setContentView(R.layout.list_person);

    // Credits.
    JsonNode[] credits = new JsonNode[mCredits.size()];
    int i = 0;
    for (JsonNode credit : mCredits) {
        credits[i++] = credit;
    }

    ListView lv = getListView();
    CreditsAdapter adapter = new CreditsAdapter(Person.this, R.layout.item_person_credits, credits);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(mOnItemClickListener);

    // Name.
    ((TextView) findViewById(R.id.person_name)).setText(mName);

    // Role.
    JsonNode rolesNode = mPerson.path("roleForPersonId");
    String[] roles = new String[rolesNode.size()];
    for (i = 0; i < rolesNode.size(); i++) {
        roles[i] = rolesNode.path(i).asText();
        roles[i] = Utils.ucFirst(roles[i]);
    }
    ((TextView) findViewById(R.id.person_role)).setText(Utils.join(", ", roles));

    // Birth date.
    TextView birthdateView = ((TextView) findViewById(R.id.person_birthdate));
    if (mPerson.has("birthDate")) {
        Date birthdate = Utils.parseDateStr(mPerson.path("birthDate").asText());
        SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMMM d, yyyy", Locale.US);
        dateFormatter.setTimeZone(TimeZone.getDefault());
        Spannable birthdateStr = new SpannableString("Birthdate: " + dateFormatter.format(birthdate));
        birthdateStr.setSpan(new ForegroundColorSpan(Color.WHITE), 11, birthdateStr.length(), 0);
        birthdateView.setText(birthdateStr);
    } else {
        birthdateView.setVisibility(View.GONE);
    }

    // Birth place.
    TextView birthplaceView = ((TextView) findViewById(R.id.person_birthplace));
    if (mPerson.has("birthPlace")) {
        Spannable birthplaceStr = new SpannableString("Birthplace: " + mPerson.path("birthPlace").asText());
        birthplaceStr.setSpan(new ForegroundColorSpan(Color.WHITE), 12, birthplaceStr.length(), 0);
        birthplaceView.setText(birthplaceStr);
    } else {
        birthplaceView.setVisibility(View.GONE);
    }

    ImageView iv = (ImageView) findViewById(R.id.person_image);
    View pv = findViewById(R.id.person_image_progress);
    String imgUrl = Utils.findImageUrl(mPerson);
    new DownloadImageTask(this, iv, pv).execute(imgUrl);
}

From source file:com.zyz.mobile.book.UserSpan.java

/**
 * checks whether this span is valid against the specified spnnable
 *
 * @param spannable the spannable this {@code UserSpan} is created for
 * @return true if this {@code UserSpan} is valid, false otherwise
 */// ww  w . j  av a  2 s. com
public boolean isValid(Spannable spannable) {
    if (mType == UserSpanType.NONE || mType != UserSpanType.BOOKMARK && mStart >= mEnd || mStart < 0) {
        // if it's a bookmark, only the {@ode mStart} is specified, so no need
        // to check for this condition
        return false;
    }
    return mEnd < spannable.length();
}