Java Code Examples for android.text.SpannedString

The following examples show how to use android.text.SpannedString. These examples are extracted from open source projects. 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 Project: android_9.0.0_r45   Source File: RecordingCanvas.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public final void drawText(@NonNull CharSequence text, int start, int end, float x, float y,
        @NonNull Paint paint) {
    if ((start | end | (end - start) | (text.length() - end)) < 0) {
        throw new IndexOutOfBoundsException();
    }
    if (text instanceof String || text instanceof SpannedString
            || text instanceof SpannableString) {
        nDrawText(mNativeCanvasWrapper, text.toString(), start, end, x, y,
                paint.mBidiFlags, paint.getNativeInstance());
    } else if (text instanceof GraphicsOperations) {
        ((GraphicsOperations) text).drawText(this, start, end, x, y,
                paint);
    } else {
        char[] buf = TemporaryBuffer.obtain(end - start);
        TextUtils.getChars(text, start, end, buf, 0);
        nDrawText(mNativeCanvasWrapper, buf, 0, end - start, x, y,
                paint.mBidiFlags, paint.getNativeInstance());
        TemporaryBuffer.recycle(buf);
    }
}
 
Example 2
@Test
public void textWithQuote() {
    SpannedString result = builder.markdownToSpans("Text\n> Quote");

    assertEquals("Text\nQuote", result.toString());
    Object[] spans = result.getSpans(0, result.length(), Object.class);
    assertEquals(3, spans.length);

    StyleSpan styleSpan = (StyleSpan) spans[0];
    assertEquals(Typeface.ITALIC, styleSpan.getStyle());
    assertEquals(5, result.getSpanStart(styleSpan));
    assertEquals(10, result.getSpanEnd(styleSpan));
    LeadingMarginSpan leadingMarginSpan = (LeadingMarginSpan) spans[1];
    assertEquals(5, result.getSpanStart(leadingMarginSpan));
    assertEquals(10, result.getSpanEnd(leadingMarginSpan));
    RelativeSizeSpan relativeSizeSpan = (RelativeSizeSpan) spans[2];
    assertEquals(5, result.getSpanStart(relativeSizeSpan));
    assertEquals(10, result.getSpanEnd(relativeSizeSpan));
}
 
Example 3
Source Project: UETool   Source File: Util.java    License: MIT License 6 votes vote down vote up
private static List<Pair<String, Bitmap>> getTextViewImageSpanBitmap(TextView textView) {
    List<Pair<String, Bitmap>> bitmaps = new ArrayList<>();
    try {
        CharSequence text = textView.getText();
        if (text instanceof SpannedString) {
            Field mSpansField = Class.forName("android.text.SpannableStringInternal").getDeclaredField("mSpans");
            mSpansField.setAccessible(true);
            Object[] spans = (Object[]) mSpansField.get(text);
            for (Object span : spans) {
                if (span instanceof ImageSpan) {
                    bitmaps.add(new Pair<>("SpanBitmap", getDrawableBitmap(((ImageSpan) span).getDrawable())));
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmaps;
}
 
Example 4
Source Project: friendspell   Source File: CustomMatchers.java    License: Apache License 2.0 6 votes vote down vote up
public static Matcher<View> withColors(final int... colors) {
  return new BoundedMatcher<View, TextView>(TextView.class) {
    @Override public boolean matchesSafely(TextView textView) {
      SpannedString text = (SpannedString) textView.getText();
      ForegroundColorSpan[] spans = text.getSpans(0, text.length(), ForegroundColorSpan.class);
      if (spans.length != colors.length) {
        return false;
      }
      for (int i = 0; i < colors.length; ++i) {
        if (spans[i].getForegroundColor() != colors[i]) {
          return false;
        }
      }
      return true;
    }
    @Override public void describeTo(Description description) {
      description.appendText("has colors:");
      for (int color : colors) {
        description.appendText(" " + getHexColor(color));
      }
    }
  };
}
 
Example 5
Source Project: edx-app-android   Source File: TextUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns displayable styled text from the provided HTML string.
 * <br/>
 * Note: Also handles the case when a String has multiple HTML entities that translate to one
 * character e.g. {@literal &amp;#39;} which is essentially an apostrophe that should normally
 * occur as {@literal &#39;}
 *
 * @param html The source string having HTML content.
 * @return Formatted HTML.
 */
@NonNull
public static Spanned formatHtml(@NonNull String html) {
    final String REGEX = "(&#?[a-zA-Z0-9]+;)";
    final Pattern PATTERN = Pattern.compile(REGEX);

    Spanned formattedHtml = new SpannedString(html);
    String previousHtml = null;

    // Break the loop if there isn't an HTML entity in the text or when all the HTML entities
    // have been decoded. Also break the loop in the special case when a String having the
    // same format as an HTML entity is left but it isn't essentially a decodable HTML entity
    // e.g. &#asdfasd;
    while (PATTERN.matcher(html).find() && !html.equals(previousHtml)) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            formattedHtml = Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
        } else {
            formattedHtml = Html.fromHtml(html);
        }
        previousHtml = html;
        html = formattedHtml.toString();
    }

    return formattedHtml;
}
 
Example 6
Source Project: android-test   Source File: ViewMatchersTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void withContentDescriptionString() {
  View view = new View(context);
  view.setContentDescription(null);
  assertTrue(withContentDescription(nullValue(String.class)).matches(view));

  String testText = "test text!";
  view.setContentDescription(testText);
  assertTrue(withContentDescription(is(testText)).matches(view));
  assertFalse(withContentDescription(is("blah")).matches(view));
  assertFalse(withContentDescription(is("")).matches(view));

  // Test withContentDescription(String) directly.
  assertTrue(withContentDescription(testText).matches(view));

  view.setContentDescription(null);
  String nullString = null;
  assertTrue(withContentDescription(nullString).matches(view));
  assertFalse(withContentDescription(testText).matches(view));

  // Test when the view's content description is not a String type.
  view.setContentDescription(new SpannedString(testText));
  assertTrue(withContentDescription(testText).matches(view));
  assertFalse(withContentDescription("different text").matches(view));
}
 
Example 7
Source Project: something.apk   Source File: ThreadViewFragment.java    License: MIT License 6 votes vote down vote up
/**
 * Trigger page load of specific thread. Called by thread list or url links.
 * See startRefresh() and ./request/ThreadPageRequest for volley implementation.
 * @param threadId Thread ID for requested thread. (Required)
 * @param page Page number to load, Optional, if -1 will go to last post, if 0 will go to newest unread post.
 * @param userId (Optional) UserId for filtering.
 * @param fromUrl True if request was sent by internal URL request. Used to decide if we should push current state into backstack.
 */
public void loadThread(int threadId, int page, int userId, boolean fromUrl) {
    if(fromUrl && isThreadLoaded()){
        threadBackstack.push(saveThreadState(new Bundle()));
    }else{
        threadBackstack.clear();
    }
    this.ignorePageProgress = true;
    this.threadId = threadId;
    this.page = page;
    this.userId = userId;
    this.maxPage = 0;
    this.forumId = 0;
    this.bookmarked = false;
    this.threadTitle = new SpannedString(getString(R.string.thread_view_loading));
    setTitle(threadTitle);
    invalidateOptionsMenu();
    updateNavbar();
    startRefresh();
    threadView.loadUrl("about:blank");
}
 
Example 8
Source Project: something.apk   Source File: ThreadViewFragment.java    License: MIT License 6 votes vote down vote up
/**
 * Trigger page load of specific thread by redirecting from a postID. Called by thread list or url links.
 * See startRefresh() and ./request/ThreadPageRequest for volley implementation.
 * @param postId Post ID to redirect to. (Required)
 * @param fromUrl True if request was sent by internal URL request. Used to decide if we should push current state into backstack.
 */
public void loadPost(long postId, boolean fromUrl){
    if(fromUrl && isThreadLoaded()){
        threadBackstack.push(saveThreadState(new Bundle()));
    }else{
        threadBackstack.clear();
    }
    this.ignorePageProgress = true;
    this.postId = postId;
    this.threadId = 0;
    this.page = 0;
    this.maxPage = 0;
    this.forumId = 0;
    this.bookmarked = false;
    this.threadTitle = new SpannedString(getString(R.string.thread_view_loading));
    setTitle(threadTitle);
    invalidateOptionsMenu();
    updateNavbar();
    startRefresh();
    threadView.loadUrl("about:blank");
}
 
Example 9
Source Project: google-authenticator-android   Source File: Utilities.java    License: Apache License 2.0 6 votes vote down vote up
private static Spanned removeTrailingNewlines(Spanned text) {
  int trailingNewlineCharacterCount = 0;
  for (int i = text.length() - 1; i >= 0; i--) {
    char c = text.charAt(i);
    if ((c == '\n') || (c == '\r')) {
      trailingNewlineCharacterCount++;
    } else {
      break;
    }
  }
  if (trailingNewlineCharacterCount == 0) {
    return text;
  }

  return new SpannedString(
      text.subSequence(0, text.length() - trailingNewlineCharacterCount));
}
 
Example 10
/**
 * Commits a result to the final transcript.
 *
 * <p>NOTE: This does not clear the hypothesis. Users who get partial results (hypotheses) should
 * prefer calling setCurrentHypothesis(...) and then finalizeCurrentHypothesis().
 */
public void addFinalizedResult(TranscriptionResult resultSingleUtterance) {
  String lineBreak = obtainLineBreaksFromLastFinalizedResult(resultSingleUtterance);
  resultsDeque.add(
      new CachedResult(
          resultSingleUtterance.toBuilder().build(),
          formatSingleFinalized(resultSingleUtterance, !lineBreak.isEmpty()),
          SpannedString.valueOf(lineBreak)));
  lastSpeakerId = getLastSpeakerIdTag(resultSingleUtterance);
}
 
Example 11
/** Returns the current finalized text with the hypothesis appended to the end. */
public Spanned getFormattedTranscript() {
  SpannableStringBuilder builder = new SpannableStringBuilder();
  for (CachedResult timestampedAndCachedResult : resultsDeque) {
    builder.append(timestampedAndCachedResult.getFormattedText());
  }
  builder.append(getFormattedHypothesis());

  return new SpannedString(builder);
}
 
Example 12
/** Returns the latest sentence from transcription result. */
public Spanned getMostRecentTranscriptSegment() {
  SpannableStringBuilder builder = new SpannableStringBuilder();
  builder.append(getFormattedHypothesis());
  if (!TextUtils.isEmpty(builder)) {
    return new SpannedString(builder);
  }

  if (!resultsDeque.isEmpty()) {
    CachedResult timestampedAndCachedResult = resultsDeque.getLast();
    builder.append(timestampedAndCachedResult.getFormattedText());
  }

  return new SpannedString(builder);
}
 
Example 13
Source Project: user-interface-samples   Source File: MarkdownBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public SpannedString markdownToSpans(@NonNull final String string) {
    TextMarkdown markdown = parser.parse(string);

    // In the SpannableStringBuilder, the text and the markup are mutable.
    SpannableStringBuilder builder = new SpannableStringBuilder();
    for (int i = 0; i < markdown.getElements().size(); i++) {
        buildSpans(markdown.getElements().get(i), builder);
    }
    return new SpannedString(builder);
}
 
Example 14
@Test
public void text() {
    SpannedString result = builder.markdownToSpans("Text");

    Object[] spans = result.getSpans(0, result.length(), Object.class);
    assertEquals(0, spans.length);
}
 
Example 15
@Test
public void textWithBulletPoints() {
    SpannedString result = builder.markdownToSpans("Points\n* one\n+ two");

    assertEquals("Points\none\ntwo", result.toString());
    Object[] spans = result.getSpans(0, result.length(), Object.class);
    assertEquals(2, spans.length);

    BulletPointSpan bulletSpan = (BulletPointSpan) spans[0];
    assertEquals(7, result.getSpanStart(bulletSpan));
    assertEquals(11, result.getSpanEnd(bulletSpan));
    BulletPointSpan bulletSpan2 = (BulletPointSpan) spans[1];
    assertEquals(11, result.getSpanStart(bulletSpan2));
    assertEquals(14, result.getSpanEnd(bulletSpan2));
}
 
Example 16
@Test
public void textWithCode() {
    SpannedString result = builder.markdownToSpans("Text `code`");

    assertEquals("Text code", result.toString());
    Object[] spans = result.getSpans(0, result.length(), Object.class);
    assertEquals(1, spans.length);

    CodeBlockSpan codeSpan = (CodeBlockSpan) spans[0];
    assertEquals(5, result.getSpanStart(codeSpan));
    assertEquals(9, result.getSpanEnd(codeSpan));
}
 
Example 17
Source Project: youqu_master   Source File: UiUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 设置hint大小
 *
 * @param size
 * @param v
 * @param res
 */
public static void setViewHintSize(Context context, int size, TextView v, int res) {
    SpannableString ss = new SpannableString(getResources(context).getString(
            res));
    // 新建一个属性对象,设置文字的大小
    AbsoluteSizeSpan ass = new AbsoluteSizeSpan(size, true);
    // 附加属性到文本
    ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    // 设置hint
    v.setHint(new SpannedString(ss)); // 一定要进行转换,否则属性会消失
}
 
Example 18
Source Project: PowerFileExplorer   Source File: TextUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static CharSequence stringOrSpannedString(CharSequence source) {
    if (source == null)
        return null;
    if (source instanceof SpannedString)
        return source;
    if (source instanceof Spanned)
        return new SpannedString(source);

    return source.toString();
}
 
Example 19
Source Project: TikTok   Source File: MvpUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 设置hint大小
 *
 * @param size
 * @param v
 * @param res
 */
public static void setViewHintSize(Context context, int size, TextView v, int res) {
    SpannableString ss = new SpannableString(getResources(context).getString(
            res));
    // 新建一个属性对象,设置文字的大小
    AbsoluteSizeSpan ass = new AbsoluteSizeSpan(size, true);
    // 附加属性到文本
    ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    // 设置hint
    v.setHint(new SpannedString(ss)); // 一定要进行转换,否则属性会消失
}
 
Example 20
Source Project: react-native-GPay   Source File: RedBoxDialog.java    License: MIT License 5 votes vote down vote up
@Override
public void onReportSuccess(final SpannedString spannedString) {
  isReporting = false;
  Assertions.assertNotNull(mReportButton).setEnabled(true);
  Assertions.assertNotNull(mLoadingIndicator).setVisibility(View.GONE);
  Assertions.assertNotNull(mReportTextView).setText(spannedString);
}
 
Example 21
Source Project: react-native-GPay   Source File: RedBoxDialog.java    License: MIT License 5 votes vote down vote up
@Override
public void onReportError(final SpannedString spannedString) {
  isReporting = false;
  Assertions.assertNotNull(mReportButton).setEnabled(true);
  Assertions.assertNotNull(mLoadingIndicator).setVisibility(View.GONE);
  Assertions.assertNotNull(mReportTextView).setText(spannedString);
}
 
Example 22
Source Project: Aurora   Source File: UiUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 设置hint大小
 *
 * @param size
 * @param v
 * @param res
 */
public static void setViewHintSize(Context context, int size, TextView v, int res) {
    SpannableString ss = new SpannableString(getResources(context).getString(
            res));
    // 新建一个属性对象,设置文字的大小
    AbsoluteSizeSpan ass = new AbsoluteSizeSpan(size, true);
    // 附加属性到文本  
    ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    // 设置hint  
    v.setHint(new SpannedString(ss)); // 一定要进行转换,否则属性会消失
}
 
Example 23
Source Project: Aurora   Source File: ArmsUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 设置hint大小
 *
 * @param size
 * @param v
 * @param res
 */
public static void setViewHintSize(Context context, int size, TextView v, int res) {
    SpannableString ss = new SpannableString(getResources(context).getString(
            res));
    // 新建一个属性对象,设置文字的大小
    AbsoluteSizeSpan ass = new AbsoluteSizeSpan(size, true);
    // 附加属性到文本  
    ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    // 设置hint  
    v.setHint(new SpannedString(ss)); // 一定要进行转换,否则属性会消失
}
 
Example 24
Source Project: MVVMArms   Source File: UiUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 设置hint大小
 *
 * @param size
 * @param v
 * @param res
 */
public static void setViewHintSize(Context context, int size, TextView v, int res) {
    SpannableString ss = new SpannableString(getResources(context).getString(
            res));
    // 新建一个属性对象,设置文字的大小
    AbsoluteSizeSpan ass = new AbsoluteSizeSpan(size, true);
    // 附加属性到文本
    ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    // 设置hint,一定要进行转换,否则属性会消失
    v.setHint(new SpannedString(ss));
}
 
Example 25
Source Project: DebugRank   Source File: AndroidHtmlToDisplay.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Spanned convertHtmlToDisplayType(String htmlString, String normalString)
{
    if(htmlString == null)
    {
        return new SpannedString(normalString);
    }

    return Html.fromHtml(htmlString);
}
 
Example 26
Source Project: memoir   Source File: RTPlainText.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public RTText convertTo(RTFormat destFormat, RTMediaFactory<RTImage, RTAudio, RTVideo> mediaFactory) {
    if (destFormat instanceof RTFormat.Html) {
        return ConverterTextToHtml.convert(this);
    } else if (destFormat instanceof RTFormat.Spanned) {
        return new RTSpanned(new SpannedString(getText()));
    }

    return super.convertTo(destFormat, mediaFactory);
}
 
Example 27
Source Project: memoir   Source File: RTPlainText.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public RTText convertTo(RTFormat destFormat, RTMediaFactory<RTImage, RTAudio, RTVideo> mediaFactory) {
    if (destFormat instanceof RTFormat.Html) {
        return ConverterTextToHtml.convert(this);
    } else if (destFormat instanceof RTFormat.Spanned) {
        return new RTSpanned(new SpannedString(getText()));
    }

    return super.convertTo(destFormat, mediaFactory);
}
 
Example 28
Source Project: tindroid   Source File: SpanFormatter.java    License: Apache License 2.0 5 votes vote down vote up
public static Spanned toSpanned(final TextView container, final Drafty content,
                                 final ClickListener clicker) {
    if (content == null) {
        return new SpannedString("");
    }
    if (content.isPlain()) {
        return new SpannedString(content.toString());
    }
    TreeNode result = content.format(new SpanFormatter(container, clicker));
    return result.toSpanned();
}
 
Example 29
Source Project: MVPArms   Source File: ArmsUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 设置hint大小
 *
 * @param size
 * @param v
 * @param res
 */
public static void setViewHintSize(Context context, int size, TextView v, int res) {
    SpannableString ss = new SpannableString(getResources(context).getString(
            res));
    // 新建一个属性对象,设置文字的大小
    AbsoluteSizeSpan ass = new AbsoluteSizeSpan(size, true);
    // 附加属性到文本  
    ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    // 设置hint  
    v.setHint(new SpannedString(ss)); // 一定要进行转换,否则属性会消失
}
 
Example 30
Source Project: AndroidBase   Source File: UiUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 设置hint大小
 *
 * @param size
 * @param v
 * @param res
 */
public static void setViewHintSize(int size, TextView v, int res) {
    SpannableString ss = new SpannableString(getResources().getString(
            res));
    // 新建一个属性对象,设置文字的大小
    AbsoluteSizeSpan ass = new AbsoluteSizeSpan(size, true);
    // 附加属性到文本  
    ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    // 设置hint  
    v.setHint(new SpannedString(ss)); // 一定要进行转换,否则属性会消失
}